You are on page 1of 8

Content Developed By | Faiz M Arif Khan & Mohd Daneyal

SIMPLE ARTHMETIC OPERATION 1

1.WAP to add two numbers 2.WAP to swap values of two no.s 3.WAP to swap values of two no.s
without using third variable
int main( ){ int main( ){
int a, b,c; int a, b, temp; int main( ){
printf(“Enter Values for A and B: ”); printf(“Enter Values for A and B: ”); int a, b;
scanf(“%d%d”,&a,&b); scanf(“%d%d”,&a,&b); printf(“Enter Values for A and B: ”);
c = a+b; printf(“BEFORE :”); scanf(“%d%d”,&a,&b);
printf(“RESULT : %d\n“+c); printf(“A=%d B=%d\n”,a,b); printf(“BEFORE :”);
return 0; temp = a; printf(“A=%d B=%d\n”,a,b);
} a = b; a = a+b;
b = temp; b = a-b;
printf(“AFTER :”); a = a-b;
OUTPUT: printf(“A=%d B=%d\n”,a,b); printf(“AFTER :”);
Enter Values for A and B : return 0; printf(“A=%d B=%d\n”,a,b);
12 13 } return 0;
RESULT : 25 }
OUTPUT: OUTPUT:
Enter Values for A and B : Enter Values for A and B :
12 13 17 19
BEFORE: A=12 B=13 BEFORE: A=17 B=19
AFTER : A=13 B=12 AFTER : A=19 B=17

4.WAP to find sum digits of three digit 5.WAP to find reverse of three digit 6.WAP to calculate the area of circle.
number. number.
int main( )
int main( ){ int main( ){ {
int n, sum, d1, d2, d3; int n, rev, d1, d2, d3; float radius, area;
printf(“Enter 3 dig. number: ”); printf(“Enter 3 dig. number: ”); const float pi = 3.14f;
scanf(“%d”,&n); scanf(“%d”,&n); printf(“Enter radius: ”);
d3 = n%10; d3 = n%10; scanf(“%f”,&radius);
n = n/10; n = n/10; area = pi*radius*radius;
d2 = n%10; d2 = n%10; printf(“CIRCLE DETAILS:\n“);
n = n/10; n = n/10; printf(“RADIUS:%f\n”,radius);
d1 = n%10; d1 = n%10; printf(“AREA:%f\n”,area);
sum = d3 + d2 + d1; rev = 100*d3 + 10*d2 + d1; return 0;
printf(“SUM OF DIG: %d”,sum); printf(“REVERSE IS: %d”,rev); }
return 0; return 0;
} } OUTPUT:
Enter radius : 8.2
OUTPUT: OUTPUT:
Enter 3 dig number: 581 Enter 3 dig number: 687 CIRCLE DETAILS:
SUM OF DIG: 14 REVERSE IS: 786 RADIUS:8.2
AREA :211.1336
Content Developed By | Faiz M Arif Khan & Mohd Daneyal

SIMPLE ARTHMETIC OPERATION 2

7.WAP to find square of a number. 8.WAP to find cube of a number. 9.WAP to find square root of a
number.
int main( ){ int main( ){ #include<math.h>
int n, sq; int n, cb; int main( ){
printf(“Enter Value for N : ”); printf(“Enter Value for N : ”); float n, sq;
scanf(“%d”,&n); scanf(“%d”,&n); printf(“Enter Value for N : ”);
sq = n*n; cb = n*n*n; scanf(“%f”,&n);
printf(“SQUARE IS : %d\n“,sq); printf(“CUBE IS : %d\n“,cb); sq = sqrt(n);
return 0; return 0; printf(“SQUARE ROOT IS : %f\n“,sq);
} } return 0;
}
OUTPUT: OUTPUT: OUTPUT:
Enter Value for N : 4 Enter Value for N : 3 Enter Value for N : 16
SQUARE IS : 16 CUBE IS : 27 SQUARE ROOT IS : 4.0

