You are on page 1of 20

- OUTPUT WRONG

- SAME CONCEPT AS YELLOW

- SAMAJH NHI AYA

INDEX
Serial no. Experiment
1 WAP to input two numbers and swap their values
without using a third variable.
2 WAP to input two numbers and find which one is
greater using ternary operator or conditional operator.
3 WAP to print your name without using semicolon.
4 WAP to input any number and check whether it is even
or odd.
5 WAP to input any year and check whether it is leap
year or not.
6 WAP to input three numbers and find the greatest
number.
7 WAP to input marks of 5 subjects and find the average
of five subjects and print a message according to the
average.
8 WAP to input any alphabet and check whether it is a
vowel or not.
9 WAP to input 2 numbers and an operator and perform
the function according to the given operator. Use switch
case.
10 WAP to print the table of any number.
11 WAP to print all the prime numbers between 1 and n.
12 WAP to print all the Armstrong numbers between 1 and
n.
13 WAP to print Fibonacci Series till the nth term
14 WAP to convert a given decimal number into a binary
number.
15 WAP to print the Number Triangle.
16 WAP to find the HCF of two numbers.
17 WAP to find nPr and nCr for the given value of n and r.
18 WAP to find the generic root of a given number.
19 WAP to find whether the given number is a perfect
square or not.
20 WAP to find the sum of squares of natural numbers till
n.
1. WAP to input two numbers and swap their values without
using a third variable.

#include <stdio.h>
OUTPUT
int main()
Enter the value of a
{
3
int a,b;
Enter the value of b
printf("Enter the value of a \n");
2
scanf("%d",&a);
2 is the new value of a
printf("Enter the value of b \n");
3 is the new value of b
scanf("%d",&b);
a=a-b;
b=b+a;
a=b-a;
printf("%d is the new value of a \n",a);
printf("%d is the new value of b",b);
return 0;
}
2. WAP to input two numbers and find which one is greater
using ternary or conditional operator.
#include <stdio.h>
OUTPUT
int main()
Enter the value of a
{
56
int a,b;
Enter the value of b
printf("Enter the value of a \n");
34
scanf("%d",&a);
a is greater than b
printf("Enter the value of b \n");
scanf("%d",&b);
(a>b)?printf("a is greater than b"):printf("b is greater than a");
return 0;
}

3. WAP to print your name without using semicolon.

#include <stdio.h>
OUTPUT
int main()
Enter name:
{
Ritu
char name[10];
Ritu
printf("Enter name:\n");
scanf("%s",name);
if(printf("%s",name))
{
}

}
4. WAP to input any number and check whether it is even or odd.
#include <stdio.h>
OUTPUT
int main()
Enter a number
{
13
int a;
13 is odd
printf("Enter a number \n");
scanf("%d",&a);
if(a%2==0)
{
printf("%d is even",a);
}
else
{
printf("%d is odd",a);
}
return 0;
}
Q.5) WAP to input any year and check whether it is leap year or
not.
#include <stdio.h>
OUTPUT
int main()
Enter a year
{
1900
int a;
1900 is not a leap year.
printf("Enter a year \n");
Enter a year
scanf("%d",&a);
2002
if(a%100==0)
2002 is not a leap year
{
Enter a year
if(a%400==0)
1996
printf("%d is a leap year.",a);
1996 is a leap year
else
printf("%d is not a leap year.",a);
}
else
{
if(a%4==0)
printf("%d is a leap year",a);
else
printf("%d is not a leap year",a);
}
return 0;
}
Q.6) WAP to input 3 numbers and find the greatest number.
#include <stdio.h>
OUTPUT
int main()
Enter the three numbers:
{ 43 6 27
int a,b,c; 43 is greatest.

printf("Enter the three numbers: \n");


scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("%d is greatest.",a);
else
printf("%d is greatest.",c);
}
else if(b>a)
{
if(b>c)
printf("%d is greatest.",b);
else
printf("%d is greatest.",c);
}
return 0;
}
Q.7) WAP to input marks of 5 subjects. Calculate their average
and print a message according to the average.

#include <stdio.h>
int main()
{
int m1,m2,m3,m4,m5,tot;
float avg;
printf("Enter the marks of 5 subjects: \n");
scanf("%d %d %d %d %d",&m1,&m2,&m3,&m4,&m5);
tot=m1+m2+m3+m4+m5;
avg=tot/5;
printf("The total of all 5 subjects is: %d \n",tot);
printf("The average thus calculated is: %f \n",avg);
return 0;
}

