Pages

Find larger value between two variable

/* Find larger value between two variable */

#include<stdio.h>
int main()
{
    int a,b;
    printf("Enter value of a: ");
    scanf("%d",&a);
    printf("Enter value of b: ");
    scanf("%d",&b);   
   
    if(a>b)
        printf("A is larger");
    else if(b>a)
        printf("B is larger");
    else
        printf("A and B are equal");
    return 0;
}

Largest value of two variables - Conditional Operator

/*    Find largest value between two variables - single line - Conditional Operator  */

#include<stdio.h>
int main()
{
 int a,b,d;

 printf("Type values of A and B : ");
 scanf("%d %d",&a,&b);
 d=(a>=b?a:b);
 printf("Greatest value : %d",d);
 return 0;
}

Largest among 3 variables using nested if

/* Find largest among 3 variables using nested if */

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

Largest value among three variables

/* Largest value among three variables */

#include<stdio.h>
int main()
{
    int a,b,c;
    printf("Enter value of a: ");
    scanf("%d",&a);
    printf("Enter value of b: ");
    scanf("%d",&b);   
    printf("Enter value of c: ");
    scanf("%d",&c);       
   
    if(a >= b && a >= c)
        printf("%d is largest",a);
    else if(b >= a && b >= c)
        printf("%d is largest",b);
    else if(c >= a && c >= b)
        printf("%d is largest",c);
    return 0;
}

Find largest value among three variable - conditional operator

/*    Find largest value among three variable using conditional operator    */

#include<stdio.h>
int main()
{
 int a,b,c,d;

 printf("Type values of A,B and C : ");
 scanf("%d %d %d",&a,&b,&c);
 d=(a>=b?a>=c?a:c:b>=c?b:c);
 printf("Greatest value : %d",d);
 return 0;
}

Leap Year

/* Check Leap Year */
#include<stdio.h>
int main()
{
    int y;
    printf("Enter a year: ");
    scanf("%d",&y);
    if(y%4==0)
        printf("Leap Year");
    else
        printf("Not a Leap Year");
   return 0;
}

Even Odd Number


/* Check a number is even or odd */

#include<stdio.h>
int main()
{
    int a;
    printf("Enter a number: ");
    scanf("%d",&a);
    if(a%2==0)
        printf("Even Number");
    else
        printf("Odd Number");
   return 0;
}

Convert Km to Meter, Centimeter or Millimeter

/*    Convert Km to Meter, Centimeter or Millimeter    */
#include<stdio.h>
int main()
{
 float km;
 char v;
 float ans;

 printf("K.M. : ");
 scanf("%f",&km);
 printf("KM will be changed in (M,s,m) : ");
 fflush(stdin);
 scanf("%c",&v);
 if(v=='M')
  ans=km*1000;
 if(v=='s')
  ans=km*100000;
 if(v=='m')
  ans=km*1000000;
 printf("Answer : %.2f",ans);
 return 0;
}

Swap values of two Variables

/*    Swap the values of two Variables using three variables    */
#include<stdio.h>
int main()
{
 int a;
 int b;
 int c;

 printf("Type value of A : ");
 scanf("%i",&a);
 printf("\nType value of b : ");
 scanf("%i",&b);
 c=a;
 a=b;
 b=c;
 printf("A : %i",a);
 printf("\nb : %i",b);
 return 0;
}

Convert meter to feet

/* convert meter to feet */

#include<stdio.h>
int main()
{
    float m,f;
    printf("Type meter : ");
    scanf("%f",&m);
    f = 3.2808399 * m;
    printf("feets: %f",f);
    return 0;
}

Convert celcius to farenheit

/* convert celcius to farenheit */

#include<stdio.h>
int main()
{
    float c,f;
    printf("Type celcius: ");
    scanf("%f",&c);
    f = c * 9/5 + 32;
    printf("farenheit: %f",f);
    return 0;
}

Sum of two numbers

/*    Sum of two Variable    */
#include<stdio.h>
 int main()
 {
  int var2;
  int var1;
  int var3;

  printf("Type your frist value to add : ");
   scanf("%i",&var1);
  printf("Type your second value to add : ");
   scanf("%i",&var2);
  var3 =var2+var1;
   printf("Total of your value =  %i",var3);
  return 0;
 }

Calculate Simple Interest

/*    Calculate Simple interest    */
#include<stdio.h>
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;
}

Convert farenheit to celcius

/* convert farenheit to celcius */

#include<stdio.h>
int main()
{
    float c,f;
    printf("Type farenheit : ");
    scanf("%f",&f);
    c = (f - 32) * 5/9;
    printf("celcius: %f",c);
    return 0;
}

Convert feet to meter

/* convert feet to meter */

#include<stdio.h>
int main()
{
    float m,f;
    printf("Type feet : ");
    scanf("%f",&f);
    m = f / 3.2808399;
    printf("meter: %f",m);
    return 0;
}

Swap two values without using third variable

/*    Swap the values of two variables without using third variable   */

#include<stdio.h>

int main()
{
 int a;
 int b;
 printf("Type value of A : ");
 scanf("%i",&a);
 printf("\nType value of b : ");
 scanf("%i",&b);
  a = a + b;
  b = a - b;
  a = a - b ;

 printf("A : %i",a);
 printf("\nB : %i",b);
 return 0;
}

Swap values of two variables using XOR

/* Swap values of two variables using XOR */

#include<stdio.h>

int main()
{
 int a;
 int b;
 printf("Type value of A : ");
 scanf("%i",&a);
 printf("\nType value of b : ");
 scanf("%i",&b);
  a ^= b;
  b ^= a;
  a ^= b;

 printf("A : %i",a);
 printf("\nB : %i",b);
 return 0;
}