KEMBAR78
C Programs Answers Part3 | PDF | Computer Programming | Numbers
0% found this document useful (0 votes)
6 views5 pages

C Programs Answers Part3

The document contains a collection of 150 categorized C programs, including examples for checking prime numbers, calculating sums of digits, and generating patterns. Each program is accompanied by its source code and demonstrates various programming concepts such as loops, conditionals, and arithmetic operations. The programs also cover topics like palindromes, strong numbers, and perfect numbers.

Uploaded by

ragineedas1
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)
6 views5 pages

C Programs Answers Part3

The document contains a collection of 150 categorized C programs, including examples for checking prime numbers, calculating sums of digits, and generating patterns. Each program is accompanied by its source code and demonstrates various programming concepts such as loops, conditionals, and arithmetic operations. The programs also cover topics like palindromes, strong numbers, and perfect numbers.

Uploaded by

ragineedas1
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/ 5

150 Categorized C Programs with Answers

Program 31: Check prime number

#include <stdio.h>
int main() {
int num = 29, flag = 0;
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number\n", num);
else
printf("%d is not a prime number\n", num);
return 0;
}

Program 32: Print all prime numbers in a range

#include <stdio.h>
int main() {
int low = 10, high = 50;
for (int i = low; i <= high; i++) {
int flag = 0;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
flag = 1;
break;
}
}
if (i > 1 && flag == 0)
printf("%d ", i);
}
return 0;
}

Program 33: Find sum of digits of a number

#include <stdio.h>
int main() {
int num = 1234, sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}

Program 34: Count even and odd digits in a number


150 Categorized C Programs with Answers
#include <stdio.h>
int main() {
int num = 123456, even = 0, odd = 0, digit;
while (num != 0) {
digit = num % 10;
if (digit % 2 == 0)
even++;
else
odd++;
num /= 10;
}
printf("Even digits: %d\nOdd digits: %d\n", even, odd);
return 0;
}

Program 35: Sum of even numbers from 1 to n

#include <stdio.h>
int main() {
int n = 20, sum = 0;
for (int i = 2; i <= n; i += 2)
sum += i;
printf("Sum of even numbers = %d\n", sum);
return 0;
}

Program 36: Sum of odd numbers from 1 to n

#include <stdio.h>
int main() {
int n = 20, sum = 0;
for (int i = 1; i <= n; i += 2)
sum += i;
printf("Sum of odd numbers = %d\n", sum);
return 0;
}

Program 37: Calculate power using loop

#include <stdio.h>
int main() {
int base = 3, exp = 4, result = 1;
for (int i = 1; i <= exp; i++)
result *= base;
printf("%d^%d = %d\n", base, exp, result);
return 0;
}

Program 38: Display ASCII values from 0 to 255

#include <stdio.h>
int main() {
150 Categorized C Programs with Answers
for (int i = 0; i <= 255; i++) {
printf("%d = %c\n", i, i);
}
return 0;
}

Program 39: Print pattern: half pyramid using *

#include <stdio.h>
int main() {
int i, j, rows = 5;
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j)
printf("* ");
printf("\n");
}
return 0;
}

Program 40: Print pattern: inverted pyramid *

#include <stdio.h>
int main() {
int i, j, rows = 5;
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j)
printf("* ");
printf("\n");
}
return 0;
}

Program 41: Print pattern: right aligned pyramid

#include <stdio.h>
int main() {
int i, j, space, rows = 5;
for (i = 1; i <= rows; ++i) {
for (space = 1; space <= rows - i; ++space)
printf(" ");
for (j = 1; j <= i; ++j)
printf("* ");
printf("\n");
}
return 0;
}

Program 42: Check number is palindrome using loop

#include <stdio.h>
int main() {
int num = 121, temp = num, rev = 0;
150 Categorized C Programs with Answers
while (temp != 0) {
rev = rev * 10 + temp % 10;
temp /= 10;
}
if (rev == num)
printf("Palindrome\n");
else
printf("Not palindrome\n");
return 0;
}

Program 43: Sum of factorials from 1 to n

#include <stdio.h>
int main() {
int n = 5, i, j, fact, sum = 0;
for (i = 1; i <= n; i++) {
fact = 1;
for (j = 1; j <= i; j++)
fact *= j;
sum += fact;
}
printf("Sum of factorials = %d\n", sum);
return 0;
}

Program 44: Check strong number

#include <stdio.h>
int main() {
int num = 145, temp = num, sum = 0;
while (temp > 0) {
int fact = 1, rem = temp % 10;
for (int i = 1; i <= rem; i++)
fact *= i;
sum += fact;
temp /= 10;
}
if (sum == num)
printf("Strong number\n");
else
printf("Not a strong number\n");
return 0;
}

Program 45: Check perfect number

#include <stdio.h>
int main() {
int num = 28, sum = 0;
for (int i = 1; i < num; i++) {
if (num % i == 0)
150 Categorized C Programs with Answers
sum += i;
}
if (sum == num)
printf("Perfect number\n");
else
printf("Not a perfect number\n");
return 0;
}

You might also like