10.WAP to find x to the power y (xy). 11.WAP to convert temp from 12.WAP to convert distance from cms
Fahrenheit to Celsius . to kms.
int main( ){
int x,y,res; int main( ){ int main( ){
printf(“Enter Values for X & Y : ”); float c,f; float dc, dk;
scanf(“%d%d”,&x,&y); printf(“Enter Temp in (F) : ”); printf(“Enter distance in cms : ”);
res = pow(x,y); scanf(“%f”,&f); scanf(“%f”,&dc);
printf(“X^Y IS : %d\n“,res); c = (5.0/9.0)*(f-32); dk = dc/(100*1000);
return 0; printf(“Temp in Celsius : %f\n“,c); printf(“SQUARE ROOT IS : %d\n“,sq);
} return 0; return 0;
} }
OUTPUT:
Enter Values for X & Y : OUTPUT: OUTPUT:
4 3 Enter Temp in (F): 42 Enter distance in cms : 5566930
X^Y IS : 64 Temp in Celsius: 5.55556 Distance in Kms : 55.6693

13.WAP to input time in millisecond 14.WAP to find difference between 15. WAP to find greatest among two
s and show in minutes and seconds. two numbers, diff must be positive. numbers using ternary operator.

int main( ){ int main( ){ int main( ){


int tms, tm, ts; int a, b, diff; int a, b;
printf(“Enter Time in millis : ”); printf(“Enter Values for A : ”); printf(“Enter Values for A : ”);
scanf(“%d”,&tms); scanf(“%d”,&a); scanf(“%d”,&a);
ts = tms/1000; printf(“Enter Values for B : ”); printf(“Enter Values for B : ”);
tm = ts/60; scanf(“%d”,&b); scanf(“%d”,&b);
ts = ts%60; diff = (a>b) ? a-b : b-a; (a>b)? printf(“A is Greatest\n”) :
printf(“TIME(mm:ss) - %d:%d”,tm,ts); printf(“DIFFERENCE IS : %d\n“,diff); printf(“B is Greatest\n”);
return 0; return 0; return 0;
} } }
OUTPUT: OUTPUT:
OUTPUT: Enter Values for A : 62 Enter Values for A : 62
Enter Time in millis:863534 Enter Values for A : 95 Enter Values for B : 119
TIME(mm:ss) – 14:39 DIFFERENCE IS : 33 B is Greatest.
Content Developed By | Faiz M Arif Khan & Mohd Daneyal

CONDITIONAL PROGRAMMING 3

16. WAP to find greatest among two 17. WAP to find greatest among 18. WAP to find greatest among
numbers. three numbers. three numbers using Nested If-else.
int main( ){ int main( ){ int main( ){
int a, b; int a, b, c; int a, b,c;
printf(“Enter Values for A : ”); printf(“Enter Values for A,B & C : ”); printf(“Enter Values for A,B & C : ”);
scanf(“%d”,&a); scanf(“%d%d%d”,&a,&b,&c); scanf(“%d%d%d”,&a,&b,&c);
printf(“Enter Values for B : ”); if(a>b) {
scanf(“%d”,&b); if(a>b && a>c) { if(a>c)
if(a>b) { printf(“A is Greatest\n”) ; printf(“A is Greatest\n”) ;
printf(“A is Greatest\n”) ; } else
} else if(b>c){ printf(“C is Greatest\n”) ;
else { printf(“B is Greatest\n”); }
printf(“B is Greatest\n”); } else {
} else { if(b>c)
return 0; printf(“C is Greatest\n”); printf(“B is Greatest\n”) ;
} } else
return 0; printf(“C is Greatest\n”) ;
OUTPUT: } }
Enter Values for A : 62 return 0;
Enter Values for B : 119 }
B is Greatest. OUTPUT: OUTPUT:
Enter Values for A,B & C : Enter Values for A,B & C :
52 43 66 52 43 66
C is Greatest. C is Greatest.

