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

Saturday, June 4, 2016

C program to check given number in strong number or not

#include<stdio.h>
//function to calculate factorial
int fact(int i)
 {
  int k=1;
  if(i==1)
   {
    return 1;
   }
  else
   {
    k=i*fact(i-1);
   }
   return k;
 }
void main()
 {
  int n,m,sum=0,a;
  int i;
  printf("Enter a no");
  scanf("%d",&n);
  m=n;
  while(n>0)
   {
    a=n%10;
    sum=sum+fact(a);
    n=n/10;
   }
  if(m==sum)
   {
    printf("No is strong no");
   }
  else
   {
    printf("Not a strong no");
   }
}

output
Enter a no 32
Not a strong no

Thursday, June 2, 2016

c program to count number of 1 in binary representation of a number

Problem Statement: count the no of ones in binary repesentation of a decimal number
#include<stdio.h>
#include<math.h>
void main()
{
 int i,k,l,j=0;
 printf("\nEnter a no");
 scanf("%d",&i);
 l=i;
//it will give the size of any data type in bits
 k=8*sizeof(int);
 while(k!=0)
  {
   if(i&1==1)
    {
     j++;
    }
   i=i>>1;
   k--;
  }
 printf("\nNo of one set in number is %d",j);
}

output
Enter a no10
No of one set in number is 2

Tuesday, May 31, 2016

C program to print binary representation of number

#include<stdio.h>
#include<math.h>
//function to print binary number of a decimal
void binary(int i)
{
 if(i!=0)
  {
   binary(i>>1);
  }
 printf("%c",i&1==1?'1':'0');
}
void main()
{
 int i,k,l,j=0;
 printf("\nEnter a no");
 scanf("%d",&i);
 l=i;
 //To print binary equivalent
 printf("\nBinary representation of the no is ");
 binary(l);
}

output
Enter a no 54
Binary representation of the no is 110110

Sunday, May 29, 2016

C program to sort list of names

#include<stdio.h>
#include<string.h>
void main()
{
 int i,j;
//list of names to be sort, you can customize to take input from keyboard
 char *list[]={"aj","su","punit","pushp"},*s;
printf("Input List is :")
 for(i=0;i<4;i++)
 {
  printf("\n%s ",*(list+i));
 }
 for(i=0;i<4;i++)
  {
   for(j=0;j<4-i-1;j++)
    {
     if(strcmp(*(list+j),*(list+j+1))>0)
      {
       s=*(list+j);
       *(list+j)=*(list+j+1);
       *(list+j+1)=s;
      }
    }
   }
 printf("\nSorted List is:\t");
 for(i=0;i<4;i++)
  {
   printf(" %s",*(list+i));
  }
}

output
Input List is:
aj
su
punit
pushp
Sorted List is: aj punit pushp su

Friday, May 27, 2016

C program to make a binary tree and print the tree in inorder, preorder and postorder form

#include<stdio.h>
#include<stdlib.h>
struct node
{
 int info;
 struct node *left,*right;
}*root;
//function to create a new node in tree
node *create(struct node *ptr,int n)
 {
  struct node *curr;
  curr=(node *)malloc(sizeof(node));
  if(ptr==NULL)
   {
    curr->info=n;
    curr->left=NULL;
    curr->right=NULL;
    ptr=curr;
   }
  else
   {
    if(ptr->info>n)
     ptr->left=create(ptr->left,n);
    else
     ptr->right=create(ptr->right,n);
   }
   return ptr;
 }
void preorder(struct node *ptr)
 {
  if(ptr!=NULL)
   {
    printf(" %d",ptr->info);
    preorder(ptr->left);
    preorder(ptr->right);
   }
 }
void inorder(struct node *ptr)
{
 if(ptr!=NULL)
  {
   inorder(ptr->left);
   printf(" %d",ptr->info);
   inorder(ptr->right);
  }
}
void postorder(struct node *ptr)
 {
  if(ptr!=NULL)
   {
    postorder(ptr->left);
    postorder(ptr->right);
    printf(" %d",ptr->info);
   }
 }
