C All Basic PRGM
C All Basic PRGM
Some other armstrong numbers are 0, 1, 153, 370, 407. Prgm: #include<stdio.h> #include<conio.h> main() { int r; long number = 0, c, sum = 0, temp; printf("Enter the maximum range upto which you want to find armstrong numbers "); scanf("%ld",&number); printf("Following armstrong numbers are found from 1 to %ld\n",number); for( c = 1 ; c <= number ; c++ ) { temp = c; while( temp != 0 ) { r = temp%10; sum = sum + r*r*r; temp = temp/10; } if ( c == sum ) printf("%ld\n", c); sum = 0; } getch(); return 0; } Output Enter the maximum range upto which you want to find armstrong numbers 1000 Following armstrong numbers are found from 1 to 1000 1
153 370 371 407 2.WAP to check whether a given no is Amstrong no or not #include<stdio.h> int main(){ int num,r,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); temp=num; while(num!=0){ r=num%10; num=num/10; sum=sum+(r*r*r); } if(sum==temp) printf("%d is an Armstrong number",temp); else printf("%d is not an Armstrong number",temp); return 0; } Output: Enter a number:153 153 is an Amstrong no 3.WAP to find the perfect no Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that number. For example 6 is perfect number since divisor of 6 are 1, 2 and 3. Sum of its divisor is 1 + 2+ 3 =6 Note: 6 is the smallest perfect number. Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28 Some more perfect numbers: 496, 8128 Prgm: #include<stdio.h>
int main(){ int n,i=1,sum=0; printf("Enter a number: "); scanf("%d",&n); while(i<n){ if(n%i==0) sum=sum+i; i++; } if(sum==n) printf("%d is a perfect number",i); else printf("%d is not a perfect number",i); return 0; } Output: Enter a number:6 6 is a perfect no 4.WAP to print perfect numbers upto an n values Prgm: #include<stdio.h> int main(){ int n,i,sum,limit; printf(enter the limit\n); scanf(%d,&limit); printf("Perfect numbers are: "); for(n=1;n<=limit;n++){ i=1; sum = 0; while(i<n){ if(n%i==0) sum=sum+i; i++; } if(sum==n) printf("%d ",n);
} return 0; } Output: Enter the limit:100 Perfect numbers are:6 28 5.WAP to make a number reverse Prgm: #include<stdio.h> int main(){ int num,r,reverse=0; printf("Enter any number: "); scanf("%d",&num); while(num){ r=num%10; reverse=reverse*10+r; num=num/10; } printf("Reversed of number: %d",reverse); return 0; } Output: Enter any number:12 Reverse of number:21 6.WAP to find the strong number Strong Number: A number is called strong number if sum of the factorial of its digit is equal to number itself. For example: 145 since 1! + 4! + 5! = 1 + 24 + 120 = 145 Prgm: #include<stdio.h> int main(){ int num,i,f,r,sum=0,temp; printf("Enter a number: ");
scanf("%d",&num); temp=num; while(num){ i=1,f=1; r=num%10; while(i<=r){ f=f*i; i++; } sum=sum+f; num=num/10; } if(sum==temp) printf("%d is a strong number",temp); else printf("%d is not a strong number",temp); return 0; } Output: Enter a number: 145 145 is a strong number 7.WAP to find a given string is palindrome or not without using any string functions Palindrome: Eg:Madam the reverse is also Madam ,thus a string which gets the same word by reversing is called palindrome Prgm: #include <stdio.h> main() { char text[100]; int begin, middle, end, length = 0; printf(Enter a string:\n); gets(text); while ( text[length] != '\0' ) length++; end = length - 1; middle = length/2; for( begin = 0 ; begin < middle ; begin++ ) { if ( text[begin] != text[end] )
{ printf("Not a palindrome.\n"); break; } end--; } if( begin == middle ) printf("Palindrome.\n"); return 0; } Output: Enter a string:Madam Palindrome 8.WAP to find whether the given no is a palindrome or not Prgm: #include <stdio.h> main() { int n, reverse = 0, temp; printf("Enter a number to check if it is a palindrome or not\n"); scanf("%d",&n); temp = n; while( temp != 0 ) { reverse = reverse * 10; reverse = reverse + temp%10; temp = temp/10; } if ( n == reverse ) printf("%d is a palindrome number.\n", n); else printf("%d is not a palindrome number.\n", n); return 0; } Output:
Enter a number to check if it is a palindrome or not:54145 54145 is a palindrome number 9.WAP to find the factorial of the number Factorial: Factorial of number is defined as: Factorial (n) = 1*2*3 * n For example: Factorial of 5 = 1*2*3*4*5 = 120 Note: Factorial of zero = 1 Prgm: #include<stdio.h> int main(){ int i=1,f=1,num; printf("Enter a number: "); scanf("%d",&num); while(i<=num){ f=f*i; i++; } printf("Factorial of %d is: %d",num,f); return 0; } Output: Enter a number: 5 Factorial of 5 is: 120 10.WAP to check whether given no is prime or not Logic:the no should be divisible only by itself and 1 which gives no remainder is called prime nos. Eg:7 it is divisible by 1 and 7 and produce the rem=0 whereas it is not divisible by 2,3,4,5,6 to give 0 Prgm: #include<stdio.h> main() { int n, c = 2; printf("Enter a number to check if it is prime\n"); scanf("%d",&n); for ( c = 2 ; c <= n - 1 ; c++ ) {
if ( n%c == 0 ) { printf("%d is not prime.\n", n); break; } } if ( c == n ) printf("%d is prime.\n", n); return 0; } Output: Enter a number to check if it is prime:13 13 is prime 11.WAP to generate Pascals triangle
Logic: To build the pascal triangle, start with "1" at the top, then continue placing numbers below it in a triangular pattern. Each number is build just sum of above two number, (except for the edge, which are all 1 and all numbers outside the Triangle are 0's). Prgm: #include<stdio.h> long fact(int); int main(){ int line,i,j; printf("Enter the no. of lines: "); scanf("%d",&line); for(i=0;i<line;i++){ for(j=0;j<line-i-1;j++) printf(" "); for(j=0;j<=i;j++) printf("%ld ",fact(i)/(fact(j)*fact(i-j))); printf("\n"); } return 0; }
long fact(int num){ long f=1; int i=1; while(i<=num){ f=f*i; i++; } return f; } Output: Enter the no. of lines: 8
12.WAP to generate Floyds triangle Floyd's triangle is a right angled-triangle using the natural numbers. Eg: 1 23 456 7 8 9 10 Prgm: #include<stdio.h> int main(){ int i,j,r,k=1; printf("Enter the range: "); scanf("%d",&r); printf("FLOYD'S TRIANGLE\n\n"); for(i=1;i<=r;i++){ for(j=1;j<=i;j++,k++) printf(" %d",k); printf("\n"); }
1 23 456 7 8 9 10 11 12 13 16 17 18 22 23 24 29 30 31 37 38 39 46 47 48
14 19 25 32 40 49
15 20 26 33 41 50
21 27 34 42 51
28 35 36 43 44 45 52 53 54 55
13.WAP to generate NCR factor In the mathematics nCr has defined as n Cr = n! /((n-r)!r!) Prgm: #include<stdio.h> int main(){ int n,r,ncr; printf("Enter any two numbers->"); scanf("%d %d",&n,&r); ncr=fact(n)/(fact(r)*fact(n-r)); printf("The NCR factor of %d and %d is %d",n,r,ncr); return 0; } int fact(int n){ int i=1; while(n!=0){ i=i*n; n--; } return i; } Output: Enter any two numbers->3 1 The NCR factor of 3 and 2 is 3 14.WAP to swap the two vaiables without the third variable
Swap:interchanging Eg:input->a=7,b=9 then after swaping a=9 and b=7 Prgm: #include <stdio.h> int main() { int a, b; printf("Enter two integers to swap\n"); scanf("%d%d", &a, &b); a = a + b; b = a - b; a = a - b; printf("After swapping); printf("a = %d\nb = %d\n",a,b); return 0; } Output: Enter two integers to swap 5 6 After swapping 6 5 15.WAP to calculate sum of digits of number Prgm: #include<stdio.h> int main(){ int num,sum=0,r; printf("Enter a number: "); scanf("%d",&num); for(;num!=0;num=num/10){ r=num%10; sum=sum+r; } printf("Sum of digits of number: %d",sum); return 0; } Output: Enter a number: 567 Sum of digits of number: 18 16.WAP to generate the ASCII characters
A-American S-Standard C-Code for. I-Information I-Interchange
Prgm: #include<stdio.h>
int main(){ int i; for(i=0;i<=255;i++) printf("ASCII value of character %c: %d\n",i,i); return 0; } Output: Note:its enough to know the below things A-65..Z-90 a-97..z-122 17.WAP to subtract two numbers without using minus sign Logic:
In c ~ is 1's complement operator. This is equivalent to: ~b = -b + 1 So, a ((~b) -1) = a-(-b + 1) + 1 =a+b1+1 =a+b
Prgm:
#include<stdio.h>
int main(){ int a,b; int sum; printf("Enter any two integers: "); scanf("%d%d",&a,&b); //sum = a - (-b); sum = a - ~b -1; printf("Sum of two integers: %d",sum); return 0; }