OUTPUT:-

Enter the marks of 5 subjects:


90 91 93 96 93
The total of all 5 subjects is: 463
The average thus calculated is: 92.000000
Q.8) WAP to input any alphabet and check whether it is a vowel
or not.
#include <stdio.h>
int main()
{
char ch;
printf("Enter an alphabet:\n");
scanf("%c", &ch);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
printf("\nIt is a vowel.");
else if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
printf("\nIt is a vowel.");
else
printf("\nIt is not a vowel.");
return 0;
}

OUTPUT:-

Enter an alphabet:
a
It is a vowel.
Enter an alphabet:
z
It is not a vowel.
Q.9) WAP to input 2 numbers and an operator and perform the
function according to the given operator. Program should be done
with switch case.

int main() { OUTPUT:-


char ch;
int a,b; Enter an operator
printf("Enter an operator \n"); +
scanf("%c",&ch); Enter the two numbers
printf("Enter the two numbers \n"); 23
scanf("%d %d",&a,&b); Result is: 5
int result=0;
int i=0; Enter an operator
printf("\n"); *
switch (ch)//only checks int values in C Enter the two numbers
{ 35
case 43: Result is: 15
result=a+b;
break; Enter an operator
case 45: %
result=a-b; Enter the two numbers
break; 45
case 42: Incorrect Input
result=a*b;
break;
case 47:
result=a/b;
break;
default:
{
i=1;
}
}
switch(i)
{
case 0:
printf("Result is: %d",result);
break;
default:
printf("Incorrect Input");
}
return 0;
}
Q.10) WAP to print the table of any number in the following
format.
Input Number: 5
Output: 5*1=5
5*2=10
5*3=15….

#include <stdio.h>
OUTPUT
int main() {
Enter a number
int a;
12
printf("Enter a number\n");
** Table of 12 **
scanf("%d",&a);
12 * 1 = 12
printf("\n** Table of %d **",a);
12 * 2 = 24
int i=1;
12 * 3 = 36
while(i<=12)
12 * 4 = 48
{
12 * 5 = 60
printf("\n %d * %d = %d",a,i,(a*i));
12 * 6 = 72
i++;
12 * 7 = 84
}
12 * 8 = 96
}
12 * 9 = 108
12 * 10 = 120
12 * 11 = 132
12 * 12 = 144
Q.11) WAP to print all the prime numbers between 1 and n.

#include <stdio.h>
OUTPUT
int main() {
Enter any limit
int n;
20
printf("Enter any limit\n");
Prime numbers between 1 and
scanf("%d",&n);
20
int i=0,j=0,k=0;
1
printf("Prime numbers between 1 and %d \n",n);
2
for(i=1;i<=n;i++)
3
{
5
for(j=2;j<=(i/2);j++)
7
{
11
if(i%j==0)
13
{
17
k++;
19
break;
}
}
if(k==0)
printf("%d \n",i);
k=0;
}
}
Q.12)WAP to print all the Armstrong Numbers between 1 and n.

# include <stdio.h>
OUTPUT:-
# include <math.h>
Enter any number
int main()
200
{
1
int n;
2
printf("Enter any number\n");
3
scanf("%d",&n);
4
int i=0,temp=0,rem=0,c=0,sum=0;
5
for(i=1;i<=n;i++)
6
{
7
sum=0;
8
c=0;
9
temp=i;
153
while(temp>0)
{
c++;
temp=temp/10;
}
temp=i;
while(temp>0)
{
rem=temp%10;
sum=sum+pow(rem,c);
temp=temp/10;
}
if(sum==i)
{
printf("%d \n",i);
}

}
}
Q.13) WAP to print Fibonacci Series upto nth terms.

# include <stdio.h>
# include <math.h> OUTPUT:-
int main() Enter nth number of term
{ 8
int i=0,a=0,b=1,c=0; 0 1 1 2 3 5 8 13
int n;
printf("Enter nth number of term\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(i<=1)
{
printf("%d ",i);
}
else
{
c=a+b;
a=b;
b=c;
printf("%d ",c);

}
}
return 0;
}
Q.14) WAP to convert decimal number to binary digits.

