Skip to main content

Posts

Showing posts from May 25, 2020

for loop Programs In C

for loop Programs In C:Q1. Write a C program to find factorial of a number by accepting the number from user. Ans. #include<stdio.h> #include<conio.h> void main() { int n, i, fact=1; clrscr(); printf("\n enter the number for finding factorial"); scanf("%d",&n); for(i=1;i<=n;i++) { fact=fact*i; } printf("\n the factorial of %d is %d",n,fact); getch(); } Output:enter the number for finding factorial 5 the factorial of 5 is 120 Explanation of the above Code:First of all What is factorial? Ans. The symbol of factorial is ! here are example which will give you idea what is factorial ex. 5!=5*4*3*2*1 3!=3*2*1 Now you have the idea what is factorial. In the above code we have declared 3 variables of datatype integer i for the loop, fact=1 for calculating the factorial, n for input.When the loop gets started with i=1 it checks the condition first i.e whether i is less than n (1<=5) which in this case is true since the initial value of i is 1 and the value of n is 5.Afte…