1
PRACTICAL SHEET-3
Program 8. To compute the factorial of a number using iteration
#include<stdio.h>
#include<conio.h>
void main()
int k, kfact, i;
clrscr();
printf("Enter the number");
scanf("%d", &k);
kfact=1;
for(i=1;i<=k;i++)
kfact=kfact*i;
printf("\n factorial of %d is %d", k, kfact);
getch();
Program 9. To find the GCD of two numbers without recursion.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
2
int x, y, nr, dr, r;
clrscr();
printf("Enter the two numbers:\n");
scanf("%d %d",&x, &y);
if(x>=y)
nr=x;
dr=y;
else
nr=y;
dr=x;
r=nr%dr;
while(r!=0)
nr=dr;
dr=r;
r=nr%dr;
3
printf("The GCD is %d", dr);
getch();
Program 10. To find the perfect numbers between num1 and num2
[A perfect number is a positive integer that is equal to the sum of its positive divisors,
excluding the number itself. For instance, 6 has divisors 1, 2 and 3 (excluding itself), and 1
+ 2 + 3 = 6, so 6 is a perfect number.
The first few perfect numbers are 6, 28, 496 and 8128.]
#include<stdio.h>
#include<conio.h>
void main()
int num1, num2;
int n, i, sum;
clrscr();
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the last number: ");
scanf("%d", &num2);
printf("The perfect numbers within the given range are: \n");
4
for(n=num1;n<=num2;n++)
i=1;
sum=0;
while(i<n)
if(n%i==0)
sum=sum+i;
i++;
if(sum==n)
printf("%d \n",n);
getch();