0% found this document useful (0 votes)
33 views22 pages

30 Beginner C Programming Exercises With Solutions

The document contains 30 beginner C programming exercises, each with a description and corresponding code solution. Exercises cover fundamental concepts such as printing output, arithmetic operations, control structures, and basic data handling. The exercises are designed to help beginners practice and understand C programming through practical examples.

Uploaded by

oddball
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)
33 views22 pages

30 Beginner C Programming Exercises With Solutions

The document contains 30 beginner C programming exercises, each with a description and corresponding code solution. Exercises cover fundamental concepts such as printing output, arithmetic operations, control structures, and basic data handling. The exercises are designed to help beginners practice and understand C programming through practical examples.

Uploaded by

oddball
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/ 22

30 Beginner C Programming Exercises with Solutions

1. Hello World
Print "Hello, World!" to the console.

#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}

2. Sum of Two Numbers


Take two integers as input and display their sum.

#include <stdio.h>

int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

int sum = num1 + num2;


printf("Sum: %d\n", sum);
return 0;
}

3. Check Even or Odd


Determine if a number is even or odd.
c

#include <stdio.h>

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

if (num % 2 == 0)
printf("%d is even.\n", num);
else
printf("%d is odd.\n", num);
return 0;
}

4. Find Maximum of Three Numbers


Find the largest among three given numbers.

#include <stdio.h>

int main() {
int a, b, c, max;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);

max = a;
if (b > max) max = b;
if (c > max) max = c;

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


return 0;
}

5. Check Prime Number


Check if a number is prime.
c

#include <stdio.h>

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

if (num <= 1) isPrime = 0;

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

6. Factorial Calculation
Calculate the factorial of a number.
c

#include <stdio.h>

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

printf("Enter a number: ");


scanf("%d", &num);

if (num < 0)
printf("Factorial is not defined for negative numbers.\n");
else {
for (i = 1; i <= num; i++)
fact *= i;
printf("Factorial of %d = %llu\n", num, fact);
}
return 0;
}

7. Fibonacci Series
Print Fibonacci series up to n terms.

#include <stdio.h>

int main() {
int n, i, t1 = 0, t2 = 1, nextTerm;

printf("Enter number of terms: ");


scanf("%d", &n);

printf("Fibonacci Series: ");


for (i = 1; i <= n; i++) {
printf("%d ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}

8. Reverse a Number
Reverse the digits of a number.

#include <stdio.h>

int main() {
int num, reversed = 0, remainder;

printf("Enter a number: ");


scanf("%d", &num);

while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

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


return 0;
}

9. Check Palindrome Number


Check if a number is a palindrome.
c

#include <stdio.h>

int main() {
int num, originalNum, reversed = 0, remainder;

printf("Enter a number: ");


scanf("%d", &num);

originalNum = num;

while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

if (originalNum == reversed)
printf("%d is a palindrome.\n", originalNum);
else
printf("%d is not a palindrome.\n", originalNum);
return 0;
}

10. Sum of Digits


Calculate the sum of digits of a number.
c

#include <stdio.h>

int main() {
int num, sum = 0, digit;

printf("Enter a number: ");


scanf("%d", &num);

while (num != 0) {
digit = num % 10;
sum += digit;
num /= 10;
}

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


return 0;
}

11. Print Multiplication Table


Print the multiplication table for a given number.

#include <stdio.h>

int main() {
int num, i;

printf("Enter a number: ");


scanf("%d", &num);

printf("Multiplication table for %d:\n", num);


for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}

12. Calculate Power


Calculate the power of a number.
c

#include <stdio.h>

int main() {
int base, exponent;
long long result = 1;

printf("Enter base: ");


scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);

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


result *= base;
}

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


return 0;
}

13. Armstrong Number Check


Check if a number is an Armstrong number.
c

#include <stdio.h>
#include <math.h>

int main() {
int num, originalNum, remainder, n = 0;
double result = 0.0;

printf("Enter a number: ");


scanf("%d", &num);

originalNum = num;

// Count number of digits


while (originalNum != 0) {
originalNum /= 10;
++n;
}

originalNum = num;

// Calculate sum of power of digits


while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}

if ((int)result == num)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);
return 0;
}

14. GCD (Greatest Common Divisor)


Find the GCD of two numbers.
c

#include <stdio.h>

int main() {
int n1, n2, gcd, i;

printf("Enter two numbers: ");


scanf("%d %d", &n1, &n2);

for(i = 1; i <= n1 && i <= n2; i++) {


if(n1 % i == 0 && n2 % i == 0) {
gcd = i;
}
}

printf("GCD of %d and %d is %d\n", n1, n2, gcd);


return 0;
}

15. LCM (Least Common Multiple)


Find the LCM of two numbers.

#include <stdio.h>

int main() {
int n1, n2, max;

printf("Enter two numbers: ");


scanf("%d %d", &n1, &n2);

// Find maximum of n1 and n2


max = (n1 > n2) ? n1 : n2;

while (1) {
if (max % n1 == 0 && max % n2 == 0) {
printf("LCM of %d and %d is %d\n", n1, n2, max);
break;
}
++max;
}
return 0;
}
16. Sum of Natural Numbers
Calculate the sum of first n natural numbers.

#include <stdio.h>

int main() {
int n, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &n);

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


sum += i;
}

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


return 0;
}

17. Check Leap Year


Check if a year is a leap year.

#include <stdio.h>

int main() {
int year;

printf("Enter a year: ");


scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))


printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}

18. Print ASCII Value


Print the ASCII value of a character.
c

#include <stdio.h>

