22AIE101 C Program Practice Problems
22AIE101 C Program Practice Problems
22AIE101 C Program Practice Problems
int main() {
int n, sum = 0, i = 1;
printf("Enter a number: ");
scanf("%d", &n);
do {
sum += i;
i++;
} while (i <= n);
#include <stdio.h>
int main() {
for(int i = 2; i <= 50; i += 2) {
printf("%d ", i);
}
return 0;
}
3. Write a C program to calculate the factorial of a number.
#include <stdio.h>
int main() {
int n, fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
#include <stdio.h>
int main() {
for(int i = 100; i >= 1; i--) {
printf("%d ", i);
}
return 0;
}
6. Write a C program to find the sum of the squares of the first 5 positive integers.
#include <stdio.h>
int main() {
int sum = 0;
for(int i = 1; i <= 5; i++) {
sum += i * i;
}
printf("Sum of squares of first 5 numbers: %d\n", sum);
return 0;
}
7. Write a C program to count the number of digits in a number.
#include <stdio.h>
int main() {
int num, count = 0;
printf("Enter a number: ");
scanf("%d", &num);
while(num != 0) {
num /= 10;
count++;
}
printf("Number of digits: %d\n", count);
return 0;
}
#include <stdio.h>
int main() {
int num, sum = 0;
for(int i = 1; i <= 10; i++) {
printf("Enter number %d: ", i);
scanf("%d", &num);
sum += num;
}
printf("Average: %.2f\n", sum / 10.0);
return 0;
}
9. Write a C program to print all lowercase alphabets from 'a' to 'z'.
#include <stdio.h>
int main() {
for(char ch = 'a'; ch <= 'z'; ch++) {
printf("%c ", ch);
}
return 0;
}
10. Write a C program to find the least common multiple (LCM) of two numbers.
#include <stdio.h>
int main() {
int a, b, max;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
max = (a > b) ? a : b;
while(1) {
if(max % a == 0 && max % b == 0) {
printf("LCM of %d and %d is %d\n", a, b, max);
break;
}
max++;
}
return 0;
}
11. Write a C program to find the greatest common divisor (GCD) of two numbers.
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
while(a != b) {
if(a > b)
a -= b;
else
b -= a;
}
#include <stdio.h>
int main() {
int num, max;
printf("Enter number 1: ");
scanf("%d", &num);
max = num;
13. Write a C program to print the first 10 numbers of the Fibonacci sequence.
#include <stdio.h>
int main() {
int n1 = 0, n2 = 1, next, i;
printf("Fibonacci sequence: ");
for(i = 1; i <= 10; i++) {
printf("%d ", n1);
next = n1 + n2;
n1 = n2;
n2 = next;
}
return 0;
}
14. Write a C program to find the sum of the digits of a number.
#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
while(num != 0) {
sum += num % 10;
num /= 10;
}
#include <stdio.h>
int main() {
int n, i = 2, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &n);
if(n <= 1) {
isPrime = 0;
} else {
while(i <= n / 2) {
if(n % i == 0) {
isPrime = 0;
break;
}
i++;
}
}
if(isPrime)
printf("%d is a prime number.\n", n);
else
printf("%d is not a prime number.\n", n);
return 0;
}
#include <stdio.h>
int main() {
int num, reversed = 0;
printf("Enter a number: ");
scanf("%d", &num);
while(num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
#include <stdio.h>
int main() {
int num, original, remainder, result = 0;
printf("Enter a 3-digit number: ");
scanf("%d", &num);
original = num;
while(original != 0) {
remainder = original % 10;
result += remainder * remainder * remainder;
original /= 10;
}
if(result == num)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);
return 0;
}
18. Write a C program to check if a given number is a perfect number.
#include <stdio.h>
int main() {
int num, sum = 0;
return 0;
}
#include <stdio.h>
int main() {
int num, original, reversed = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
do {
digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
} while (num > 0);
if (original == reversed)
printf("Palindrome\n");
else
printf("Not a Palindrome\n");
return 0;
}
20 Write a C program to find the power of a number (without using pow function).
#include <stdio.h>
int main() {
int base, exponent, result = 1, i = 0;
printf("Enter base and exponent: ");
scanf("%d %d", &base, &exponent);
do {
result *= base;
i++;
} while (i < exponent);
#include <stdio.h>
int main() {
int n, i = 1, sum = 0;
printf("Enter the value of N: ");
scanf("%d", &n);
do {
if (i % 2 != 0) {
sum += i;
}
i++;
} while (i <= n);
return 0;
}
int main() {
int rows;
return 0;
}
3) Full Pyramid
#include <stdio.h>
int main() {
int rows = 5; // Set the number of rows
return 0;
}
#include <stdio.h>
int main()
{
int rows = 5;
#include <stdio.h>
int main()
{
int rows = 5;
return 0;
}
#include <stdio.h>
int main() {
int rows;
return 0;
}
7) Rhombus Pattern
#include <stdio.h>
int main() {
int rows = 4;
return 0;
}
8) Diamond Pattern
// C program to print the Diamond pattern of stars
#include <stdio.h>
int main() {
int n = 4; // Number of rows for the diamond
int main() {
int n = 5; // Number of rows for the hourglass
int main()
{
int rows = 5;
int main()
{
int rows = 5;
// first outer loop to iterate through each loop
for (int i = 0; i < rows; i++) {
// first inner loop to print leading whitespaces
for (int j = 0; j < 2 * (rows - i) - 1; j++) {
printf(" ");
}
// second inner loop to print stars * and inner whitespaces
for (int k = 0; k < 2 * i + 1; k++) {
if (k == 0 || k == 2 * i || i == rows - 1) {
printf("* ");
}
else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int rows = 5;
#include <stdio.h>
int main()
{
int n = 5;
#include <stdio.h>
int main()
{
int n = 5;
// assigning comparator
int comp;
if (i < n) {
comp = 2 * i + 1;
}
else {
comp = 2 * (2 * n - i) - 3;
}
#include <stdio.h>
int main()
{
int rows = 4;
int n = 1;
#include <stdio.h>
int main()
{
int rows = 5;
int C = 1; // coefficient