Practice Program
Solution: through for loop
void main()
{
int n,i,product = 1;;
printf("Product of how many numbers: ");
scanf("%d",&n);
//at every iteration we are assigning
//(current_product_value * i ) to new value of product.
for(i=1;i<=n;i++)
{
product = product*i;
}
printf("Product of first %d natural numbers are %d",n,product);
}
void main() { int n,i,product = 1;; printf("Product of how many numbers: "); scanf("%d",&n); //at every iteration we are assigning //(current_product_value * i ) to new value of product. for(i=1;i<=n;i++) { product = product*i; } printf("Product of first %d natural numbers are %d",n,product); }
Output1
Product of how many numbers:5
Product of first 5 natural numbers are 120.
Product of first 5 natural numbers are 120.
Output2
product of how many numbers:10
Product of first 10 natural numbers are 3628800.
Product of first 10 natural numbers are 3628800.
Output3
Product of how many numbers:15
Product of first 15 natural numbers are 2004310016.
Product of first 15 natural numbers are 2004310016.
Solution : through while loop
void main()
{
int n,i,product = 1;;
printf("Product of how many numbers: ");
scanf("%d",&n);
i=1;
//at every iteration we are assigning
//(current_value + i ) to new value of sum.
while(i<=n)
{
product = product*i;
i++;
}
printf("Product of first %d natural numbers are %d",n,product);
}
void main() { int n,i,product = 1;; printf("Product of how many numbers: "); scanf("%d",&n); i=1; //at every iteration we are assigning //(current_value + i ) to new value of sum. while(i<=n) { product = product*i; i++; } printf("Product of first %d natural numbers are %d",n,product); }
Solution:from do-while loop
void main()
{
int n,i,product = 1;;
printf("Product of how many numbers: ");
scanf("%d",&n);
i=1;
//at every iteration we are assigning
//(current_product_value + i ) to new value of product.
do
{
product = product*i;
i++;
}while(i<=n)
printf("Product of first %d natural numbers are %d",n,product);
}
void main() { int n,i,product = 1;; printf("Product of how many numbers: "); scanf("%d",&n); i=1; //at every iteration we are assigning //(current_product_value + i ) to new value of product. do { product = product*i; i++; }while(i<=n) printf("Product of first %d natural numbers are %d",n,product); }
defining a function 'product' which will return product of n numbers.
int product(int n)
{
int i,product=1;
//at every iteration we are assigning
//(current_product_value * i ) to new value of product.
for(i=1;i<=n;i++)
{
product = product*i;
}
return product;
}
int product(int n) { int i,product=1; //at every iteration we are assigning //(current_product_value * i ) to new value of product. for(i=1;i<=n;i++) { product = product*i; } return product; }
Using above define function 'product':
void main()
{
int n,result;
printf("How many numbers of product you want: ");
scanf("%d",&n);
result = product(5);
printf("product of first %d numbers are %d",n,result);
}
void main() { int n,result; printf("How many numbers of product you want: "); scanf("%d",&n); result = product(5); printf("product of first %d numbers are %d",n,result); }
Output1
How many numbers of product you want: 5
product of first 5 natural numbers are 15.
product of first 5 natural numbers are 15.
Using above define function 'product':
void main()
{
int n,result;
printf("How many numbers of product you want: ");
scanf("%d",&n);
result = product(10);
printf("productof first %d numbers are %d",n,result);
}
void main() { int n,result; printf("How many numbers of product you want: "); scanf("%d",&n); result = product(10); printf("productof first %d numbers are %d",n,result); }
Output2
How many numbers of product you want: 10
product of first 10 natural numbers are 3628800.
product of first 10 natural numbers are 3628800.
Using above define function 'product':
void main()
{
int n,result;
printf("How many numbers of sum you want: ");
scanf("%d",&n);
result = product(15);
printf("product of first %d numbers are %d",n,result);
}
void main() { int n,result; printf("How many numbers of sum you want: "); scanf("%d",&n); result = product(15); printf("product of first %d numbers are %d",n,result); }
Output3
How many numbers of product you want: 15
product of first 15 natural numbers are 2004310016.
product of first 15 natural numbers are 2004310016.
0 Comments
Post a Comment