Problem Statement-

Write a program containing methods that perform each of the following functions. In each case choose the most appropriate iterative control structure (i.e. for, while or dowhile loop) to perform the function.

(a) The factorial of a number n is defined to n x n-1 x….x1. For example, the factorial of the number 3 is 6 and the factorial of the number 4 is 24. Note that the factorial of 0 is 1.

Write a method that takes a number n as input (value is passed to method) and

calculates and returns the factorial of the number.

(b) Write a method that inputs (from the keyboard) a number of single positive digits and

combines them into an integer value. The user will enter a negative number to indicate

the end of the digits. For example, inputting 234 -1 should result in the program outputting 234.

(c) Write a method that takes two integer values as input (passed to the method) and finds the highest common factor (HCF) of the two values. The HCF is calculated by iteratively subtracting the smaller number from the larger number until both the numbers are equal. This value should then be returned as the HCF of both the numbers. For example, the HCF of 2 and 4 is 2 while the HCF of 3 and 4 is 1.

#include<stdio.h>
#include<math.h>
void fact(void);
void combine(void);
void hcf(void);
void fact()
{
long long n;
printf("Enter any no.\n");
scanf("%lld",&n);
long long f=1;
if(n==1||n==0)
printf("%lld\n",n);
else
{
while(n>1)
{
f=f*n;
n--;
}
printf("%lld\n",f);
}
}
void combine()
{
long long i=0,t,a[10],l,n=0;
printf("Enter nos.");
do
{
scanf("%lld",&a[i]);
i++;
}
while(a[i-1]>0);
t=i-1;
l=t;
for(i=0;i<t;i++)
{
n=n+*(a+i)*pow(10,l-1);
l--;
}
printf("%lld\n",n);
}
void hcf()
{
int a,b;
printf("enter any two nos.\n");
scanf("%d%d",&a,&b);
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
printf("hcf is %d\n",a);
}
int main()
{
fact();
combine();
hcf();
return 0;
}

### Like this:

Like Loading...

*Related*