Certainly! Here are some simple exercises in C language along with examples:
Write a program that takes two numbers as input from the user and calculates their sum.
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("Sum = %d\n", sum);
return 0;
}
Write a program that checks whether a given number is even or odd.
#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;
}
Write a program to calculate the factorial of a given number.
#include <stdio.h>
int main() {
int num, fact = 1, i;
printf("Enter a number: ");
scanf("%d", &num);
for (i = 1; i <= num; i++) {
fact *= i;
}
printf("Factorial of %d = %d\n", num, fact);
return 0;
}
Write a program to check whether a given year is a leap year or not.
#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;
}
Write a program to print the Fibonacci series up to a given number of terms.
#include <stdio.h>
int main() {
int num, first = 0, second = 1, next, i;
printf("Enter the number of terms: ");
scanf("%d", &num);
printf("Fibonacci Series: ");
for (i = 0; i < num; i++) {
printf("%d, ", first);
next = first + second;
first = second;
second = next;
}
return 0;
}
These exercises cover basic concepts such as input/output, arithmetic operations, conditional statements, loops, and functions in C language. They're suitable for beginners to practice and enhance their programming skills.