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