Friday, June 24, 2016

C program to understand the concept of initialization in 2D arrays

#include<stdio.h>
void main()
{
//here inner curly braced values will be assigned to 0th array and outer to 1th array
 int arr_new[3][4]={{1,2},3,4};
 arr_new[2][3]=0;
 printf("\n%d %d %d %d %d",arr_new[0][0],arr_new[0][1],arr_new[0][2],arr_new[1][0],arr_new[1][1]);
}

output-
1 2 0 3 4

Sunday, June 19, 2016

C program to make two matrix dynamically and show thier addition

#include <stdio.h>
#include<stdio.h>
int main()
{
   int row, colum, row_element, colum_element, first[50][50], second[50][50], sum[50][50];
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &row, &colum);
   if(row>0&&colum>0)
   {
   printf("Enter the elements of first matrix\n");
   for (row_element = 0; row_element < row; row_element++)
   {
     for (colum_element= 0; colum_element <colum; colum_element++)
      {
         scanf("%d", &first[row_element][colum_element]);
      }
   }
   printf("Enter the elements of second matrix\n");
   for (row_element = 0; row_element < row; row_element++)
   {
      for (colum_element = 0 ; colum_element< colum; colum_element++)
      {
            scanf("%d", &second[row_element][colum_element]);
      }
   }
   printf("Sum of entered matrices:-\n");
   for (row_element = 0; row_element< row; row_element++)
    {
      for (colum_element= 0 ; colum_element < colum; colum_element++)
  {
    sum[row_element][colum_element] = first[row_element][colum_element] +                                        second[row_element][colum_element];
    printf("%d\t", sum[row_element][colum_element]);
      }
 printf("\n");
   }
   }
   else
   printf("please enter positive number only");
}

output
Enter the number of rows and columns of matrix
2 2
Enter the elements of first matrix 1
2
3
2
Enter the elements of second matrix 2
3
2
1
Sum of entered matrices:-
3  5
5  3

Friday, June 17, 2016

C program to calculate GCD (greatest common divisors) of two numbers

Problem Statement:- GCD of two numbers is the greatest number which divides both the numbers
ex:- 20 and 10 have 10 as GCD because it divides both the numbers completely.

#include<stdio.h>
void function_gcd( int catch_firstnumber,int catch_secondnumber,int minimum_number1 )
{
  int counter_variable,gcd;
  for(counter_variable=1;counter_variable<=minimum_number1;counter_variable++)
  {
    if(catch_firstnumber%counter_variable==0 && catch_secondnumber % counter_variable == 0 )
gcd=counter_variable;
  }
 printf("the gcd of %d and %d is %d",catch_firstnumber,catch_secondnumber,gcd);
}
main()
{
float first_number,second_number;
int minimum_number,counter_variable,gcd;
int first_temp,second_temp;
clrscr();
printf("Enter the First integer number\n");
scanf("%f",&first_number);
first_temp=first_number;
        //processing for calculating GCD
while((first_number/first_temp!=1)||(first_temp<0))
{
if(first_number/first_temp!=1)
{
 printf("\nEnter a integer number only Enter first number again");
}
else
{
 printf("\nEnter a positive number only Enter first number again");
}
scanf("%f",&first_number);
first_temp=first_number;
}
printf("\nEnter the Second integer");
scanf("%f",&second_number);
second_temp=second_number;
while((second_number/second_temp!=1)||(second_temp<0))
{
if(second_number/second_temp!=1)
{
 printf("\nEnter a integer number only Enter Second number again");
}
else
{
 printf("\nEnter a positive number only Enter Second number again");
}
scanf("%f",&second_number);
second_temp=second_number;
}
    
if(first_number>second_number)
{
  minimum_number = second_number;
  function_gcd(first_number,second_number,minimum_number);
}
else
{
 minimum_number = first_number;
 function_gcd(first_number,second_number,minimum_number);
}
return 0;
}
output:
Enter the First integer number 20
Enter the Second integer number 10
the gcd of 20 and 10 is 10

Wednesday, June 15, 2016

