Question: What is the program to print the factorial of a number using recursion?
Answer: The C program to find the factorial of a given number using recursion is as follows:
// C program to find factorial // of a given number
#include <stdio.h> // Function to find factorial of // a given number
unsigned int factorial(unsigned int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
// Driver code
int main()
{
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
// Output: Factorial of 5 is 120
Question: What is the use of an extern storage specifier?
Answer: The extern storage specifier, which is short for external, is used to increase the visibility of C functions and variables. It is used by one file to access variables from another file.
Question: Write a program to check an Armstrong number.
Answer: The C program to determine whether the given number is an Armstrong number or not is as follows:
// C program to determine whether // the given number is Armstrong // or not
#include <stdio.h> // Driver code
int main()
{
int n;
printf("Enter Number \n");
scanf("%d", &n);
int var = n;
int sum = 0;
// Loop to calculate the order of // the given number
while (n > 0)
{
int rem = n % 10;
sum = (sum) + (rem * rem * rem);
n = n / 10;
}
// If the order of the number will be // equal to the number then it is // Armstrong number.
if (var == sum)
{
printf("%d is an Armstrong number \n", var);
}
else
{
printf("%d is not an Armstrong number", var);
}
return 0;
}
// Output example: Enter Number 0 is an Armstrong number
Question: Write a program to reverse a given number.
Answer: The C program to reverse digits of a number is as follows:
// C program to reverse digits // of a number
#include <stdio.h> // Driver code
int main()
{
int n, rev = 0;
printf("Enter Number to be reversed : ");
scanf("%d", &n);
// r will store the remainder while we // reverse the digit and store it in rev
int r = 0;
while (n != 0)
{
r = n % 10;
rev = rev * 10 + r;
n /= 10;
}
printf("Number After reversing digits is: %d", rev);
return 0;
}
// Output example: Enter Number to be reversed : 123 Number After reversing digits is: 321
…till next post, bye-bye & take care.
No comments:
Post a Comment