Thanks to visit codestin.com
Credit goes to www.scribd.com

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

C Programs Prime Factorial Series

The document contains multiple C programs that perform various mathematical operations. These include checking if a number is prime, calculating factorials, summing natural numbers, even and odd numbers, generating prime numbers, and summing squares. Each program prompts the user for input and displays the result accordingly.

Uploaded by

Aditya kumar
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)
12 views4 pages

C Programs Prime Factorial Series

The document contains multiple C programs that perform various mathematical operations. These include checking if a number is prime, calculating factorials, summing natural numbers, even and odd numbers, generating prime numbers, and summing squares. Each program prompts the user for input and displays the result accordingly.

Uploaded by

Aditya kumar
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
You are on page 1/ 4

12.

Check whether a given number is prime or not

#include <stdio.h>

int main() {
int num, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &num);

if (num <= 1) {
isPrime = 0;
} else {
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}

if (isPrime)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);

return 0;
}

13. Factorial of a given number

#include <stdio.h>

int main() {
int num, i;
unsigned long long factorial = 1;

printf("Enter a number: ");


scanf("%d", &num);

for (i = 1; i <= num; i++) {


factorial *= i;
}

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

return 0;
}

14. Sum of first n natural numbers (1 + 2 + ... + n)

#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a value for n: ");
scanf("%d", &n);

sum = n * (n + 1) / 2;
printf("Sum = %d\n", sum);

return 0;
}

15. Sum of even numbers (2 + 4 + ... + n)

#include <stdio.h>

int main() {
int n, sum = 0, i;
printf("Enter the value of n: ");
scanf("%d", &n);

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

printf("Sum of even numbers up to %d = %d\n", n, sum);


return 0;
}

16. Sum of odd numbers (1 + 3 + ... + n)

#include <stdio.h>

int main() {
int n, sum = 0, i;
printf("Enter the value of n: ");
scanf("%d", &n);

for (i = 1; i <= n; i += 2) {
sum += i;
}

printf("Sum of odd numbers up to %d = %d\n", n, sum);


return 0;
}

17. Series 1 - 2 + 3 - 4 + 5 - 6 ... ±n

#include <stdio.h>

int main() {
int n, i, sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);

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


if (i % 2 == 0)
sum -= i;
else
sum += i;
}

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


return 0;
}

18. Sum of squares: 1 + 4 + 9 + ... + n^2

#include <stdio.h>

int main() {
int n, i, sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);

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


sum += i * i;
}

printf("Sum of squares = %d\n", sum);


return 0;
}

19. Generate prime numbers from 2 to N

#include <stdio.h>

int main() {
int n, i, j, isPrime;
printf("Enter the value of N: ");
scanf("%d", &n);

printf("Prime numbers from 2 to %d are:\n", n);


for (i = 2; i <= n; i++) {
isPrime = 1;
for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime)
printf("%d ", i);
}
printf("\n");
return 0;
}

20. Generate factorials of numbers from 0 to N

#include <stdio.h>

int main() {
int n, i, j;
unsigned long long fact;

printf("Enter the value of N: ");


scanf("%d", &n);

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


fact = 1;
for (j = 1; j <= i; j++) {
fact *= j;
}
printf("Factorial of %d = %llu\n", i, fact);
}

return 0;
}

You might also like