Monday, 20 March 2017

C in size operator

Program :

#include
void main()
{
  printf("\n size of int is: %d",sizeof(int));
  printf("\n size of float is: %d",sizeof(float));
  printf("\n size of double is: %d",sizeof(double));
  printf("\n size of char is: %d",sizeof(char));

}

Note: In borland C or Turbo C compilers size of int is 2.

Output :


Sum of Series 1+2+3+..+n

Program :

#include
void main()
{
  int sum=0,i,n;
  printf("\n>>> C Program to print Sum of Series 1+2+3+..+n <<<\n");
  printf("\n Enter the number of terms:");
  scanf("%d",&n);
  for(i=1;i<=n;i++)
  {
      sum+=i;
  }
  printf("\n Sum of series upto %d is :%d \n\n",n,sum);

}


Output :




C program the division operation

Program :

#include

#include
int main()
{
     int x,y;
     int result1,result2;
     printf("\n Enter the dividend : ");
     scanf("%d",&x);
     printf("\n Enter the divisor: ");
     scanf("%d",&y);
     result1 = x/y;          
     result2 = x%y;        
     printf("\n The Quotient is: %d ",result1);
     printf("\n The Remainder is: %d ",result2);
     getch();
     return 0;

}


Output ;




Calculate Simple interest

/*    Calculate Simple interest    */

#include

int main()
{
 float p;
 float ir,t;
 float in;

  printf("Type your principle amount : ");
  scanf("%f",&p);

  printf("\n Type your rate of intrest : ");
  scanf("%f",&ir);

  printf("\n Type time (in year) : ");
  scanf("%f",&t);

  in=p*ir*t/100;

  printf("\n You will get intrest : %.2f",in);
  return 0;
}

Output:



Swap the values of two Variables using three variables

/*    Swap the values of two Variables using three variables    */

#include
int main()
{
 int p;
 int q;
 int r;
 clrscr();

 printf("Enter your value of P : ");
 scanf("%i",&p);

 printf("\n Enter your value of Q : ");
 scanf("%i",&q);

r=p;
p=q;
q=r;

 printf("P : %i",p);
 printf("\nQ : %i",q);

 getch();
 return 0;
}

Output:



Simple if statement

Syantax:

if(condition)
{
 statement1;
 statement2;
 . 
 .
 .
 statement n;

}

Program :

#include
void main()
{
    int age;
    printf(" >>> c program to implement simple if<<< \n\n");
    printf("\n Enter the age of the person: ");
    scanf("%d",&age);
    if(age>=18)
    {
        printf("\n Eligible for Licence \n");
    }
    printf("\n program ends \n\n");
}


Output:






C program to sort an array using function

Program :

#include
#include
#define MAX 100     // maximum no of elements of array
int sortArray(int);
int array[MAX];
int main()
{
  int i,size;
  printf("\n>>>> PROGRAM TO SORT ARRAY USING FUNCTION <<<<\n\n");
  printf("\n Enter the size of array: ");
  scanf("%d",&size);
  printf("\n Enter the %d elements of array: \n",size);
  for(i=0;i
  {
   printf("\n array[%d]=",i);
   scanf("%d", &array[i]);
  }
  sortArray(size);    //calling sortArray() function
   printf("\n The Sorted elements of array are:");
   for(i=0;i
   {
     printf(" %d",array[i]);
   }
   getch();
   return 0;
}

sortArray(n)   // function for sorting array elements

{
  int temp=0,i,j;   // temp var is temporary variable used for swapping
  for(i=0;i
  {
    for(j=i+1;j
    {
      if(array[i]>array[j])
      {
        temp=array[i];   //swapping for the array to be sorted
        array[i]= array[j];
        array[j]=temp;
      }
    }
  }

}


Output :