22AIE101 C Program Practice Problems

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Amrita Vishwa Vidyapeetham

Amrita School of Artificial Intelligence, Coimbatore


First Semester – CSE-AI – A & B batch
22AIE101 – Problem Solving & C Programming

Practice Problems: for loop, while loop, do while loop


Q.No. Question

Write a C program to find the sum of the first N natural numbers.


1.
#include <stdio.h>

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

do {
sum += i;
i++;
} while (i <= n);

printf("Sum of first %d natural numbers: %d\n", n, sum);


return 0;
}
2. Write a C program to print all even numbers between 1 and 50.

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

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


fact *= i;
}
printf("Factorial of %d is %d\n", n, fact);
return 0;
}
4. Write a C program that prints the multiplication table of a given number.

#include <stdio.h>

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

for(int i = 1; i <= 10; i++) {


printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}

5. Write a C program to print numbers from 50 to 1 in reverse order.

#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;
}

8. Write a C program to input 10 numbers and find their average.

#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;
}

printf("GCD is %d\n", a);


return 0;
}
12. Write a C program that takes 5 integers from the user and finds the maximum value.

#include <stdio.h>

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

for(int i = 2; i <= 5; i++) {


printf("Enter number %d: ", i);
scanf("%d", &num);
if(num > max) {
max = num;
}
}

printf("Maximum value: %d\n", max);


return 0;
}

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;
}

printf("Sum of digits: %d\n", sum);


return 0;
}
15. Write a C program to check if a given number is prime.

#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;
}

16. Write a C program to reverse a number.

#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;
}

printf("Reversed number: %d\n", reversed);


return 0;
}

17. Write a C program to check if a number is an Armstrong number (e.g., 153).

#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;

printf("Enter a number: ");


scanf("%d", &num);

for(int i = 1; i <= num / 2; i++) {


if(num % i == 0) {
sum += i;
}
}

if(sum == num && num != 0) {


printf("%d is a perfect number.\n", num);
} else {
printf("%d is not a perfect number.\n", num);
}

return 0;
}

19. Write a C program to check whether a given number is Palindrome or not.

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

printf("%d^%d = %d\n", base, exponent, result);


return 0;
}

21 Write a C program to find the sum of odd numbers between 1 and N.

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

printf("Sum of odd numbers between 1 and %d: %d\n", n, sum);


return 0;
}
Patterns - Program in C
1) Right Half Pyramid

// C program to print right half pyramid pattern of star


#include <stdio.h>
int main() {
int i, j, rows;

// Enter the number of rows


printf("Enter the number of rows: ");
scanf("%d", &rows);

// first loop for printing rows


for(i = 1; i <= rows; i++) {
// second loop (inner loop) for printing character star in each row
for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}

return 0;
}

2) Left Half Pyramid

// C program to print left half pyramid pattern of star


#include <stdio.h>

int main() {
int rows;

// Enter the number of rows


printf("Enter the number of rows: ");
scanf("%d", &rows);

for (int i = 1; i <= rows; i++) {


// Print leading spaces
for (int j = 0; j < rows - i; j++) {
printf(" "); // Two spaces for alignment
}
// Print stars
for (int k = 1; k <= i; k++) {
printf("* ");
}
printf("\n");
}

return 0;
}
3) Full Pyramid

// C program to print left half pyramid pattern of star

#include <stdio.h>

int main() {
int rows = 5; // Set the number of rows

for (int i = 1; i <= rows; i++) {


// Create leading spaces for centering the stars
for (int j = rows - i; j > 0; j--) {
printf(" "); // Print leading spaces
}

for (int k = 1; k <= i; k++) {


printf("* "); // Print i stars in each row with a space
}

printf("\n"); // Move to the next line


}

return 0;
}

4) Inverted Right Half Pyramid

// C program to print the inverted right half pyramid of stars

#include <stdio.h>

int main()
{
int rows = 5;

// first loop to print all rows


for (int i = 0; i < rows; i++) {

// first inner loop to print the * in each row


for (int j = 0; j < rows - i; j++) {
printf("* ");
}
printf("\n");
}
}
5) Inverted Left Half Pyramid

// C program to print the inverted left half pyramid pattern of stars

#include <stdio.h>

int main()
{
int rows = 5;

// first loop for printing all rows


for (int i = 0; i < rows; i++) {

// first inner loop for printing white spaces


for (int j = 0; j < 2 * i; j++) {
printf(" ");
}

// second inner loop for printing star *


for (int k = 0; k < rows - i; k++) {
printf("* ");
}
printf("\n");
}

return 0;
}

6) Inverted Full Pyramid

// C program to print the inverted full pyramid pattern of stars

#include <stdio.h>

int main() {
int rows;

printf("Enter the number of rows: ");


scanf("%d", &rows);

// Loop for each row


for (int i = rows; i >= 1; i--) {
// Print leading spaces
for (int j = 0; j < rows - i; j++) {
printf(" ");
}
// Print stars for the current row
for (int k = 1; k <= i; k++) {
printf("* ");
}
// Move to the next line after printing stars
printf("\n");
}

return 0;
}
7) Rhombus Pattern

// C program to print the Rhombus pattern of stars

#include <stdio.h>