void main()
{
//initially root will be null as there is no node
 root=NULL;
 int n;
 int i;
 do
  {
   printf("\n\n1.insert\n\n2.preorder.\n\n3.inorder\n\n4.postorder\n\n5.exit");
   scanf("%d",&n);
   if(n==1)
    {
     printf("Enter value to insert");
     scanf("%d",&i);
     root=create(root,i);
    }
   else if(n==2)
    {
     preorder(root);
    }
   else if(n==3)
    {
     inorder(root);
    }
   else if(n==4)
    {
     postorder(root);
    }
   }while(n!=5);
 }

output

1.insert
2.preorder
3.inorder
4.postorder
5.exit
1
Enter value to insert 4

1.insert
2.preorder
3.inorder
4.postorder
5.exit
1
Enter value to insert 3
1.insert
2.preorder
3.inorder
4.postorder
5.exit
2
3 4
1.insert
2.preorder
3.inorder
4.postorder
5.exit
5

Tuesday, May 24, 2016

C program to find whether given string or sentence is armstrong or not

#include<stdio.h>
#include<string.h>
#include<math.h>
void main()
{
//str1 is input string here you can input here using your choice
 char str1[50]="hey its its hey",str2[50]="",strtemp[50];
 printf("Input string is %s",str1)
 strlwr(str1);
 int s[50],l[50],c=0;
 for(int i=0;i<=strlen(str1);i++)
  {
   if(str1[i]>=97&&str1[i]<=122)
    continue;
   else
    {
     if(c==0)
      {
       s[c]=0;
       l[c]=i-1;
       c++;
      }
     else
      {
       s[c]=l[c-1]+2;
       l[c]=i-1;
       c++;
      }
    }
  }
 c=c-1;
 for(i=c;i>=0;i--)
  {
   int temp=0;
   if((i!=c))
    {
     strtemp[temp]=' ';
     temp++;
    }
   for(int j=s[i];j<=l[i];j++)
    {
     strtemp[temp]=str1[j];
     temp++;
    }
   strtemp[temp]='\0';
   strcat(str2,strtemp);
  }
 printf("%s",str2);
 if((strcmp(str1,str2)==0))
  {
   printf("\nString are armstrong");
  }
 else
  {
   printf("\nString is not armstrong");
  }
}

output
Input string is hey it's it's hey
String are armstrong

Friday, May 20, 2016

C program to find whether given string is palindrome or not

Problem Statement:- you are given a string and you need to check whether the given string is palindrome or not, if yes you need to produce output yes
Palindrome:- if the string is equal to its reverse of string then it is called palindrome numbers

#include<stdio.h>
#include<string.h>
void check(char *t)
 {
  char *i;
//strlen() is used to calculate length of the string defined in string.h
  int l=strlen(t);
  while(*t!=NULL)
   {
    *i=*t;
    i++;
    t++;
   }
   *i='\0';
   t=t-l;
   i--;
   int count=0;
   while(*t!=NULL)
    {
     if(*t!=*i)
      {
       count++;
       break;
      }
      t++;
      i--;
    }
  if((count==0)&&(*t==NULL))
   {
    printf("String is palindrome");
   }
 else
  {
   printf("String is not palindrome");
  }
}

void main()
{
 char s[20];
 printf("Enter a string");
 scanf("%s",s);
 check(s);
}

output
Enter a string
swswwsws
String is palindrome

Wednesday, May 18, 2016

c program to sort a given string alphabetically

Problem Statement:- you are given a string and you need to sort that string into their alphabetical order. Like in "enter" the first alphabet also comes first alphabetically so it will stay there and after that the secode 'e' must come.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
 //random string as input
 char s[15]="enter";
 int i;
 char temp;
 printf("String entered is %s ",s);
