Question-131. Write a program to swap two numbers without using third variable ?
Answer-
#include #include main() { int a=10, b=20; clrscr(); printf("Before swap a=%d b=%d",a,b); a=a+b;//a=30 (10+20) b=a-b;//b=10 (30-20) a=a-b;//a=20 (30-10) printf("\nAfter swap a=%d b=%d",a,b); getch(); }
Question-132. Write a program to print fibonacci series without using recursion ?
Answer-
#include #include void main() { int n1=0,n2=1,n3,i,number; clrscr(); printf("Enter the number of elements:"); scanf("%d",&number); printf("\n%d %d",n1,n2);//printing 0 and 1 for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed { n3=n1+n2; printf(" %d",n3); n1=n2; n2=n3; } getch(); }
Question-133. Write a program to print fibonacci series using recursion ?
Answer-
#include #include void printFibonacci(int n){ static int n1=0,n2=1,n3; if(n>0){ n3 = n1 + n2; n1 = n2; n2 = n3; printf("%d ",n3); printFibonacci(n-1); } } void main(){ int n; clrscr(); printf("Enter the number of elements: "); scanf("%d",&n); printf("Fibonacci Series: "); printf("%d %d ",0,1); printFibonacci(n-2);//n-2 because 2 numbers are already printed getch(); }
Question-134. Write a program to check prime number in C Programming ?
Answer-
#include #include void main() { int n,i,m=0,flag=0; clrscr(); printf("Enter the number to check prime:"); scanf("%d",&n); m=n/2; for(i=2;i<=m;i++) { if(n%i==0) { printf("Number is not prime"); flag=1; break; } } if(flag==0) printf("Number is prime"); getch(); }
Question-135. a program to check palindrome number in C Programming ?
Answer-
#include #include main() { int n,r,sum=0,temp; clrscr(); printf("enter the number="); scanf("%d",&n); temp=n; while(n>0) { r=n%10; sum=(sum*10)+r; n=n/10; } if(temp==sum) printf("palindrome number "); else printf("not palindrome"); getch(); }
Question-136. Write a program to print factorial of given number without using recursion ?
Answer-
#include #include void main(){ int i,fact=1,number; clrscr(); printf("Enter a number: "); scanf("%d",&number); for(i=1;i<=number;i++){ fact=fact*i; } printf("Factorial of %d is: %d",number,fact); getch(); }
Question-137. Write a program to print factorial of given number using recursion ?
Answer-
#include #include long factorial(int n) { if (n == 0) return 1; else return(n * factorial(n-1)); } void main() { int number; long fact; clrscr(); printf("Enter a number: "); scanf("%d", &number); fact = factorial(number); printf("Factorial of %d is %ld\n", number, fact); getch(); }
Question-138. Write a program to check armstrong number in C ?
Answer-
#include #include main() { int n,r,sum=0,temp; clrscr(); printf("enter the number="); scanf("%d",&n); temp=n; while(n>0) { r=n%10; sum=sum+(r*r*r); n=n/10; } if(temp==sum) printf("armstrong number "); else printf("not armstrong number"); getch(); }
Question-139. Write a program to reverse a given number in C ?
Answer-
#include #include main() { int n, reverse=0, rem; clrscr(); printf("Enter a number: "); scanf("%d", &n); while(n!=0) { rem=n%10; reverse=reverse*10+rem; n/=10; } printf("Reversed Number: %d",reverse); getch(); }