c program to find the greatest between two number


To compare between two number follow the logic
e.g- if a>b is true then a is greater else b is greater.

using if else

#include <stdio.h>
int main()
{
 int a,b;
 printf("Enter two numbers\n");
 scanf("%d%d",&a,&b);
 if(a>b)
 printf("%d is greatest\n",a);
 else
 printf("%d is greatest\n",b);
 return 0;
}

OUTPUT:
Enter two numbers
11
22
22 is greatest


using conditional operator

#include <stdio.h>
int main()
{
 int a,b;
 printf("Enter two numbers\n");
 scanf("%d%d",&a,&b);
 (a>b)?printf("%d is greatest\n",a):printf("%d is greatest\n",b);
 return 0;

}

OUTPUT:
Enter two numbers
47
24

47 is greatest

c program to find the greatest between three number

To find the greatest between three number follow the logic 
consider three numbers a,b,c.
step-1: first compare a with b.
step-2: if step-1 is true compare a with c
step-3: if step-2 is true, print a is greater or if step-2 false print c is greater.
step-4: if step-1 is false, compare b with c.
step-5: if step-4 is true, print b is greater or if step-4 is false print c is greater.  

using if else

#include <stdio.h>
int main()
{
 int a,b,c;
 printf("Enter three numbers\n");
 scanf("%d%d%d",&a,&b,&c);
 if(a>b)
 {
  if(a>c)
  printf("%d is greatest\n",a);
  else
  printf("%d s greatest\n",c);
 }
 else
 {
  if(b>c)
  printf("%d is greatest\n",b);
  else
  printf("%d is greatest\n",c);
 }
return 0;
}

OUTPUT:
Enter three numbers
15
18
17
18 is greatest

using conditional operator

#include <stdio.h>
int main()
{
 int a,b,c;
 printf("Enter three numbers\n");
 scanf("%d%d%d",&a,&b,&c);
 (a>b)?
 ((a>c)?printf("%d is greatest\n",a):printf("%d s greatest\n",c)):
 ((b>c)?printf("%d is greatest\n",b):printf("%d is greatest\n",c));
return 0;
}

OUTPUT:
Enter three numbers
24
32
49
49 is greatest

c program to find the greatest between four number

To find the greatest between four numbers follow the logic
consider four numbers a,b,c,d.
step-1: first compare a with b(a>b).
step-2: if step-1 is true compare a with c(a>c).
step-3: if step-2 is true compare a with d(a>d).
step-4: if step-3 is true, print a is greater or if step-3 false print d is greater.
step-5: if step-2 is false, compare c with d.
step-6: if step-5 is true, print c is greater or if step-5 is false print d is greater. 
step-7: if step-1 is false compare b with c(b>c)
step-8: if step-7 is true compare b with d(b>d).
step-9: if step-8 is true, print b is greater or if step-8 false print d is greater.
step-10: if step-7 is false compare c with d(c>d).
step-11: if step-10 is true, print c is greater or if step-10 false print d is greater.

using if else

#include <stdio.h>
int main()
{
int a,b,c,d;
printf("Enter four number\n");
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a>b)
   {
     if(a>c)
        {
           if(a>d)
            printf("%d is greatest\n",a);
           else
            printf("%d is greatest\n",d);
        }

      else
        {
          if(c>d)
          printf("%d is greatest\n",c);
          else
printf("%d is greatest\n",d);
 }
   }
else
   {
     if(b>c)
        {
           if(d>b)
            printf("%d is greatest\n",d);
           else
            printf("%d is greatest\n",b);
        }

      else
        {
          if(c>d)
          printf("%d is greatest\n",c);
          else
          printf("%d is greatest\n",d);
         }
   }
return 0;
}

OUTPUT:
Enter four number
14
78
25
34
78 s greatest

using conditional operator

#include <stdio.h>
int main()
{
int a,b,c,d;
printf("Enter four number\n");
scanf("%d%d%d%d",&a,&b,&c,&d);
(a>b)?
   (
    (a>c)?((a>d)?printf("%d is greatest\n",a):printf("%d is greatest\n",d)):
     ((c>d)?printf("%d is greatest\n",c):printf("%d is greatest\n",d))
   ):
   ((b>c)?((d>b)?printf("%d is greatest\n",d):printf("%d is greatest\n",b)):
    ((c>d)?printf("%d is greatest\n",c):printf("%d is greatest\n",d))
   );      
return 0;
}

OUTPUT:
Enter four number
41
65
52
34
65 is greatest