//applying logic as bubble sort for string sorting
 for(i=0;i<strlen(s);i++)
  {
   for(int k=0;k<strlen(s)-i-1;k++)
    {
     //this  will use ASCII codes for characters
     if(s[k]>s[k+1])
     {
      temp=s[k];
      s[k]=s[k+1];
      s[k+1]=temp;
     }
    }
   }
 printf("\nString is %s",s);
}

output
String entered is  enter
String is eenrt

Monday, May 16, 2016

c program to print box using stars

Problem Statement:- print square box using stars in c, you can use nested loops to solve this problem.

#include<stdio.h>
void main()
 {
  int a,n=6,i,j,k;
  for(i=1;i<=n-2;i++)
   {
    if(i==1||i==(n-2))
     {
      for(j=1;j<=n;j++)
       printf("*");
      printf("\n");
     }
    else
     {
      printf("*");
      for(j=1;j<=n-2;j++)
       printf(" ");
      printf("*");
      printf("\n");
     }
   }
}

output
******
*        *
*        *
******

Saturday, May 14, 2016

C program to print stars in diamond form

#include<stdio.h>
#include<conio.h>
void main()
 {
  clrscr();
  int a,n=4,i,j,k;
  for(i=1;i<=n;i++)
   {
    for(k=1;k<=n-i;k++)
     printf(" ");
    for(j=1;j<=2*i-1;j++)
     printf("*");
     printf("\n");
   }
   for(i=n;i>=1;i--)
   {
    for(j=1;j<=n-i;j++)
     printf(" ");
    for(k=1;k<=2*i-1;k++)
     printf("*");
     printf("\n");
   }
 getch();
}

output
      *
    ***
  *****
*******
*******
  *****
   ***
     *

Thursday, May 12, 2016

C program to find factorial of a numbe using recursive technique

Problem Statement: factorial of any number is equal to multiplication of all the numbers from 1 to that number. Factorial of 5 will be equal to multiplication of numbers from 1 to 5 which will be 1*2*3*4*5 = 120
Recursive Technique: If a function calls itself then it is called recursive function.

#include<stdio.h>
//recursive function for calculating factorial
int fact(int i)
 {
  int k=1;
  if(i<=1)
   {
    return 1;
   }
  else
   {
    k=i*fact(i-1);
   }
   return k;
 }
void main()
 {
  int n,factorial=1;
  printf("Enter a no");
  scanf("%d",&n);
  factorial=fact(n);
  printf("Factorial = %d",factorial);
}

output
Enter a no 5
Factorial = 120

Tuesday, May 10, 2016

C++ program to explain default, parameterized and copy constructor

#include<iostream.h>
class vector
{
 public:
  int *a;
  int b;
//default constructor
 vector()
  {
   a = new int;
   *a=10;
  }
//parameterized constructor
 vector(int k)
  {
   a = new int;
   *a=k;
  }
 void getvalue()
  {
   cout<<"a="<<*a<<"\n";
  }
//copy constructor
  vector(vector &v)
  {
  a = new int;
  *a=*v.a;
  }
};
void main()
{
 vector vector1; //invoke default constructor
 vector1.getvalue();
 vector vector2(25); //invoke parameterized constructor
 vector.getvalue();
 vector vector3;
 vector vector4(vector3); //invoke copy constructor
 vector4.getvalue();
}
output
a=10
a=25
a=10

Monday, May 9, 2016

c++ program to swap two numbers using third variable

problem statement:- swapping of two numbers means you are given two numbers in variables a and b respectively and with the use program you have to swap value of a to b and value of b to a

#include<iostream.h>
class a
{
public:
int a,b,c;
void swap();
};
void a::swap()
{

c=a;
a=b;
b=c;
cout<<"\n after swapping\n";
cout<<a<<" "<<b;
}
void main()
{
 a obj;
 cout<<"enter two nos.\n";
 cin>>obj.a>>obj.b;
 cout<<"before swapping\n";
 cout<<obj.a<<" "<<obj.b;
 obj.swap();
 }

output
enter two nos
13 18
before swapping
13 18
after swapping
18 13