DO NOT MISS

Ads

Tuesday 12 July 2016

C-Programs Using Assignment Operators


* Write a Program to swap two numbers


#include<stdio.h>
#include<conio.h>

void main()

{

         int a,b,t;
         clrscr();

         
printf("enter the a b values:");
         scanf("%d%d",&a,&b);

         printf("\n before swapping a,b values:%d and %d",a,b);

         t=a;
         a=b;
         b=t;

         printf("\n after the swapping a,b values:%d and %d",a,b);

         getch();

}
Output:-

enter the a b values:
4
7


before swapping a,b values: 4,7
after swapping a,b values : 7,4


* Write a Program to swap two numbers by using only two variables.


#include<stdio.h>
#include<conio.h>

void main()

{

      int a,b;
      clrscr();

     
printf("enter the a b values:");
      scanf("%d%d",&a,&b);

      printf("\n before swapping a,b values::%d and %d",a,b);

      a=a+b;
      b=a-b;
      a=a-b;

      printf("\n after the swapping a,b values:%d and %d",a,b);

      getch(); 

}
                                                                                                          
Output:-

enter the a b values:
71
38


before swapping a,b values:
71,38
after swapping a,b values : 38,71


* Write a Program to swap three numbers.



#include<stdio.h> 
#include<conio.h>

void main()

{

          int a,b,c,t;
          clrscr();

         
printf("enter the a b c values :");
          scanf("%d%d%d",&a,&b,&c);

          printf("\n before the swapping a,b,c values:%d ,%d,%d",a,b,c);

          t=a;
          a=b;
          b=c;
          c=t;

          printf("\n after the swapping a,b,c values:%d ,%d,%d",a,b,c);
          getch();

}  
                                                                                     
Output:-


enter a,b,c values:
3
4
5


before swapping a,b,c values: 3,4,5
after swapping a,b,c values: 4,5,3      
     

* Write a Program to swap three numbers by using only three variables.

#include<stdio.h> 
#include<conio.h>

void main()

{

      int a,b,c;
      clrscr();

     
printf("enter the a b c values :");
      scanf("%d%d%d",&a,&b,&c);

      printf("\n before the swapping a,b,c values:%d ,%d,%d",a,b,c);

      a=a+b+c;
      b=a-b-c;
      c=a-b-c;
      a=a-b-c;

      printf("\n after the swapping a,b,c values:%d ,%d,%d",a,b,c);

      getch();

}
                                                                                   
Output:-


enter a,b,c values:
23
56
35


before swapping a,b,c values: 23,56,35
after swapping a,b,c values: 56,35,23  
         
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________

Greataims

 
Copyright © 2016 Computersadda. Designed by @ Computersadda Team - Published By Greataims