Introduction to condition and switch

  

example of how to use condition in c

  

Different example of condition in c programming

  

example of how to use switch in c programming

  

example of different qauestion using switch in c programming

  

Introduction to Loop in c programming

  

Different question of using loop in c programming

  

Introduction to array in c programming

  

Different question and answer in array

  

Introduction to Condition and switch


Example of different question in c programming



1. Write a C program to input marks of five subjects: Physics, Chemistry, Biology, Mathematics and Computer. Calculate the percentage and grade according to following: Percentage >=90%: Grade A
Percentage >=80%: Grade B
Percentage >=70%: Grade C
Percentage >=60%: Grade D
Percentage >=40%: Grade E
Percentage < 40%: Grade F



#include <stdio.h>
int main() {
float physics, chemistry, biology, mathematics, computer, percentage;
printf("Enter marks in Physics, Chemistry, Biology, Mathematics, and Computer: ");
scanf("%f %f %f %f %f", &physics, &chemistry, &biology, &mathematics, &computer);
percentage = (physics + chemistry + biology + mathematics + computer) / 5.0;
if (percentage >= 90)
printf("Grade A\n");
else if (percentage >= 80)
printf("Grade B\n");
else if (percentage >= 70)
printf("Grade C\n");
else if (percentage >= 60)
printf("Grade D\n");
else if (percentage >= 40)
printf("Grade E\n");
else
printf("Grade F\n");
return 0;
}


2. Write a C program to find the maximum between two numbers.



#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 > num2)
printf("Maximum is %d\n", num1);
else
printf("Maximum is %d\n", num2);
return 0;
}



3. Write a C program to find the maximum between three numbers.



#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3)
printf("Maximum is %d\n", num1);
else if (num2 >= num1 && num2 >= num3)
printf("Maximum is %d\n", num2);
else
printf("Maximum is %d\n", num3);
return 0;
}



6. Write a C program to accept two integers and check whether they are equal or not.



#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
if (num1 == num2)
printf("The integers are equal.\n");
else
printf("The integers are not equal.\n");
return 0;
}




7. Write a C program to find 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("Leap year.\n");
else
printf("Not a leap year.\n");
return 0;
}



8. Write a C program to read the age of a citizen and determine whether he is eligible to cast his/her own vote.



#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18)
printf("You are eligible to cast your vote.\n");
else
printf("You are not eligible to cast your vote.\n");
return 0;
}



9. Write a C program to read the value of an integer m and display the value of n is 1 when m is larger than 0, 0 when m is 0 and -1 when m is less than 0.



#include <stdio.h>
int main() {
int m, n;
printf("Enter an integer m: ");
scanf("%d", &m);
if (m > 0)
n = 1;
else if (m == 0)
n = 0;
else
n = -1;
printf("n is %d\n", n);
return 0;
}



10. Write a C program to rank three numbers from the smallest to the largest. Expected Output :
1st Number = 12, 2nd Number = 25, 3rd Number = 52



#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 <= num2 && num1 <= num3) {
printf("Smallest: %d\n", num1);
if (num2 <= num3)
printf("Middle: %d\nLargest: %d\n", num2, num3);
else
printf("Middle: %d\nLargest: %d\n", num3, num2);
} else if (num2 <= num1 && num2 <= num3) {
printf("Smallest: %d\n", num2);
if (num1 <= num3)
printf("Middle: %d\nLargest: %d\n", num1, num3);
else
printf("Middle: %d\nLargest: %d\n", num3, num1);
} else {
printf("Smallest: %d\n", num3);
if (num1 <= num2)
printf("Middle: %d\nLargest: %d\n", num1, num2);
else
printf("Middle: %d\nLargest: %d\n", num2, num1);
}
return 0;
}



11. Write a C program to read temperature in celsius and display a suitable message according to the temperature state below:
Temp < 0 then Freezing weather
Temp 0-10 then Very Cold weather
Temp 10-20 then Cold weather
Temp 20-30 then Normal in Temp
Temp 30-40 then Its Hot
Temp >=40 then Its Very Hot



#include <stdio.h>
int main() {
float temperature;
printf("Enter temperature in Celsius: ");
scanf("%f", &temperature);
if (temperature < 0)
printf("Freezing weather\n");
else if (temperature >= 0 && temperature <= 10)
printf("Very Cold weather\n");
else if (temperature > 10 && temperature <= 20)
printf("Cold weather\n");
else if (temperature > 20 && temperature <= 30)
printf("Normal in Temp\n");
else if (temperature > 30 && temperature <= 40)
printf("It's Hot\n");
else
printf("It's Very Hot\n");
return 0;
}



12. Write a C program to check whether a triangle is Equilateral, Isosceles or Scalene.



#include <stdio.h>
int main() {
int side1, side2, side3;
printf("Enter the lengths of three sides of a triangle: ");
scanf("%d %d %d", &side1, &side2, &side3);
if (side1 == side2 && side2 == side3)
printf("Equilateral Triangle\n");
else if (side1 == side2 || side2 == side3 || side1 == side3)
printf("Isosceles Triangle\n");
else
printf("Scalene Triangle\n");
return 0;
}