int main() {
char c;

printf("Enter a character: ");


scanf("%c", &c);

printf("ASCII value of %c is %d\n", c, c);


return 0;
}

19. Count Vowels and Consonants


Count the number of vowels and consonants in a string.

#include <stdio.h>
#include <ctype.h>

int main() {
char line[100];
int vowels = 0, consonants = 0;

printf("Enter a string: ");


fgets(line, sizeof(line), stdin);

for (int i = 0; line[i] != '\0'; i++) {


char ch = tolower(line[i]);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')


vowels++;
else if ((ch >= 'a' && ch <= 'z'))
consonants++;
}

printf("Vowels: %d\nConsonants: %d\n", vowels, consonants);


return 0;
}

20. Print Pattern (Pyramid)


Print a pyramid pattern of stars.
c

#include <stdio.h>

int main() {
int rows, i, j, space;

printf("Enter number of rows: ");


scanf("%d", &rows);

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


for (space = 1; space <= rows - i; space++)
printf(" ");

for (j = 1; j <= 2 * i - 1; j++)


printf("*");

printf("\n");
}
return 0;
}

21. Swap Two Numbers Without Temp


Swap two numbers without using a temporary variable.

#include <stdio.h>

int main() {
int a, b;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

printf("Before swapping: a = %d, b = %d\n", a, b);

a = a + b;
b = a - b;
a = a - b;

printf("After swapping: a = %d, b = %d\n", a, b);


return 0;
}
22. Calculate Area of Circle
Calculate the area of a circle using the formula πr².

#include <stdio.h>
#define PI 3.14159

int main() {
float radius, area;

printf("Enter the radius of the circle: ");


scanf("%f", &radius);

area = PI * radius * radius;

printf("Area of the circle: %.2f square units\n", area);


return 0;
}

23. Simple Calculator


Create a simple calculator for basic operations.
c

#include <stdio.h>

int main() {
char operator;
double n1, n2, result;

printf("Enter an operator (+, -, *, /): ");


scanf("%c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &n1, &n2);

switch(operator) {
case '+': result = n1 + n2; break;
case '-': result = n1 - n2; break;
case '*': result = n1 * n2; break;
case '/':
if (n2 != 0) result = n1 / n2;
else { printf("Error! Division by zero.\n"); return 1; }
break;
default: printf("Error! Invalid operator.\n"); return 1;
}

printf("%.1lf %c %.1lf = %.1lf\n", n1, operator, n2, result);


return 0;
}

24. Check Vowel or Consonant


Check if a character is a vowel or consonant.
c

#include <stdio.h>
#include <ctype.h>

int main() {
char ch;

printf("Enter a character: ");


scanf("%c", &ch);

ch = tolower(ch);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')


printf("%c is a vowel.\n", ch);
else if (ch >= 'a' && ch <= 'z')
printf("%c is a consonant.\n", ch);
else
printf("%c is not an alphabet.\n", ch);
return 0;
}

25. Linear Search in Array


Implement linear search to find an element in an array.
c

#include <stdio.h>

int main() {
int arr[10], n, i, searchElement, found = 0;

printf("Enter size of array (max 10): ");


scanf("%d", &n);

printf("Enter %d elements: ", n);


for (i = 0; i < n; i++)
scanf("%d", &arr[i]);

printf("Enter element to search: ");


scanf("%d", &searchElement);

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


if (arr[i] == searchElement) {
printf("Element found at index %d\n", i);
found = 1;
break;
}
}

if (!found)
printf("Element not found in the array\n");
return 0;
}

26. Find Average of Array Elements


Calculate the average of elements in an array.
c

#include <stdio.h>

int main() {
int n, i;
float arr[10], sum = 0.0, average;

printf("Enter size of array (max 10): ");


scanf("%d", &n);

printf("Enter %d elements: ", n);


for (i = 0; i < n; i++) {
scanf("%f", &arr[i]);
sum += arr[i];
}

average = sum / n;
printf("Average of array elements: %.2f\n", average);
return 0;
}

27. Count Occurrence of a Character


Count the occurrences of a character in a string.
c

#include <stdio.h>

int main() {
char str[100], ch;
int count = 0;

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);

printf("Enter a character to find its frequency: ");


scanf("%c", &ch);

for (int i = 0; str[i] != '\0'; i++) {


if (ch == str[i])
count++;
}

printf("Frequency of %c = %d\n", ch, count);


return 0;
}

28. Find Largest Element in Array


Find the largest element in an array.
c

#include <stdio.h>

int main() {
int arr[10], n, i, max;

printf("Enter size of array (max 10): ");


scanf("%d", &n);

printf("Enter %d elements: ", n);


for (i = 0; i < n; i++)
scanf("%d", &arr[i]);

max = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > max)
max = arr[i];
}

printf("Largest element in array: %d\n", max);


return 0;
}

29. Convert Binary to Decimal


Convert a binary number to decimal.
c

#include <stdio.h>
#include <math.h>

int main() {
long long binary;
int decimal = 0, i = 0, remainder;

printf("Enter a binary number: ");


scanf("%lld", &binary);

while (binary != 0) {
remainder = binary % 10;
binary /= 10;
decimal += remainder * pow(2, i);
++i;
}

printf("Decimal equivalent: %d\n", decimal);


return 0;
}

30. Bubble Sort


Implement bubble sort to sort an array.
c

#include <stdio.h>

int main() {
int arr[10], n, i, j, temp;

printf("Enter size of array (max 10): ");


scanf("%d", &n);

printf("Enter %d elements: ", n);


for (i = 0; i < n; i++)
scanf("%d", &arr[i]);

// Bubble sort
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

printf("Sorted array: ");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}

You might also like