Swapping of two numbers using c language


              To interchange, the value of two variables is known as swapping.
              Example:- Before execution a=2, b=3
                                After execution a=3, b=2

     #include <stdio.h>
  int main()
   {
    int a,b,c;
    printf("Enter value for a\n");
    scanf("%d",&a);
    printf("Enter value for b\n");
    scanf("%d",&b);
    c=a;
    a=b;
    b=c;
    printf("After swapping a=%d b=%d",a,b);
    return 0;   
   }


output: Enter value for a
               10
               Enter value for b
               20
               After swapping a=22 b=11

  swapping of two number without using third variable

      on the above example, we need a third variable c to store the value temporarily but here the following c program only need two variable to swap the values.

   #include <stdio.h>
   int main()
    {
     int a,b;
     printf("Enter value for a\n");
     scanf("%d",&a);
     printf("Enter value for b\n");
     scanf("%d",&b);
     a=a+b;
     b=a-b;
     a=a-b;
     printf("After swapping a=%d b=%d",a,b);
    }

output: Enter value for a
               11
               Enter value for b
               22
               After swapping a=22 b=11




How to find the ASCII value of any character


        ASCII-American standard code for information interchange.

   ASCII table maintains a unique seven-bit number for each character using which the computer understands which key is pressed, following program will help to find the ASCII value for the entered character.


  #include <stdio.h>
  int main()
  {
   char c;
   printf("Enter a character\n");
   scanf("%c",&c);
   printf("ASCII value of '%c' is %d\n",c,c);
   return 0;
  }

output:-
   Enter a character
   s  
   ASCII value of 's' is 115

program to find the character from its ASCII value


   #include <stdio.h>
   int main()
   {
    int a;
    printf("Enter the ASCII value (in decimal)\n");
    scanf("%d",&a);
    printf("character for ASCII %d is %c\n",a,a);
    return 0;
   }

output:-
  Enter the ASCII value (in decimal)
  65
  character for ASCII 65 is A


C Program To Calculate Average Of four Integer




 it is a single number that is used to represent a collection of numbers.                    

Average =Sum/Count 

#include<stdio.h>
int main()
{
int a,b,c,d;
float avg;
printf("Enter four numbers:\n");
scanf("%d%d%d%d",&a,&b,&c,&d);
avg=(a+b+c+d)/4.0;                 //4.0 is for type casting
printf("Average=%f\n",avg);
}

OUTPUT:
Enter four numbers:
25
65
45
88
Average=55.750000



Program to calculate area and perimeter of rectangle, sqare and circle using C language

 
         
       
Shape
Area
Perimeter
Rectangle
Length x width
2*(length+width)
Square
Side x side
Side x 4
Circle
πr2
2πr


#include <stdio.h>
int main()
{
int length,width,side;
float radius,pi=3.14;
int a_rect,p_rect,a_sq,p_sq;
float a_cir,p_cir;

printf("Enter length and width for rectangle\n");
scanf("%d%d",&length,&width);
printf("Enter sides of the square\n");
scanf("%d",&side);
printf("Enter radius of the circle\n");
scanf("%f",&radius);
a_rect=length*width;
p_rect=2*(length+width);
a_sq=side*side;
p_sq=4*side;
a_cir=pi*radius*radius;
p_cir=2*pi*radius;
printf("For rectangle:Area=%d, Perimeter.=%d\n",a_rect,p_rect);
printf("For square:Area=%d, Perimeter=%d\n",a_sq,p_sq);
printf("For circle:Area=%f, Perimeter=%f\n",a_cir,p_cir);
}

OUTPUT:
Enter length and width for rectangle
18 15
Enter sides of the square
12
Enter a radius of the circle
6.5
For rectangle:Area=270, Perimeter.=66
For square:Area=144, Perimeter=48

For circle:Area=132.664993, Perimeter=40.820000



C program to calculate simple interest


       Simple interest is a method of calculating interest charged on fixed deposit
    Formula  i=(Ptr/100)
    Where i:Interese
               p:Principal
               t:time period
               r:rate of interest/year

    #include <stdio.h>
    int main()
    {
    int principal,time;
    float r,i;
    printf("input principal amount\n");
    scanf("%d",&principal);
    printf("input time (in year)\n");
    scanf("%d",&time);
    printf("input the rate of interest\n");
    scanf("%f",&r);
    i=(principal*time*r)/100;
    printf("Total interest %f\n",i);
    }

  OUTPUT:
     input principal amount
    10000
     input time (in year)
     4
     input the rate of interest
     7.5
     total interest 3000.000000