# include <stdio.h>
# include <math.h> OUTPUT:-
int main()
{ Enter any number
int n; 10
printf("Enter any number\n"); Hence binary of 10 is: 1010
scanf("%d",&n);
int j=0,rem=0,sum=0,i=0;
for(j=n;;j=j/2)
{
rem=j%2;
sum=sum+(rem*pow(10,i));
i++;
if(j==1)
break;
}
printf("Hence binary of %d is: %d",n,sum);
return 0;
}
15. WAP To print the following number triangle.
Sample input: 4
Sample output:
1
121
12321
1234321

# include <stdio.h>
int main() OUTPUT:-
{ Enter a range
int n; 5
printf("Enter a range\n"); 1
scanf("%d",&n); 121
int i=0,j=0,k=0,l=0; 12321
for(i=1;i<=n;i++) 1234321
{ 123454321
for(j=i;j<n;j++)
printf(" ");
for(k=1;k<=i;k++)
printf("%d",k);
for(l=(i-1);l>0;l--)
printf("%d",l);
printf("\n");
}
return 0;
}
16. WAP to find the HCF of two numbers.

# include <stdio.h>
OUTPUT:-
# include <math.h>
int main()
Enter two numbers
{
56 49
int a,b;
Required HCF of 56 and 49 is: 7
printf("Enter two numbers\n");
scanf("%d %d",&a,&b);
int c=0;
c=min(a,b);
int i=0,result=0;

for(i=1;i<=c;i++)
{
if(a%i==0 && b%i==0)
{
result=i;
}
}
printf("\nRequired HCF of %d and %d is: %d",a,b,result);
return 0;
}
int min(int x,int y)
{
int ans=(x>y)?x:y;
return ans;
}
Q.17) WAP to find nPr and nCr for given value of n and r.

# include <stdio.h>
# include <math.h>
OUTPUT
int main() Enter the value of n and r
{ 69
int n,r; r should be less than or equal to n
printf("Enter the value of n and r\n"); Enter the value of n and r
scanf("%d %d",&n,&r); 43
int nfac=0,rfac=0,nrfac=0,ans1=0,ans2=0; C(4,3) is : 4
if(r<=n) P(4,3) is : 24
{
nfac=factorial(n);
rfac=factorial(r);
nrfac=factorial(n-r);
ans1=nfac/nrfac;
ans2=ans1/rfac;
printf("C(%d,%d) is : %d\n",n,r,ans2);
printf("P(%d,%d) is : %d",n,r,ans1);

}
else
printf("r should be less than or equal to n");

}
int factorial(int x)
{
int i=0;
int fact=1;
for(i=x;i>1;i--)
fact=fact*i;
return fact;
}
Q.18) WAP to print the generic root of the given number.
Sample Input:- 24112002
2+4+1+1+2+0+0+2=12=1+2=3
Calculate sum of digits of a given number until we get a
single digit
output.
Sample Output: 3
# include <stdio.h>
# include <math.h> OUTPUT
int main() Enter any number
{ 04051975
int n; Generic root=4
printf("Enter any number\n");
scanf("%d",&n);
int c=count(n);
while(c!=1)
{
n=sum(n);
c=count(n);
}
if(c==1)
printf("Generic root=%d",sum(n));
return 0;
}
int sum(int n)
{
int sum=0;
while(n>0)
{
sum=sum+(n%10);
n=n/10;
}
return sum;
}
int count(int x)
{
int c=0;
while(x>0)
{
c++;
x=x/10;
}
return c;
}
Q.19) WAP to find whether the number is a perfect square or not.

# include <stdio.h>
# include <math.h> OUTPUT
int main()
{ Enter a number
int n; 49
int i=0,k=0; 49 is a perfect square
printf(“Enter a number\n”);
scanf(“%d”,&n);
for(i=1;i<=(n/2);i++)
{
if((i*i)==n)
{
printf(“\n %d is a perfect square”,n);
k=1;
break;
}
}
if(k==0)
printf(“\n %d is not a perfect square”,n);
return 0;
}
Q.20) WAP to find the sum of the series:
12 + 22 + 32 + 42 + 52 +………..+n2
# include <stdio.h>
int main() OUTPUT
{ Enter a nth term of the series
int n; 5
printf("Enter a nth term of the series\n"); 1 + 4 + 9 + 16 + 25
scanf("%d",&n); Sum of the series= 55
int i=0,sum=0;
for(i=1;i<=n;i++)
{
if(i!=n)
{
printf("%d + ",(i*i));
sum=sum+(i*i);
}
else
{
printf("%d",(i*i));
sum=sum+(i*i);
}
}
printf("\nSum of the series= %d",sum);
return 0;
}

You might also like