C Interview Questions

Write a program to check Armstrong number in C?

Post Views: 48,052 #include #include main() { int n,r,sum=0,temp; //declaration of variables. clrscr(); //It clears the screen. 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(); //It reads a character from the keyword. }

Read More

Write a program to print factorial of given number using recursion?

Post Views: 48,026 #include #include long factorial(int n) // function to calculate the factorial of a given number. { if (n == 0) return 1; else return(n * factorial(n-1)); //calling the function recursively. } void main() { int number; //declaration of variables. long fact; clrscr(); printf(“Enter a number: “); scanf(“%d”, &number); fact = factorial(number); //calling a function. printf(“Factorial of %d is %ld\n”, number, fact); getch(); //It reads a character from the keyword. }

Read More

Write a program to check prime number in C Programming?

Post Views: 47,978 #include #include void main() { int n,i,m=0,flag=0; //declaration of variables. clrscr(); //It clears the screen. 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; //break keyword used to terminate from the loop. } } if(flag==0) printf("Number is prime"); getch(); //It reads a character from the keyword. }

Read More

Write a program to print Fibonacci series using recursion?

Post Views: 47,972 #include #include void printFibonacci(int n) // function to calculate the fibonacci series of a given number. { static int n1=0,n2=1,n3; // declaration of static variables. if(n>0){ n3 = n1 + n2; n1 = n2; n2 = n3; printf(“%d “,n3); printFibonacci(n-1); //calling the function recursively. } } 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(); }

Read More