19. WAP to check the given number 20. WAP to check the given number 21. WAP to check the alphabet is
is positive or negative is even or odd vowel or not.
int main( ){ int main( ){ int main( ){
int a; int a; int a, b,c;
printf(“Enter Values for A : ”); printf(“Enter Values for A : ”); printf(“Enter Values for A : ”);
scanf(“%d”,&a); scanf(“%d”,&a); scanf(“%c”,&a);
if(a>0) { if(a%2 == 0) { if(a==’a’ || a==’e’ || a==’I’ ||a=’o’||
printf(“A is POSITIVE NUMBER\n”) ; printf(“%d is EVEN NUMBER\n”,a) ; a==’u’) {
} } printf(“Character is VOWEL\n”);
else { else { }
printf(“A is NEGATIVE NUMBER\n”); printf(“%d is ODD NUMBER\n”,a); else if(a==’A’ || a==’E’ || a==’I’
} } ||a=’O’|| a==’U’) {
return 0; return 0; printf(“Character is VOWEL\n”);
} } }
else {
OUTPUT: OUTPUT: printf(“Character is NOT VOWEL\n”);
Enter Values for A : -9 Enter Values for A : 18 }
A is NEGATIVE NUMBER. 18 is EVEN NUMBER. return 0;
}
OUTPUT:
Enter Values for A : x
Character is NOT VOWEL
Content Developed By | Faiz M Arif Khan & Mohd Daneyal

CONDITIONAL PROGRAMMING 4

22.WAP to ask user to enter marks of 5 subjects and 23.WAP to print DAY according to user input for
calculate percentage then print GRADE according to day count.
marks in percentage.
int main( ){
int main( ){ int i;
int m1,m2,m3,m4,m5; printf(“Enter Day Number : ”);
float per; scanf(“%d”,&i);
printf(“Enter Marks Subject 1: ”); switch(i) {
scanf(“%d”,&m1); case 1:
printf(“Enter Marks Subject 2: ”); printf(“DAY IS MONDAY\n”);
scanf(“%d”,&m2); break;
printf(“Enter Marks Subject 3: ”); case 2:
scanf(“%d”,&m3); printf(“DAY IS TUESDAY\n”);
printf(“Enter Marks Subject 4: ”); break;
scanf(“%d”,&m4); case 3:
printf(“Enter Marks Subject 5: ”); printf(“DAY IS WEDNESDAY\n”);
scanf(“%d”,&m5); break;
per = (m1+m2+m3+m4+m5)/5.0f; case 4:
printf(“YOUR PERCENTGE : %f\n”,per); printf(“DAY IS THURSDAY\n”);
if(per>=70){ break;
printf(“YOUR GRADE IS : A\n”); case 5:
} printf(“DAY IS FRIDAY\n”);
else if(per>=60){ break;
printf(“YOUR GRADE IS : B\n”); case 6:
} printf(“DAY IS SATURDAY\n”);
else if(per>=50){ break;
printf(“YOUR GRADE IS : C\n”); case 7:
} printf(“DAY IS SUNDAY\n”);
else if(per>=40){ break;
printf(“YOUR GRADE IS : D\n”); default:
} printf(“WRONG CHOICE \n”);
else{ }
printf(“YOUR GRADE IS : FAIL\n”); return 0;
} }
return 0;
} OUTPUT:1
Enter Day Number : 3
OUTPUT: DAY IS WENESDAY
Enter Marks Subject 1: 55
Enter Marks Subject 2: 67 OUTPUT:2
Enter Marks Subject 3: 50 Enter Day Number : 9
Enter Marks Subject 4: 68 WRONG CHOICE
Enter Marks Subject 5: 78
YOUR PERCENTAGE : 63.60
YOUR GRADE IS : B
Content Developed By | Faiz M Arif Khan & Mohd Daneyal

LOOPING PROGRAMMING (CALCULATIVE) 5

24.WAP to calculate the sum of 25.WAP to calculate the sum of 26.WAP to find the sum of numbers
numbers from 1 to 10 numbers from 1 to 10 using while. from 1 to 10 using do-while.

int main( ){ int main( ){ int main( ){


int i, sum=0; int i, sum=0; int i, sum=0;
i=1; i=1;
for(i=1; i<=10; i++) while(i<=10){ do{
{ sum = sum+i; sum = sum+i;
sum = sum+i; i++; i++;
} } } while(i<=10);
printf(“SUM IS : %d\n“+sum); printf(“SUM IS : %d\n“+sum); printf(“SUM IS : %d\n“+sum);
return 0; return 0; return 0;
} } }

OUTPUT: OUTPUT: OUTPUT:


SUM IS : 55 SUM IS : 55 SUM IS : 55

27.WAP to find the factorial of a 28.WAP to find x to the power y (xy) 29.WAP to check the given number
given number. using loop. is prime or not.