13. Write a C program to check whether a triangle can be formed with the given values for the angles.



#include <stdio.h>
int main() {
int angle1, angle2, angle3;
printf("Enter three angles of a triangle: ");
scanf("%d %d %d", &angle1, &angle2, &angle3);
if (angle1 + angle2 + angle3 == 180)
printf("A triangle can be formed with these angles.\n");
else
printf("A triangle cannot be formed with these angles.\n");
return 0;
}



14. Write a program in C to calculate and print the electricity bill of a given customer. The customer unit consumed by the user should be captured from the keyboard to display the total amount to be paid to the customer.
The charge are as follow :
Unit Charge/unit
upto 199 Rwf300
200 and above but less than 400 Rwf500
400 and above but less than 600 Rwf700
600 and above Rwf900
If bill exceeds Rwf 500 then a surcharge of 15% will be charged and the
minimum bill should be of Rwf 1000



#include <stdio.h>
int main() {
int units;
float totalAmount;
printf("Enter the units consumed: ");
scanf("%d", &units);
if (units <= 199)
totalAmount = units * 1.5;
else if (units >= 200 && units < 400)
totalAmount = units * 1.2;
else if (units >= 400 && units < 600)
totalAmount = units * 1.0;
else
totalAmount = units * 0.9;
if (totalAmount > 500)
totalAmount = totalAmount + (totalAmount * 0.15);
if (totalAmount < 1000)
totalAmount = 1000;
printf("Electricity Bill: Rwf %.2f\n", totalAmount);
return 0;
}



15.Write a C program to read any day number in integer and display the day name in word format.



#include <stdio.h>
int main() {
int dayNumber;
printf("Enter a day number (1-7): ");
scanf("%d", &dayNumber);
if (dayNumber == 1)
printf("Sunday\n");
else if (dayNumber == 2)
printf("Monday\n");
else if (dayNumber == 3)
printf("Tuesday\n");
else if (dayNumber == 4)
printf("Wednesday\n");
else if (dayNumber == 5)
printf("Thursday\n");
else if (dayNumber == 6)
printf("Friday\n");
else if (dayNumber == 7)
printf("Saturday\n");
else
printf("Invalid day number\n");
return 0;
}





16. Write a program in C to read any digit and display it in the word.



#include <stdio.h>
int main() {
int digit;
printf("Enter a digit (0-9): ");
scanf("%d", &digit);
if (digit == 0)
printf("Zero\n");
else if (digit == 1)
printf("One\n");
else if (digit == 2)
printf("Two\n");
else if (digit == 3)
printf("Three\n");
else if (digit == 4)
printf("Four\n");
else if (digit == 5)
printf("Five\n");
else if (digit == 6)
printf("Six\n");
else if (digit == 7)
printf("Seven\n");
else if (digit == 8)
printf("Eight\n");
else if (digit == 9)
printf("Nine\n");
else
printf("Invalid digit\n");
return 0;
}





Write a C program for reading any Month Number and displaying the Month name as a word.



#include <stdio.h>
int main() {
int monthNumber;
printf("Enter a month number (1-12): ");
scanf("%d", &monthNumber);
if (monthNumber == 1)
printf("January\n");
else if (monthNumber == 2)
printf("February\n");
else if (monthNumber == 3)
printf("March\n");
else if (monthNumber == 4)
printf("April\n");
else if (monthNumber == 5)
printf("May\n");
else if (monthNumber == 6)
printf("June\n");
else if (monthNumber == 7)
printf("July\n");
else if (monthNumber == 8)
printf("August\n");
else if (monthNumber == 9)
printf("September\n");
else if (monthNumber == 10)
printf("October\n");
else if (monthNumber == 11)
printf("November\n");
else if (monthNumber == 12)
printf("December\n");
else
printf("Invalid month number\n");
return 0;
}



18. Write a program in C to read any Month Number in integer and display the number of days for this month.



#include <stdio.h>
int main() {
int monthNumber;
printf("Enter a month number (1-12): ");
scanf("%d", &monthNumber);
if (monthNumber == 1 || monthNumber == 3 || monthNumber == 5 || monthNumber == 7 ||
monthNumber == 8 || monthNumber == 10 || monthNumber == 12)
printf("Number of days: 31\n");
else if (monthNumber == 4 || monthNumber == 6 || monthNumber == 9 || monthNumber ==
11)
printf("Number of days: 30\n");
else if (monthNumber == 2)
printf("Number of days: 28 or 29\n");
else
printf("Invalid month number\n");
return 0;
}


Contact Us

Email: info@learn.co.rw
Phone: +250723709880
Whatsap: +250725527181

we will try to reply to your message as quickly as we can. please be aware that because of many messages we receive it take us same time to respond