Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
29 views14 pages

If Condition Sample Programs

The document contains a series of programming exercises in C, categorized into Simple, Medium, and Hard levels. Each exercise includes a scenario description and a corresponding C code snippet to solve the problem, such as checking if a number is positive or negative, determining if a number is prime, and calculating income tax. The exercises cover various programming concepts including conditional statements, loops, and functions.

Uploaded by

e22ec018
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views14 pages

If Condition Sample Programs

The document contains a series of programming exercises in C, categorized into Simple, Medium, and Hard levels. Each exercise includes a scenario description and a corresponding C code snippet to solve the problem, such as checking if a number is positive or negative, determining if a number is prime, and calculating income tax. The exercises cover various programming concepts including conditional statements, loops, and functions.

Uploaded by

e22ec018
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Simple

Question 1: Check if a Number is Positive or Negative

Scenario: Write a program that checks if a given number is positive,


negative, or zero.

#include <stdio.h>

int main() {

int number;

printf("Enter a number: ");

scanf("%d", &number);

if (number > 0) {

printf("The number is positive.\n");

} else if (number < 0) {

printf("The number is negative.\n");

} else {

printf("The number is zero.\n");

return 0;

___________________________________________________________________________
______

Question 2: Check if a Number is Even or Odd

Scenario: Write a program that checks if a given number is even or odd.

#include <stdio.h>

int main() {

int number;
printf("Enter a number: ");

scanf("%d", &number);

if (number % 2 == 0) {

printf("The number is even.\n");

} else {

printf("The number is odd.\n");

return 0;

___________________________________________________________________________
______

Question 3: Find the Largest of Two Numbers

Scenario: Write a program that finds the largest of two given numbers.

#include <stdio.h>

int main() {

int num1, num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

if (num1 > num2) {

printf("The largest number is %d.\0;

___________________________________________________________________________
______
Question 4: Check if a Character is a Vowel or Consonant

Scenario: Write a program that checks if a given character is a vowel or a


consonant.

#include <stdio.h>

int main() {

char ch;

printf("Enter a character: ");

scanf(" %c", &ch);

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

ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {

printf("The character is a vowel.\n");

} else {

is a consonant.\n");

return 0;

___________________________________________________________________________
______

Question 5: Check if a Year is a Leap Year

Scenario: Write a program that checks if a given 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("The year is a leap year.\n");

} else {

printf("The year is not a leap year.\n");

___________________________________________________________________________
______

Medium

Question 1: Check if a Number is Prime

Scenario: Write a program that checks if a given number is a prime


number.

#include <stdio.h>

int main() {

int number, i, flag = 0;

printf("Enter a number: ");

scanf("%d", &number);

if (number <= 1) {

flag = 1;

} else {

for (i = 2; i <= number / 2; ++i) {

if (number % i == 0) {

flag = 1;

break;

}
if (flag == 0) {

printf("The number is a prime number.\n");

} else {

printf("The number is not a prime number.\n");

return 0;

___________________________________________________________________________
______

Question 2: Calculate the Grade of a Student

Scenario: Write a program that calculates the grade of a student based


on their marks in three subjects.

#include <stdio.h>

int main() {

int marks1, marks2, marks3;

float average;

printf("Enter marks for three subjects: ");

scanf("%d %d %d", &marks1, &marks2, &marks3);

average = (marks1 + marks2 + marks3) / 3.0;

if (average >= 90) {

printf("Grade: A\n");

} else if (average >= 80) {

printf("Grade: B\n");

} else if (average >= 70) {


printf("Grade: C\n");

} else if (average >= 60) {

printf("Grade: D\n");

} else {

printf("Grade: F\n");

return 0;

___________________________________________________________________________
______

Question 3: Check if a Triangle is Valid

Scenario: Write a program that checks if three given sides can form a
valid triangle.

#include <stdio.h>

int main() {

int side1, side2, side3;

printf("Enter three sides of a triangle: ");

scanf("%d %d %d", &side1, &side2, &side3);

if ((side1 + side2 > side3) && (side1 + side3 > side2) && (side2 +
side3 > side1)) {

printf("The sides form a valid triangle.\n");

} else {

");

return 0;
}

___________________________________________________________________________
______

Question 4: Check if a Character is an Alphabet

Scenario: Write a program that checks if a given character is an alphabet


(either uppercase or lowercase).

#include <stdio.h>

int main() {

char ch;

printf("Enter a character: ");

scanf(" %c", &ch);

if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {

printf("The character is an alphabet.\n");

} else {

printf("The character is not an alphabet.\n");

___________________________________________________________________________
______

Question 5: Calculate Electricity Bill

Scenario: Write a program that calculates the electricity bill based on the
following conditions:

 For the first 100 units, the cost is 1.5 per unit.

 For the next 100 units, the cost is 2.5 per unit.
 For units above 200, the cost is 3.5 per unit.

#include <stdio.h>

int main() {

int units;

float bill = 0.0;

printf("Enter the number of units consumed: ");

scanf("%d", &units);

* 1.5;

} else if (units <= 200) {

bill = 100 * 1.5 + (units - 100) * 2.5;

} else {

bill = 100 * 1.5 + 100 * 2.5 + (units - 200) * 3.5;

printf("The electricity bill is: %.2f\n", bill);

return 0;

___________________________________________________________________________
______

Hard

Question 1: Check if a Number is an Armstrong Number

Scenario: Write a program that checks if a given number is an Armstrong


number (also known as a narcissistic number).

#include <stdio.h>

#include <math.h>

int main() {

int number, originalNumber, remainder, n = 0;


float result = 0.0;

printf("Enter an integer: ");

scanf("%d", &number);

originalNumber = number;

// Find the number of digits

for (originalNumber = number; originalNumber != 0; ++n) {

originalNumber /= 10;

originalNumber = number;

// Calculate the sum of the nth power of each digit

for (originalNumber = number; originalNumber != 0; originalNumber /=


10) {

remainder = originalNumber % 10;

result += pow(remainder, n);

if ((int)result == number) {

printf("%d is an Armstrong number.\n", number);

} else {

printf("%d is not an Armstrong number.\n", number);

return 0;

}
___________________________________________________________________________
______

Question 2: Calculate Income Tax

Scenario: Write a program that calculates the income tax based on the
following slabs:

 Income up to 2,50,000: No tax

 Income from 2,50,001 to 5,00,000: 5%

 Income from 5,00,001 to 10,00,000: 20%

 Income above 10,00,000: 30%

#include <stdio.h>

int main() {

float income, tax = 0.0;

printf("Enter your annual income: ");

scanf("%f", &income);

if (income <= 250000) {

tax = 0;

} else if (income <= 500000) {

tax = (income - 250000) * 0.05;

} else if (income <= 1000000) {

tax = (250000 * 0.05) + (income - 500000) * 0.20;

} else {

tax = (250000 * 0.05) + (500000 * 0.20) + (income - 1000000) *


0.30;

printf("Your income tax is: %.2f\n", tax);


return 0;

___________________________________________________________________________
______

Question 3: Check if a String is a Palindrome

Scenario: Write a program that checks if a given string is a palindrome.

#include <stdio.h>

#include <string.h>

int main() {

char str[100], reversedStr[100];

int length, i, flag = 0;

printf("Enter a string: ");

gets(str);

length = strlen(str);

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

reversedStr[i] = str[length - i - 1];

reversedStr[length] = '\0';

if (strcmp(str, reversedStr) == 0) {

printf("The string is a palindrome.\n");

} else {

printf("The string is not a palindrome.\n");


}

return 0;

___________________________________________________________________________
______

Question 4: Calculate the Roots of a Quadratic Equation

Scenario: Write a program that calculates the roots of a quadratic


equation (ax^2 + bx + c = 0).

#include <stdio.h>

#include <math.h>

int main() {

float a, b, c, discriminant, root1, root2, realPart, imaginaryPart;

printf("Enter coefficients a, b, and c: ");

scanf("%f %f %f", &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 different.\n");

printf("Root 1 = %.2f\n", root1);

printf("Root 2 = %.2f\n", root2);

} else if (discriminant == 0) {

root1 = root2 = -b / (2 * a);

printf("Roots are real and the same.\n");


printf("Root 1 = Root 2 = %.2f\n", root1);

} else {

realPart = -b / (2 * a);

imaginaryPart = sqrt(-discriminant) / (2 * a);

printf("Roots are complex and different.\n");

printf("Root 1 = %.2f + %.2fi\n", realPart, imaginaryPart);

printf("Root 2 = %.2f - %.2fi\n", realPart, imaginaryPart);

return 0;

___________________________________________________________________________
______

Question 5: Check if a Number is a Perfect Number

Scenario: Write a program that checks if a given number is a perfect


number.

#include <stdio.h>

int main() {

int number, i, sum = 0;

printf("Enter a number: ");

scanf("%d", &number);

for (i = 1; i <= number / 2; i++) {

if (number % i == 0) {

sum += i;

}
if (sum == number) {

printf("%d is a perfect number.\n", number);

} else {

printf("%d is not a perfect number.\n", number);

return 0;

___________________________________________________________________________
______

You might also like