You are on page 1of 18

1) WAP to enter two integers and swap their contents using

third variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp=0;
clrscr();
printf("Enter the numbers:");
scanf("%d %d", &a,&b);
printf("The entered numbers are: %d & %d\n", a,b);
temp=a;
a=b;
b=temp;
printf("The swaped values are: %d & %d", a,b);
getch();
}
2) Write the above program using arithmetic operator.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp=0;
clrscr();
printf("Enter the numbers:");
scanf("%d %d", &a,&b);
printf("The entered numbers are: %d & %d\n", a,b);
a=a*b;
b=a/b;
a=a/b;
printf("The swaped values are: %d & %d", a,b);
getch();
}
3) Write the above program using bitwise XOR operator.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the numbers:");
scanf("%d %d", &a,&b);
printf("The entered numbers are: %d & %d\n", a,b);
a=a^b;
b=a^b;
a=a^b;
printf("The swaped values are: %d & %d", a,b);
getch();
}
4) WAP to calculate sum of 5 digit no. entered through
keyboard.

#include<stdio.h>
#include<conio.h>
void main()
{
int num,r1,r2,r3,r4,r5,sum;
clrscr();
printf("Enter the number:");
scanf("%d", &num);
r1=num%10;
num=num/10;
r2=num%10;
num=num/10;
r3=num%10;
num=num/10;
r4=num%10;
r5=num/10;
sum=r1+r2+r3+r4+r5;
printf("Sum of digits of entered number is: %d");
getch();
}
5) Write the above program using while loop.

#include<stdio.h>
#include<conio.h>
void main()
{
int num,r,sum=0;
clrscr();
printf("Enter the number:");
scanf("%d", &num);
while(num>0)
{
r=num%10;
sum=sum+r;
num=num/10;
}
printf("Sum of digits of entered number is: %d",sum);
getch();
}
6) WAP to reverse a 4 digit no entered through keyboard w/o
using any loop.

#include<stdio.h>
#include<conio.h>
void main()
{
int num,rev=0,r;
clrscr();
printf("Enter a four digit number:");
scanf("%d", &num);
r=num%10;
rev=r*1000+rev;
num=num/10;
r=num%10;
rev=r*100+rev;
num=num/10;
r=num%10;
rev=r*10+rev;
num=num/10;
r=num%10;
rev=r*1+rev;
printf("Reverse of entered number is: %d",rev);
getch();
}
7) Write the above program using while loop.

#include<stdio.h>
#include<conio.h>
void main()
{
int num,r,rev=0;
clrscr();
printf("Enter a four digit number:");
scanf("%d", &num);
while(num>0)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
printf("Reverse of entered number is: %d", rev);
getch();
}
8) WAP to check whether a year inputted from keyboard is leap
year or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int year,n;
clrscr();
printf("Enter the year:");
scanf("%d", &year);
if(year!=0)
{
n=year%100;
if((n%4)==0)
printf("Entered year is Leap.\n");
else
printf("Entered year is not leap.");
}
getch();
}
9) Write the above program using ternary operator.

#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter the year:");
scanf("%d", &year);
(year%4==0)&&(year%100!=0)||(year%400==0)?
printf("Leap"):printf("Not leap");
getch();
}
10) WAP to check whether an integer inputted from keyboard is
even or odd.

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the number:");
scanf("%d", &a);
if((a%2)==0)
printf("Even");
else
printf("Odd");
getch();
}
11) Write the above program ternary operator.

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the number:");
scanf("%d", &a);
(a%2)==0?printf("Even"):printf("Odd");
getch();
}
12) WAP to find greatest of the three integers entered through
keyboard using if-else.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the numbers:");
scanf("%d%d%d", &a,&b,&c);
if(a>b&&a>c)
printf("The greatest number is: %d",a);
else if(b>a&&b>c)
printf("The greatest number is: %d",b);
else
printf("The greatest number is: %d",c);
getch();
}
13) Write the above using ternary operator.

#include<stdio.h>
#include<conio.h>
void main()
{
int greatest,a,b,c;
clrscr();
printf("Enter the numbers:");
scanf("%d%d%d", &a,&b,&c);
greatest=a>b&&a>c?a:b>c?b:c;
printf("The greatest number is: %d",greatest);
getch();
}
14) WAP to print all ASCII values with their equivalent
characters using a while loop.

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
printf("\n");
while(i<256)
{
printf(" %3d:'%c' ",i,i);
i++;
}
getch();
}
15) WAP to print all the Armstrong numbers from 1 to 999.

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, num, i=1;
clrscr();
printf("Printing all the armstrong numbers between 1 - 999");
while(i<=999)
{
a= i/100;
a= a*a*a;
num= i%100;
b= num/10;
b= b*b*b;
c= num%10;
c=c*c*c;
if(i==a+b+c)
{
printf("\n Armstrong number= %d", i);
}
i++;
}
getch();
}
16) WAP to check whether entered number is prime or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int no,i;
clrscr();
printf("Enter the number:");
scanf("%d", &no);
i=2;
while(i<=no-1)
{
if(no%i==0)
{
printf("Entered no is not prime.");
break;
}
i=i++;
}
if(no==i)
printf("Entered number is prime.");
getch();
}
17) WAP to print the Fibonacci series, limit to be given by user.

#include<stdio.h>
void main()
{
int i=0,j=1,sum=0,num;
clrscr();
printf("Enter the limit of the series:");
scanf("%d", &num);
while(sum<num)
{
printf("%d ", sum);
i=j;
j=sum;
sum=i+j;
}
getch();
}
18) WAP to convert decimal number to its corresponding binary
equivalent.

#include<stdio.h>
#include<conio.h>
void main()
{
int dec,rem,i=1;
long bin=0;
clrscr();
printf("Enter the decimal number:");
scanf("%d",&dec);
while(dec>0)
{
rem=dec%2;
dec=dec/2;
bin=bin+(i*rem);
i=i*10;
}
printf("The binary equivalent is %d",bin);
getch();
}

You might also like