Basic
Practice Problems based on chapter which you learn in previous Tutorials.
1. Even or Odd numbers
#include <stdio.h>
Copy Code
int
main
() {
int
number;
printf
("Enter an integer: ");
scanf
("%d", &number);
if
(
number
% 2 == 0) {
printf
("%d is an even number.
",
number
);
}
else
{
printf
("%d is an odd number.
",
number
);
}
return
0;
}
The program checks whether the entered number is even or odd using the modulus operator (%).
If the result of number % 2 is equal to 0, it means the number is even. Otherwise, it's odd.
The program then prints the result.
2. Vowel and Consonant
#include <stdio.h>
Copy Code
int
main
() {
char
character;
printf
("Enter a character: ");
scanf
(" %c", &character);
if
((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z')) {
if
(character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u' ||
character == 'A' || character == 'E' || character == 'I' || character == 'O' || character == 'U') {
printf
("%c is a vowel.
", character);
}
else
{
printf
("%c is a consonant.
", character);
}
}
else
{
printf
("Invalid input. Please enter an alphabet.
");
}
return
0;
}
3. Roots of Quadratic Equations
#include <stdio.h>
Copy Code
#include <math.h>
int
main
() {
double
a, b, c, discriminant, root1, root2;
printf
("Enter coefficients (a, b, c) of the quadratic equation (ax^2 + bx + c = 0):
");
scanf
("%lf %lf %lf", &a, &b, &c);
discriminant
=
b * b - 4 * a * c
;
if
(
discriminant
> 0) {
root1
= (-
b + sqrt(discriminant)) / (2 * a)
;
root2
= (-
b - sqrt(discriminant)) / (2 * a)
;
printf
("Roots are real and distinct:
");
printf
("Root 1: %lf
",
root1
);
printf
("Root 2: %lf
",
root2
);
}
else
if
(
discriminant
== 0) {
root1
= -
b / (2 * a)
;
printf
("Roots are real and equal:
");
printf
("Root 1 = Root 2: %lf
",
root1
);
}
else
{
double
realPart = -
b / (2 * a)
;
double
imaginaryPart =
sqrt(-discriminant) / (2 * a)
;
printf
("Roots are complex and conjugate:
");
printf
("Root 1: %lf + %lfi
",
realPart
,
imaginaryPart
);
printf
("Root 2: %lf - %lfi
",
realPart
,
imaginaryPart
);
}
return
0;
}
4. Fibonacci Sequence
#include <stdio.h>
Copy Code
int
main
() {
int
n, first = 0, second = 1, next;
printf
("Enter the number of terms: ");
scanf
("%d", &n);
printf
("Fibonacci Series: ");
for
(
int
i = 0; i < n; i++) {
if
(i <= 1) {
next
= i;
}
else
{
next
= first + second;
first
= second;
second
= next;
}
printf
("%d ",
next
);
}
return
0;
}