Monday, 1 October 2012

Simple C Programs

Program 1- Write a program to exchange the value of two values in C.
#include<stdio.h>
main()
{
     int a,b,t;
     printf("Enter the two values");
     scanf("%d %d",&a,&b);
     t=a;
     a=b;
     b=t;
     printf("The values of a and b are \n");
     printf("%d %d", a,b);
}
-----------------------------------------------
Output
-----------------------------------------------
Enter the two values 12 13

The values of a and b are 13 12
------------------------------------------------


Program 1- Write a program to exchange the value of two values in C with the help of only two variable .
#include<stdio.h>
main()
{
     int a,b;
     printf("Enter the two values");
     scanf("%d %d",&a,&b);
     printf("The values of a and b are \n");
     printf("a = %d",a);
     printf("b = %d",b);
     a=a+b;
     b=a-b;
     a=a-b;
     printf("After swapping the values of a and b are \n");
     printf("a = %d",a);
     printf("b = %d",b);
}
-----------------------------------------------
Output
-----------------------------------------------
Enter the two values 12 13

The values of a and b are
a = 12
b = 13

After swapping the values of a and b are
a = 13
b = 12
------------------------------------------------