Вы находитесь на странице: 1из 35

1. /*WAP to print Hello 10 times by using While loop*/ #include<stdio.

h> main() { int i=1; while(i<=10) { printf("Hello\n"); i++; } }

2. /*WAP to find the Factorial of a given number by using While loop*/ #include<stdio.h> main() { int n,i=1,fact=1; printf("Enter any integer: "); scanf("%d",&n); while(i<=n) { fact=fact*i; i++; } printf("The factorial of %d is: %d\n",n,fact); }

3. /*WAP to print the Multiplication table of given number using while loop*/ #include<stdio.h> main() { int n,i=1; printf("Enter any integer: "); scanf("%d",&n); while(i<=30) { printf("%3d * %3d = %3d\n",n,i,n*i); i++; }
}

4.

/*WAP to print Hello 10 times using Do-While loop*/ #include<stdio.h> main() { int i=1; do { printf("Hello\n"); i++; }while(i<=10); }

5. /*WAP to find the Factorial of a given number using Do-While loop*/ #include<stdio.h> main() { int i=1, fact=1, n; printf("Enter a Number: "); scanf("%d",&n); do { fact=fact*i; i++; }while(i<=n); printf("The factorrial of %d is %d",n,fact); }

6. /*WAP to print the Multiplication table of a given number using DoWhile loop*/ #include<stdio.h> main() { int i=1, n; printf("Enter a Number: "); scanf("%d",&n); do { printf("%3d *%3d =%3d\n",n,i,n*i); i++; }while(i<=10); }

7. /*WAP to find the given number is Even or not*/ #include<stdio.h> void main() { int i=1,n; clrscr(); printf("Enter a number: "); scanf("%d",&n); while(i<=n) { if(i%2==0) printf("%d\n",i); i++; } getch();

8 /*WAP to print the is Even numbers between given range*/ #include<stdio.h> main() { int i, first, last, n; printf("Enter first and last numbers: "); scanf("%d%d",&first,&last); for(n=first;n<=last;n++) { i=n; while(i<=n) { if(i%2==0) printf("%d\n",i); i++; } } }

9. /*WAP to find the given number is Odd or not*/ #include<stdio.h> void main() { int i=1,n; clrscr(); printf("Enter a number: "); scanf("%d",&n); while(i<=n) { if(i%2!=0) printf("%d\n",i); i++; } getch(); }

10. /*WAP to print the is Odd numbers between given range*/ #include<stdio.h> main() { int i,first, last, n; printf("Enter first and last numbers: "); scanf("%d%d",&first,&last); for(n=first;n<=last;n++) { i=n; while(i<=n) { if(i%2!=0) printf("%d\n",i); i++; } } }

11. /*WAP to find the Average of N numbers */ #include<stdio.h> void main() { int i=1, n,sum=0,m; float avg; clrscr(); printf("Enter a number: "); scanf("%d",&n); m=n; while(i<=n) { sum=sum+n; n--; } printf("sum= %d\n",sum); avg=(float)sum/m; printf("Average= %f",avg); getch(); }

12. /*WAP to print and find the SumofDivisors of N*/ #include<stdio.h> main() { int n,i=1,sum=0; printf("Enter a number: "); scanf("%d", &n); while(i<=n) { if(n%i==0) { printf("%d\n",i); sum=sum+i; } i++; } printf("sum value =%d",sum); }

13. /*WAP to find the Reverse of a given number using While*/ #include<stdio.h> main() { int n,a,b,sum; printf("Enter a Number: "); scanf("%d",&n); sum=0; while(n>0) { a=n%10; sum=sum*10+a; n=n/10; } printf("The reverse of given number is %d\n",sum); }

14. /*WAP to find the Reverse of a given number using D0-While*/ #include<stdio.h> main() { int n,a,b,sum; printf("Enter a Number: "); scanf("%d",&n); sum=0; do { a=n%10; sum=sum*10+a; n=n/10; }while(n>0); printf("The reverse of given number is %d\n",sum); }

