Write a C program to calculate the multiplication of first n
Write a C program to calculate the multiplication of first n numbers. Use a do-while statement.
EX: enter a positive interger : 5
the multiplication of the first 5 integers is 120.
(1*2*3*4*5=120)
Solution
Solution.c
#include <stdio.h>//header file for input output function
int main()
{//main function
int n, i, sum = 1;
do{//do while loop
printf(\"Enter a positive integer: \");
scanf(\"%d\",&n);//key board inputting
sum = 1;
for(i=1; i <= n; ++i)
{
sum *= i; // sum = sum*i;
}
printf(\"multiplication of %d natural numbers is %d \ \",n,sum);
}while(n>0);
printf(\"please enter only positive numbers\");
return 0;
}
output
sh-4.3$ main
Enter a positive integer: 5
multiplication of 5 natural numbers is 120
Enter a positive integer: 6
multiplication of 6 natural numbers is 720
Enter a positive integer: -1
multiplication of -1 natural numbers is 1
please enter only positive numbers
