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

C Lab Programs

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

C Lab Programs

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

1) Write A C Program To Find Sum Of Individual Digits Of Positive Integer

AIM: To find Sum Of Individual Digits Of given Positive Integer

PROGRAM:

#include <stdio.h>

#include <conio.h>

int main() {

int n, s = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

while (n > 0) {

s += n % 10;

n /= 10;

printf("Sum = %d\n", s);

getch();

OUTPUT:

Enter a positive integer: 987

Sum = 24
2. write a c program to generate all the prime numbers between 1 and n where n is a value
supplied by user

Aim: To Generate All The Prime Numbers Between 1 And N Where N Is A Value Supplied By User

PROGRAM:

#include <stdio.h>

#include <conio.h>

int main() {

int n, i, j, prime;

printf("Enter n: ");

scanf("%d", &n);

for (i = 2; i <= n; i++) {

prime = 1;

for (j = 2; j <= i / 2; j++)

if (i % j == 0) { prime = 0; break; }

if (prime) printf("%d ", i);

getch();

Output:

Enter n: 30

2 3 5 7 11 13 17 19 23 29
Write a c program to find the factorial of given integer

Aim: To Find The Factorial Of a Given Integer

Program:

#include <stdio.h>

#include <conio.h>

int factorial(int n)

if (n == 0) // base condition

return 1;

else

return n * factorial(n - 1); // recursive call

int main()

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("Factorial of %d is %d\n", num, factorial(num));

return 0;

Output:

Enter a number :5

Factorial of 5 is 120
4. Write a c program to find the GCD of two given integers

Aim: To Find GCD of two given integers

Program

#include <stdio.h>

#include <conio.h>

int main() {

int num1, num2;

// Input two integers

printf("Enter two integers: ");

scanf("%d %d", &num1, &num2);

int a = num1, b = num2;

// Ensure both numbers are positive

if (a < 0) a = -a;

if (b < 0) b = -b;

// Euclidean algorithm

while (b != 0) {

int temp = b;

b = a % b;

a = temp;

printf("GCD of %d and %d = %d\n", num1, num2, a);

getch();

Output

Enter two integers: 56 98

GCD of 56 and 98 = 14

You might also like