15. /*WAP to find the given number is Palindrome or not*/ #include<stdio.h> main() { int n,m,a,b,sum; printf("Enter a Number: "); scanf("%d",&n); m=n; sum=0; while(n>0) { a=n%10; sum=sum*10+a; n=n/10; } printf("The reverse of given number is %d\n",sum); if(sum==m) printf("%d is Palindrome",m); else printf("%d is not Palindrome",m); }

16. /*WAP to print Palindrome numbers between range*/ #include<stdio.h> void main() { int i,j,n,r,rev,first,last; clrscr(); printf("Enter a first and last numbers: "); scanf("%d%d",&first,&last); for(i=first;i<=last;i++) { n=i; rev=0; while(n>0) { r=n%10; rev=rev*10+r; n=n/10; } if(rev==i) printf("%d ",i); } getch(); }

17. /*WAP to find the given number is Perfect or Deficient or Abundant*/ #include<stdio.h> main() { int n,i,sum=0; printf("Enter a number: "); scanf("%d", &n); for(i=1;i<=n/2;i++) { if(n%i==0) sum=sum+i; } if(sum==n) printf("%d is perfect number",n); else if(sum<n) printf("%d is deficient number",n); else printf("%d is abundant number",n); }

18. /*WAP to print the is Perfect numbers between given range*/ #include<stdio.h> main() { int n,i,sum,first,last; printf("Enter first and last numbers: "); scanf("%d%d", &first,&last); for(n=first;n<=last;n++) { sum=0; for(i=1;i<n;i++) { if(n%i==0) sum=sum+i; } if(sum==n) printf("%d ",n); } }

19. /*WAP to print the is Deficient numbers between given range*/ #include<stdio.h> main() { int n,i,sum=0,first,last; printf("Enter first and last numbers: "); scanf("%d%d", &first,&last); for(n=first;n<=last;n++) { sum=0; for(i=1;i<n;i++) { if(n%i==0) sum=sum+i; } if(sum<n) printf("%d ",n); } }

20. /*WAP to print the is Abundant numbers between given range*/ #include<stdio.h> main() { int n,i,sum=0,first,last; printf("Enter first and last numbers: "); scanf("%d%d", &first,&last); for(n=first;n<=last;n++) { sum=0; for(i=1;i<n;i++) { if(n%i==0) sum=sum+i; } if(sum>n) printf("%d ",n); } }

21. /*WAP to check the given number is Armstrong or not*/ #include<stdio.h> main() { int n,m,r,b,sum; printf("Enter a Number: "); scanf("%d",&n); m=n; sum=0; while(n>0) { r=n%10; b=r*r*r; sum=sum+b; n=n/10; } if(sum==m) printf("%d is Armstrong",m); else printf("%d is not an Armstrong",m);

22. /*WAP to print the Armstrong numbers between given range*/ #include<stdio.h> main() { int i,n,m,r,b,sum,first,last; printf("Enter first and last numbers: "); scanf("%d%d",&first,&last); for(i=first;i<=last;i++) { n=i; sum=0; while(n>0) { r=n%10; b=r*r*r; sum=sum+b; n=n/10; } if(sum==i) printf("%d\n",i); } }

23. /*WAP to check the given number is Strong or not*/ #include<stdio.h> main() { int i,m,n,r,fact,sum; printf("Enter a number: "); scanf("%d",&n); for(sum=0,m=n;m>0;m=m/10) { r=m%10; for(fact=1;r>0;r--) fact=fact*r; sum=sum+fact; } if(sum==n) printf("%d is strong",n); else printf("%d is not strong",n); }