int main() {
int rows = 4;

// First outer loop for the upper part of the rhombus


for (int i = 0; i < rows; i++) {
// Print leading spaces
for (int j = 0; j < i; j++) {
printf(" ");
}
// Print stars for the current row
for (int k = 0; k < rows; k++) {
printf("* ");
}
printf("\n");
}

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

// Print upper half of the diamond


for (int i = 1; i <= n; i++) {
// Print leading spaces
for (int j = 0; j < n - i; j++) {
printf(" ");
}
// Print stars equal to the row number
for (int k = 1; k <= i; k++) {
printf("* ");
}
printf("\n"); // Move to the next line
}
// Print lower half of the diamond
for (int i = n - 1; i >= 1; i--) {
// Print leading spaces
for (int j = 0; j < n - i; j++) {
printf(" ");
}
// Print stars equal to the row number
for (int k = 1; k <= i; k++) {
printf("* ");
}
printf("\n"); // Move to the next line
}
return 0;
}
// C program to print the Hourglass pattern of stars
9) Hour Glass Pattern
#include <stdio.h>

int main() {
int n = 5; // Number of rows for the hourglass

// Print upper half of the hourglass


for (int i = 0; i < n; i++) {
// Print leading spaces
for (int j = 0; j < i; j++) {
printf(" ");
}
// Print stars
for (int k = 0; k < n - i; k++) {
printf("* ");
}
printf("\n"); // Move to the next line
}
// Print lower half of the hourglass
for (int i = 1; i < n; i++) {
// Print leading spaces
for (int j = n - i - 1; j > 0; j--) {
printf(" ");
}
// Print stars
for (int k = 0; k < i + 1; k++) {
printf("* ");
}
printf("\n"); // Move to the next line
}
return 0;
}

// C program to print the Hollow square pattern of stars


10) Hollow Square Pattern #include <stdio.h>

int main()
{
int rows = 5;

// outer loop to iterator through each row


for (int i = 0; i < rows; i++) {

// inner loop to print * star in each row


for (int j = 0; j < rows; j++) {
// statement to check boundary condition
if (i > 0 && i < rows - 1 && j > 0
&& j < rows - 1) {
printf(" ");
}
else {
printf("* ");
}
}
printf("\n");
}
return 0;
}
11) Hollow Full Pyramid // C program to print hollow full pyramid pattern of star
#include <stdio.h>

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;
}

12) Hollow full inverted Pyramid

// C Program to print hollow full pyramid pattern using star

#include <stdio.h>

int main()
{
int rows = 5;

// first loop iterating through each row


for (int i = 0; i < rows; i++) {

// first inner loop to print leading white space


for (int j = 0; j < 2 * i + 1; j++) {
printf(" ");
}

// second inner loop to print star* and hollow white space


for (int k = 0; k < 2 * (rows - i) - 1; k++) {
if (k == 0 || k == 2 * (rows - i) - 2 || i == 0)
printf("* ");
else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
13) Hollow Diamond Pyramid
// C Program to print the hollow diamond pattern using star

#include <stdio.h>
int main()
{
int n = 5;

// first outer loop to iterator through each row


for (int i = 0; i < 2 * n - 1; i++) {
// assigning values to the comparator according to row number
int comp;
if (i < n) {
comp = 2 * (n - i) - 1;
}
else {
comp = 2 * (i - n + 1) + 1;
}

// first inner loop to print leading whitespaces


for (int j = 0; j < comp; j++) {
printf(" ");
}

// second inner loop to print star * and inner whitespaces


for (int k = 0; k < 2 * n - comp; k++) {
if (k == 0 || k == 2 * n - comp - 1) {
printf("* ");
}
else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
14) Hollow hourglass pattern

// C Program to print the hourglass pattern using star

#include <stdio.h>
int main()
{
int n = 5;

// first outer loop to iterate through each row


for (int i = 0; i < 2 * n - 1; i++) {

// assigning comparator
int comp;
if (i < n) {
comp = 2 * i + 1;
}
else {
comp = 2 * (2 * n - i) - 3;
}

// first inner loop to print leading whitespaces


for (int j = 0; j < comp; j++) {
printf(" ");
}

// second inner loop to print star * and inner whitespaces


for (int k = 0; k < 2 * n - comp; k++) {
if (k == 0 || k == 2 * n - comp - 1 || i == 0 || i == 2 * n - 2) {
printf("* ");
}
else {
printf(" ");
}
}
printf("\n");
}
return 0;
}

15) Floyd’s Triangle


// C Program to print the Floyd's Triangle

#include <stdio.h>

int main()
{
int rows = 4;
int n = 1;

// outer loop to print all rows


for (int i = 0; i < rows; i++) {

// inner loop to print alphabet in each row


for (int j = 0; j <= i; j++) {
printf("%d ", n++);
}
printf("\n");
}
return 0;
}
16) Pascal’s Triangle
// C program to print the pascal's triangle pattern

#include <stdio.h>

int main()
{
int rows = 5;

// outer loop for rows


for (int i = 1; i <= rows; i++) {

// inner loop 1 for leading white spaces


for (int j = 0; j < rows - i; j++) {
printf(" ");
}

int C = 1; // coefficient

// inner loop 2 for printing numbers


for (int k = 1; k <= i; k++) {
printf("%d ", C);
C = C * (i - k) / k;
}
printf("\n");
}
return 0;
}

You might also like