0% found this document useful (0 votes)
90 views4 pages

Factorial, GCD, Perfect Numbers Code

The document contains 3 programs to calculate factorial of a number using iteration, find the greatest common divisor (GCD) of two numbers without recursion, and find perfect numbers between two given numbers. Program 1 uses a for loop to calculate the factorial of a number entered by the user. Program 2 finds the GCD of two entered numbers by repeatedly taking the remainder when dividing the larger number by the smaller. Program 3 checks if each number from the range entered is equal to the sum of its positive divisors, and prints any perfect numbers found.

Uploaded by

Grace Hoagn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views4 pages

Factorial, GCD, Perfect Numbers Code

The document contains 3 programs to calculate factorial of a number using iteration, find the greatest common divisor (GCD) of two numbers without recursion, and find perfect numbers between two given numbers. Program 1 uses a for loop to calculate the factorial of a number entered by the user. Program 2 finds the GCD of two entered numbers by repeatedly taking the remainder when dividing the larger number by the smaller. Program 3 checks if each number from the range entered is equal to the sum of its positive divisors, and prints any perfect numbers found.

Uploaded by

Grace Hoagn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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();

You might also like