24. /*WAP to print the is Strong numbers between given range*/ #include<stdio.h> main() { int i,m,n,r,fact,sum,first,last; printf("Enter fisrt and last numbers: "); scanf("%d%d",&first,&last); for(n=first;n<=last;n++) { for(sum=0,m=n;m>0;m=m/10) { r=m%10; for(fact=1;r>0;r--) fact=fact*r; sum=sum+fact; } if(sum==n) printf("%d ",n); } }

25. /*WAP to find the given number is Prime or not*/ #include<stdio.h> main() { int n,i,c=0; printf("Enter n value: "); scanf("%d",&n); for(i=1;i<=n;i++) { if(n%i==0) c++; } if(c==2) printf("%d is prime",n); else printf("%d is not prime",n); }

26. /*WAP to print Prime numbers between range*/ #include<stdio.h> main() { int n, i=1, count, first, last; printf("Enter a first and last numbers: "); scanf("%d%d",&first,&last); for(n=first;n<=last;n++) { count=0; for(i=1;i<=n;i++) { if(n%i==0) count++; } if(count==2) printf("%d ",n); } }

27. /*WAP to print the Fibonacci series of a given number*/ #include<stdio.h> void main() { int a=0,b=1,c,n; clrscr(); printf("Enter a number: "); scanf("%d",&n); printf("%d %d ",a,b); c=a+b; while(c<=n) { printf("%d ",c); a=b; b=c; c=a+b; } getch(); }

28. /*WAP to print Fibonacci numbers between given range*/ #include<stdio.h> main() { int a=0,b=1,c,n,first,last; printf("Enter first and last numbers: "); scanf("%d %d",&first,&last); c = a+b; if(first > last) { printf("Invalid series options"); exit(0); } if(first == 0 && last ==0) printf("0 "); if(first ==0 && last !=0) printf("0 1 "); if(first == 1) printf("1 "); while(c<=last) { if(c>=first) printf("%d ",c); a = b; b = c; c = a+b; }

29. /*WAP to GCD of a given number*/ #include<stdio.h> void main() { int m,n,a,b,r; clrscr(); printf("Enter two nos: "); scanf("%d %d",&m,&n); if(m>n) { a=m; b=n; } else { a=n; b=m; } r=a%b; while(r!=0) { a=b; b=r; r=a%b; } printf("GCD = %d",b); getch(); }

30. /* Write a C program to accept a decimal number and convert it to binary and count the number of 1's in the binary number */ #include <stdio.h> main() { long int n, m, bin = 0, base = 1; int rem, ones = 0 ; printf("Enter a decimal integer "); scanf("%d", &n); /*maximum five digits */ m = n; while( n > 0) { rem = n % 2; if (rem==1) /*To count no.of 1s*/ { ones++; } bin = bin + rem * base; n=n/2; base = base * 10; } printf("Input number is = %ld\n", m); printf("Its Binary equivalent is = %ld\n", bin); printf("No.of 1's in the binary number is = %d\n", ones);

/*Program on break*/ #include<stdio.h> main() { int i=1,sum=0; system("clear"); while(i<=10) { sum=sum+i; if(sum==6) break; i++; } printf("Sum = %d",sum);

/*Progrm on continue*/ #include<stdio.h> main() { int i=0; system("clear"); while(i<10) { i++; if(i==5) continue; printf("%d ",i); }

/*Progrm on continue1*/
#include<stdio.h> main() { int i=1,sum=0,n; system("clear"); for(i=0;i<5;i++) { printf("Enter a Number: "); scanf("%d",&n); if(n<0) { printf("You have entered -ve Number\n"); continue; } sum=sum+n; } printf("The sum of the +ve numbers entered = %d",sum); }

/*Progrm on goto*/ #include<stdio.h> main() { int i=1; system("clear"); Hello:printf("%d ",i); i++; if(i<=10) goto Hello; }

/*Progrm on goto*/ #include<stdio.h> main() { int i=1; system("clear"); Start:if(i>10) goto End; printf("%d ",i); i++; goto Start; End:printf("End\n"); }

Вам также может понравиться