Lab 03
Lab 03
3. Write a program that will prompt the user for two integers a and b.
Then swap (interchange) the values of a and b. That means, a should
get the value of b and b should get the value of a.
Hint: You can use a temporary variable.
Solution:
#include <stdio.h>
int main()
{
int a = 5, b = 4;
printf("Before swapping:\n");
printf("\ta = %d",a);
printf("\n\tb = %d",b);
int temp;
temp = a;
a = b;
b = temp;
printf("\nAfter swapping:\n");
printf("\ta = %d",a);
printf("\n\tb = %d",b);
return 0; }
4. Write a program that will swap (interchange) the values of two
variables: a and b. That means, a should get the value of b and b
should get the value of a.
Note: You cannot use any temporary variable.
Solution:
#include<stdio.h>
#include<math.h>
int main()
{
int a = 5;
int b = 4;
printf("Before swapping:\n");
printf("\ta = %d",a);
printf("\n\tb = %d",b);
//Method 01:
//a = a+b;
//b = a-b;
//a = a-b;
//Method 02:
b = a^b;
a = b^a;
b = b^a;
printf("\nAfter swapping:\n");
printf("\ta = %d",a);
printf("\n\tb = %d",b);
return 0;
}
5. Write a C program to print the following Christmas tree:
Solution:
#include <stdio.h>
int main()
{
printf(" * \n");
printf(" *** \n");
printf(" ***** \n");
printf("*******\n");
printf(" | \n");
printf(" | \n");
return 0;
}