C program to save and retrieve records from external file

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 200
struct emp
{
    int id;
    char *name;
    int eng;
    int math;
    int hindi;
}*emp1, *emp3;
void display();
void create();
FILE *fp, *fp1;
int count = 0;
void main()
{
    int i, n, ch;
    printf("1 Create a Record\n");
    printf("2 Display Records\n");
    printf("3 Exit");
    while (1)
    {
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
   fp = fopen("file.txt", "a");
   create();
   break;
case 2:
   fp1 = fopen("file.txt","rb");
   display();
   break;
case 3:
   exit(0);
}
    }
}
void create()
{
    int i;
    char *p;
    emp1 = (struct emp *)malloc(sizeof(struct emp));
    emp1->name = (char *)malloc((size)*(sizeof(char)));
    printf("Enter name of employee : ");
    scanf(" %[^\n]s", emp1->name);
    printf("Enter emp id : ");
    scanf(" %d", &emp1->id);
 printf("\nEnter English Marks");
 scanf("%d",&emp1->eng);
 printf("\nEnter Maths Marks");
 scanf("%d",&emp1->math);
 printf("\nEnter Hindi Marks");
 scanf("%d",&emp1->hindi);
    fwrite(&emp1->id, sizeof(emp1->id), 1, fp);
    fwrite(emp1->name, size, 1, fp);
    fwrite(&emp1->eng, sizeof(emp1->eng), 1, fp);
    fwrite(&emp1->math, sizeof(emp1->math), 1, fp);
    fwrite(&emp1->hindi, sizeof(emp1->hindi), 1,fp);
    count++;
    fclose(fp);
}
void display()
{
    int i=1;
    emp3=(struct emp *)malloc(1*sizeof(struct emp));
    emp3->name=(char *)malloc(size*sizeof(char));
    if (fp1 == NULL)
    printf("\nFile not opened for reading");
    printf("Employee ID Employee Name English Maths Hindi");
    while (i <= count)
    {
fread(&emp3->id, sizeof(emp3->id), 1, fp1);
fread(emp3->name, size, 1, fp1);
fread(&emp3->eng, sizeof(emp3->eng), 1, fp1);
fread(&emp3->math, sizeof(emp3->math), 1, fp1);
fread(&emp3->hindi, sizeof(emp3->hindi), 1, fp1);
printf("\n%d %s %d %d %d",emp3->id,emp3->name,emp3->eng,emp3->math,emp3->hindi);
i++;
    }
    fclose(fp1);
    free(emp3->name);
    free(emp3);
}

output
1 Create a record
2 Display Records
3 Exit
Enter your choice 1
Enter name of employee: sasas
eneter emp id: 1
Enter Englist Marks 70
Enter Maths Marks 75
Enter Hindi Marks 60
Enter your choice 1
Enter name of employee sasa
enter emp id: 2
Enter English Marks 65
Enter Maths Marks 82
Enter Hindi Marks 70
Enter your choice 2
Employee ID     Employee Name    English    Maths    Hindi
    1                        sasas                      70          75            60
    2                        sasa                        65         82            70

Monday, June 13, 2016

c program to find factorial of a number without using recursive technique

#include<stdio.h>
#include<conio.h>
void main()
 {
  clrscr();
  int n,m,sum=1,a;
  printf("Enter a no");
  scanf("%d",&n);
//it will loop through the number and multiply all its small integers
  for(int i=1;i<=n;i++)
   {
    sum=sum*i;
   }
  printf("Factorial = %d",sum);
 getch();
}

output
Enter a no 6
720

Wednesday, June 8, 2016

C program to select elements from three given array and make sum of zero

Problem Statement: you're given three arrays and you have to pick one element from each array to make sum of zero
Example
 int a[]={1,7,5,9};
 int b[]={-2,-1,1,2};
 int c[]={-8,-7,1,-8};
taking 1 from a,-2 from b and 1 from c is making a combination with sum as zero
1 + -2 + 1 = 0

now ignoring these values, and repeating the process
taking 7 from first array, taking -1 from b isn't suiting with any element of c, taking 1 from b and -8 from c making combination with sum as zero
7 + 1 + -8 = 0

repeat this procedure