int main( ){ int main( ){ int main( ){


int i, fact=1, n; int x,y,res=1,i; int i, n, flag=1;
printf(“Enter Value of N: “); printf(“X : ”); printf(“Enter Value of N: “);
scanf(“%d”, &n); scanf(“%d”,&x); scanf(“%d”, &n);
printf(“Y : ”);
for(i=n; i>=1; i--) scanf(“%d”,&y); for(i=2; i<=n/2; i++) {
{ if(n%i == 0) {
fact = fact*i; for(i=0; i<y; i++) flag = 0;
} { }
printf(“FACTORIAL IS : %d\n”,fact); res = res*x; }
return 0; } if(flag == 1) {
} printf(“X^Y IS : %d\n“,res); printf(“%d is PRIME NO.\n”,n);
return 0; }
} else {
printf(“%d is NOT PRIME NO.\n”,n);
}
OUTPUT: return 0;
OUTPUT: }
X : 8
Enter Value of N: 4
Y : 2
FACTORIAL IS : 24 OUTPUT:1
X^Y IS : 64
Enter Value of N: 17
17 is PRIME NO.
OUTPUT:1
Enter Value of N: 9
9 is NOT PRIME NO.
Content Developed By | Faiz M Arif Khan & Mohd Daneyal

LOOPING PROGRAMMING (SERIES) 6

30.WAP to check the given number 31. WAP to print all prime numbers 32. WAP to print the following
is prime or not. (by count factors) till 20. series.
int main( ){
int i, n, factors; int main( ){ 1 2 3 4 5 ……… n
printf(“Enter Value of N: “); int i, j, factors;
for(n=2;nj<=20; n++) { int main( ){
scanf(“%d”, &n);
factors = 0; int n, i;
factors = 0;
for(i=1; i<=n ; i++) { printf(“Enter Value for N : “);
for(i=1; i<=n ; i++) {
if(n%i == 0) { scanf(“%d”, &n);
if(n%i == 0) {
factors++ factors++
} for(i=1; i<=n; i++) {
}
} // i’s for end printf(“%d “, i);
}
if(factors == 2) { }
if(factors == 2) {
printf(“%d ”,n); printf(“\n”);
printf(“%d is PRIME NO.\n”,n);
} return 0;
}
} // n’s for end }
else {
printf(“%d is NOT PRIME NO.\n”,n); return 0;
} }
return 0; OUTPUT:
} Enter Value for N: 11
OUTPUT: OUTPUT:
2 3 5 7 11 13 17 19 1 2 3 4 5 6 7 8 9 10 11
Enter Value of N: 17
17 is PRIME NO.

33. WAP to print the following 34. WAP to print the following 35. WAP to print the following
series. series. series.

2 4 6 8 10 . . . . . . . n 1 3 5 7 9 . . . ..n 3 6 9 12 15 . . . . . . n

int main( ){ int main( ){ int main( ){


int n, i; int n, i; int n, i, value;
printf(“Enter Value for N : “); printf(“Enter Value for N : “); printf(“Enter Value for N : “);
scanf(“%d”, &n); scanf(“%d”, &n); scanf(“%d”, &n);

for(i=1; i<=n; i++) { for(i=1; i<=n; i++) { for(i=1; i<=n; i++) {


if(n%2 == 0) { if(n%2 != 0) { value = i*3;
printf(“%d “, i); printf(“%d “, i); printf(“%d “, value);
} } }
} } printf(“\n”);
printf(“\n”); printf(“\n”); return 0;
return 0; return 0; }
} }

OUTPUT: OUTPUT: OUTPUT:


Enter Value for N: 15 Enter Value for N: 15 Enter Value for N: 10
2 4 6 8 10 12 14 1 3 5 7 9 11 13 15 3 6 9 12 15 18 21 24 27 30
Content Developed By | Faiz M Arif Khan & Mohd Daneyal

LOOPING PROGRAMMING (SERIES) 7

36. WAP to print the following 37. WAP to print the following 38. WAP to print the following
series. febonnaci series. febonnaci series.

1 3 7 15 31 . . . . . . . upto n times 1 1 2 3 5 8 13 . . . . upto n time 0 1 1 2 3 5 8 13 . . . . upto n time

#include<math.h> int main( ){ int main( ){


int main( ){ int , i, a,b,c; int , i, a,b,c;
int n, i, value=0; printf(“Enter Value for N : “); printf(“Enter Value for N : “);
printf(“Enter Value for N : “); scanf(“%d”, &n); scanf(“%d”, &n);
scanf(“%d”, &n); a = 1; a = -1;
b = 0; b = 1;
for(i=0; i<=n; i++) { for(i=0; i<n; i++) { for(i=0; i<n; i++) {
value = value + pow(2, i); c = a+b; c = a+b;
printf(“%d “, value); printf(“%d “,c); printf(“%d “,c);
} a = b; a = b;
printf(“\n”); b = c; b = c;
return 0; } }
} printf(“\n”); printf(“\n”);
return 0; return 0;
OUTPUT: } }
Enter Value for N: 6 OUTPUT: OUTPUT:
1 3 7 15 31 63 Enter Value for N: 8 Enter Value for N: 8
1 1 2 3 5 8 13 21 0 1 1 2 3 5 8 13

