C Top Exercises
Write a C program to check whether an alphabet is Vowel or Consonant
Description:
You need to create a C program to check whether an alphabet is Vowel or Consonant.
Conditions:
- Create a character type variable with name of alphabet and take the value from the user.
- You can use conditional statements.
#include <stdio.h>
int main()
{
char alphabet;
printf("Enter an alphabet: ");
scanf("%c", &alphabet);
if (alphabet == 'a' || alphabet == 'A')
{
printf("%c is a vowel.", alphabet);
}
else if (alphabet == 'e' || alphabet == 'E')
{
printf("%c is a vowel.", alphabet);
}
else if (alphabet == 'i' || alphabet == 'I')
{
printf("%c is a vowel.", alphabet);
}
else if (alphabet == 'o' || alphabet == 'O')
{
printf("%c is a vowel.", alphabet);
}
else if (alphabet == 'u' || alphabet == 'U')
{
printf("%c is a vowel.", alphabet);
}
else
{
printf("%c is a consonant.", alphabet);
}
return 0;
}
Tutorials Class - Output Window
Enter an alphabet: O
O is a vowel.
Write a C program to find the maximum number between three numbers
Description:
You need to write a C program to find the maximum number between three numbers.
Conditions:
- Create three variables in c with name of number1, number2 and number3
- Find out the maximum number using the nested if-else statement
#include <stdio.h>
int main()
{
int number1, number2, number3;
printf("Enter three numbers: ");
scanf("%d%d%d", &number1, &number2, &number3);
if(number1 > number2)
{
if(number1 > number3)
{
printf("Number1 is max with value of %d",number1);
}
else
{
printf("Number3 is max with value of %d",number3);
}
}
else
{
if(number2 > number3)
{
printf("Number2 is max with value of %d",number2);
}
else
{
printf("Number3 is max with value of %d",number3);
}
}
return 0;
}
Tutorials Class - Output Window
Enter three numbers: 10
20
30
Number3 is max with value of 30
Write a C program to calculate Electricity bill.
Description:
You need to write a C program to calculate electricity bill using if-else statements.
Conditions:
- For first 50 units – Rs. 3.50/unit
- For next 100 units – Rs. 4.00/unit
- For next 100 units – Rs. 5.20/unit
- For units above 250 – Rs. 6.50/unit
- You can use conditional statements.
#include<stdio.h>
int main()
{
float bill, units;
printf("Enter the units consumed=");
scanf("%f",&units);
if(units<=50 && units>=0)
{
bill=units*3.50;
printf("Electricity Bill=%f Rupees",bill);
}
else if(units<=100 && units>50)
{
bill=50*3.50+(units-50)*4;
printf("Electricity Bill=%f Rupees",bill);
}
else if(units<=250 && units>150)
{
bill=50*3.50+100*4+(units-150)*5.20;
printf("Electricity Bill=%f Rupees",bill);
}
else if(units>250)
{
bill=50*3.50+100*4+100*5.20+(units-250)*6.50;
printf("Electricity Bill=%f Rupees",bill);
}
else
{
printf("Please enter valid consumed units...");
}
return 0;
}
Tutorials Class - Output Window
Enter the units consumed=278.90
Electricity Bill=1282.84 Rupees