C Programming language

adplus-dvertising
C Program example: Input a number n and print multiplication from 1 to n
Previous Home Next

In this example we are going to print multiplication from 1 to n .Here n is number which user will enter at run time. To find multiplication we are using for loop. for loop will iterate from 1 to n and multiply values. Here we have define a variable f and assign 1 into f .In for loop we multiply the values with f and assign the values into f. When for loop will end then the value of multiplications of values from 1 to n will store into f. At last we print value of the f .This is final result.

/*input a no and print the following 1*2*3*4*5*6......*n */

#include<stdio.h>
#include<conio.h>
void main()
{
	int n,i,f=1;
	clrscr();
	printf("Enter the no ");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		f=f*i;
	printf("The multiplication from 1 to %d =%d",n,f);
	getch();
}
Output:

Enter the no3
The multiplication from 1 to3=6

Previous Home Next