39. WAP to print to print the 40. WAP to check given number is 41. WAP to count the digits in a
following series. PALINDROME or NOT. given number.
int main( ){
1 2 2 3 3 3 4 4 4 4.. . . int n, rev = 0, temp; int main( ){
printf(“Enter Value for N : “); int n, dig=0;
int main( ){ printf(“Enter Value for N : “);
scanf(“%d”, &n);
int i,j; scanf(“%d”, &n);
temp = n;
for(i=1; i<=4; i++) {
while(temp!=0) {
for(j=1; j<=i; j++) { while(n!=0){
rev = rev*10 + temp%10;
printf(“%d “,i); dig++;
temp = temp/10;
} n = n/10;
}
} }
if(rev == n) {
printf(“\n”); printf(“TOTAL DIGITS : %d\n”,dig);
printf(“N is PALINDROME\n”);
return 0; printf(“\n”);
}
} return 0;
else {
printf(“N is NOT PALINDROME\n”); }
OUTPUT:
}
1 2 2 3 3 3 4 4 4 4 printf(“\n”);
return 0; OUTPUT:
} Enter Value for N: 1232
OUTPUT: TOTAL DIGITS : 4
Enter Value for N: 1221
N is PALINDROME
Content Developed By | Faiz M Arif Khan & Mohd Daneyal

LOOPING PROGRAMMING (PATTERNS) 8

42.WAP to print the pattern as in 43.WAP to print the pattern as in 44.WAP to print the pattern as in
output box. output box. output box.
int main(){ int main(){ int main(){
inti,j; inti,j; char i,j;
for(i=1;i<=5;i++){ for(i=1;i<=5;i++) { for(i='A';i<='E';i++){
for(j=1;j<=i;j++) for(j=4;j>=i;j--) { for(j='A';j<=i;j++) {
{ printf(" "); printf("%c ",j);
printf("*"); } }
} for(j=1;j<=i;j++) { printf("\n");
printf("\n"); printf("* "); }
} } return 0;
return 0; printf("\n");
} } }
return 0;
}
OUTPUT : OUTPUT : OUTPUT :
* * A
** ** AB
*** *** ABC
**** **** ABCD
***** ***** ABCDE

45.WAP to print the pattern as in 46.WAP to print the pattern as in 47.WAP to print the pattern as in
output box. output box. output box.
int main(){ int main(){ int main(){
int i,j; int i,j; int i,j;
for(i=1;i<=5;i++) { for(i=1;i<=5;i++){ for(i=1;i<=5;i++){
for(j=4;j>=i;j--) { if(i%2==0) for(j=1;j<=i;j++) {
printf(" ",j); for(j=i;j>=1;j--) { printf(" ");
} printf("%d",j); }
for(j=1;j<=i;j++) { } for(j=1;j<=5-i;j++) {
printf("%d",j); else { printf("%d",j);
} for(j=1;j<=i;j++) { }
for(j--;j>=1;j--) { printf("%d",j); for(j=j-2;j>=1;j--) {
printf("%d",j); } printf("%d",j);
} } }
printf("\n"); printf("\n"); printf("\n");
} } }
return 0; return 0; return 0;
} } }

OUTPUT : OUTPUT : OUTPUT :


1 1
121 21 1234321
12321 123 12321
1234321 4321 121
123454321 12345 1

You might also like