#include<stdio.h>
void main()
{
 int a[]={1,7,5,9};
 int b[]={-2,-1,1,2};
 int c[]={-8,-7,1,-8};
 int i,j,k;
 int occupy[100];
 int count=0, flag=0,temp;
 for(i=0;i<4;i++)
  {
   for(j=0;j<4;j++)
    {
     for(k=0;k<4;k++)
      {
       if((a[i]+b[j]+c[k])==0)
{
flag=0;
temp=count;
while(temp>0)
{
 if((i==occupy[temp*3-3])||(j==occupy[3*temp-2])||(k==occupy[3*temp-1]))
  {
   flag=1;
   break;
  }
  temp--;
 }
 if(flag==0)
  {
   printf("%d %d %d \n",a[i],b[j],c[k]);
   count++;
   occupy[3*count-3]=i;
   occupy[3*count-2]=j;
   occupy[3*count-1]=k;
  }
}
      }
    }
  }
}

output
1 -2 1
7 1 -8
5 2 -7
9 -1 -8

Monday, June 6, 2016

c++ program to show operation on time, add seconds, add minutes, add hours, show time

#include<iostream.h>
#include<process.h>
class Time
{
 private:
  int hours;
  int minutes;
  int seconds;
 public:
  Time();
  void addhour(int);
  void addminute(int);
  void addsecond(int);
  void displaytime();
  void addtime();
  int gethour();
  int getminute();
  int getsecond();
  void sethour(int);
  void setminute(int);
  void setsecond(int);
};
Time::Time()
 {
  sethour(0);
  setminute(0);
  setsecond(0);
 }
void Time::addhour(int h)
 {
  h=gethour()+h;
  if(h>24)
   {
    h=h%24;
   }
  sethour(h);
 }
//function to get the value of hour
int Time::gethour()
 {
  return hours;
 }
//function to set the value of the hour
void Time::sethour(int h)
 {
  hours=h;
 }
//function to add the minutes in current minutes
void Time::addminute(int m)
 {
  m=getminute()+m;
  if(m>60)
   {
    m=m%60;
    addhour(1);
   }
  setminute(m);
 }
//function to get the value of current minutes
int Time::getminute()
 {
  return minutes;
 }
//function to set the value of current value
void Time::setminute(int m)
 {
  minutes=m;
 }
//function to add the seconds in current second value
void Time::addsecond(int s)
 {
  s=getsecond()+s;
  if(s>60)
   {
    s=s%60;
    addminute(1);
   }
  setsecond(s);
 }
//function to get the value of current second value
int Time::getsecond()
 {
  return seconds;
 }
//function to set the value of current second value
void Time::setsecond(int s)
 {
  seconds=s;
 }
void Time::displaytime()
 {
  cout<<"Time:: ";
  cout.width(2);
  cout.fill('0');
  cout<<gethour()<<":";
  cout.width(2);
  cout.fill(2);
  cout<<getminute()<<":";
  cout.width(2);
  cout.fill('0');
  cout<<getsecond();
 }
void Time::addtime()
 {
  int h,m,s;
  cout<<"Enter hours, minutes and seconds to add in time";
  cin>>h>>m>>s;
  addsecond(s);
  addminute(m);
  addhour(h);
 }
void main()
 {
  int i;
  Time t;
  int h;
  int m;
  int s;
  do
   {
    cout<<endl<<"Enter 1.Add hours \n2.Add minutes \n3.Add seconds \n4.Add time \n5.Show time \n6.Exit";
    cout<<endl<<"Enter your choice";
    cin>>i;
    switch(i)
     {
      case 1:
       cout<<endl<<"How many hours to add";
       cin>>h;
       t.addhour(h);
       break;
      case 2:
       cout<<endl<<"How many minutes to add";
       cin>>m;
       t.addminute(m);
       break;
      case 3:
       cout<<endl<<"How many seconds to add";
       cin>>s;
       t.addsecond(s);
       break;
      case 4:
       t.addtime();
       break;
      case 5:
       t.displaytime();
       break;
      case 6:
       exit(0);
      default:
       cout<<endl<<"Please enter a correct choice";
     }
   }while(i!=6);
 }

output
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
1
How many hours to add5
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
2
How many minutes to add25
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
5
Time::05:25:00
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
6