0% found this document useful (0 votes)
4 views5 pages

C Language CONDITION PROGRAM

The document contains several C programming examples demonstrating the use of if, if-else, and nested if-else statements. It includes programs for converting seconds to hours, checking if a number is positive or negative, calculating grades, determining leap years, and checking triangle types. Each example is accompanied by explanations to aid understanding of the concepts presented.

Uploaded by

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

C Language CONDITION PROGRAM

The document contains several C programming examples demonstrating the use of if, if-else, and nested if-else statements. It includes programs for converting seconds to hours, checking if a number is positive or negative, calculating grades, determining leap years, and checking triangle types. Each example is accompanied by explanations to aid understanding of the concepts presented.

Uploaded by

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

1|Page

REALtechSmart

Below is a C program that takes input in seconds and converts it into hours, minutes, and
seconds.
#include <stdio.h>
int main()
{
long int seconds;
int hours, minutes, remaining_seconds;
printf("Enter total seconds: ");
scanf("%ld", &seconds);
if (seconds < 0) // Check for negative input
{
printf("Please Enter a non-negative Value \n");
}
else // Calculate hours
{
hours = seconds / 3600; // 1 hour =3600 seconds
seconds = seconds % 3600; // Calculate remaining sec
minutes = seconds / 60; // Calculate minutes
remaining_seconds = seconds % 60; // remaining sec
printf("Time: %d hours, %d minutes, %d seconds\n",
hours, minutes, remaining_seconds);
}
return 0;
}

REALtechSmart
ÇgMkSu dh 'kku gj dne vkids lkFk
Trust Of 25 Years
Contact :9314397377 HINDAUN CITY
2|Page
REALtechSmart
Below are a few C language practice programs
demonstrating if, if-else, and nested if-else statements.
Each program includes a brief explanation and is designed
to help you understand the concepts through practical
examples.

1. Simple If Statement
This program checks if a number is positive.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("%d is positive.\n", num);
}
return 0;
}
Explanation:
 The program takes a number as input.
 The if statement checks if the number is greater than 0.
 If true, it prints that the number is positive.

2. If-Else Statement
This program checks if a number is positive or negative.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("%d is positive.\n", num);
} else {
printf("%d is negative or zero.\n", num);
}
return 0;
}
Explanation:
 The if condition checks if the number is positive.
 The else block executes if the condition is false, indicating the number is negative or zero.

REALtechSmart
ÇgMkSu dh 'kku gj dne vkids lkFk
Trust Of 25 Years
Contact :9314397377 HINDAUN CITY
3|Page
REALtechSmart

3. Nested If-Else Statement


This program checks if a number is positive, negative, or zero.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("%d is positive.\n", num);
} else {
if (num < 0) {
printf("%d is negative.\n", num);
} else {
printf("The number is zero.\n");
}
}
return 0;
}
Explanation:
 The outer if checks if the number is positive.
 If false, the else block contains a nested if-else to check if the number is negative or zero.

4. Practice Program: Grade Calculator (If-Else Ladder)


This program calculates a student's grade based on their marks.
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks (0-100): ");
scanf("%d", &marks);
if (marks >= 90 && marks <= 100) {
printf("Grade: A+\n");
} else if (marks >= 80) {
printf("Grade: A\n");
} else if (marks >= 70) {
printf("Grade: B\n");
} else if (marks >= 60) {
printf("Grade: C\n");

REALtechSmart
ÇgMkSu dh 'kku gj dne vkids lkFk
Trust Of 25 Years
Contact :9314397377 HINDAUN CITY
4|Page
REALtechSmart

} else if (marks >= 50) {


printf("Grade: D\n");
} else if (marks >= 0) {
printf("Grade: F (Fail)\n");
} else {
printf("Invalid marks entered.\n");
}
return 0;
}
Explanation:
 The program uses an if-else ladder to assign a grade based on the marks.
 Each condition checks a range of marks, and the corresponding grade is printed.
 The final else handles invalid input (e.g., negative marks or marks > 100).

5. Nested If-Else: Leap Year Checker


This program 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) {
if (year % 100 == 0) {
if (year % 400 == 0) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
} else {
printf("%d is a leap year.\n", year);
}
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
Explanation:
 A year is a leap year if it is divisible by 4, but not by 100 unless it is also divisible by 400.

REALtechSmart
ÇgMkSu dh 'kku gj dne vkids lkFk
Trust Of 25 Years
Contact :9314397377 HINDAUN CITY
5|Page
REALtechSmart
 Nested if statements check these conditions step-by-step.

6. Practice Program: Triangle Type Checker


This program determines if three sides form a valid triangle and, if so,
whether it’s equilateral, isosceles, or scalene.
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three sides of the triangle: ");
scanf("%d %d %d", &a, &b, &c);
// Check if the sides form a valid triangle
if (a + b > c && b + c > a && a + c > b) {
// Check triangle type
if (a == b && b == c) {
printf("Equilateral triangle.\n");
} else if (a == b || b == c || a == c) {
printf("Isosceles triangle.\n");
} else {
printf("Scalene triangle.\n");
}
} else {
printf("Invalid triangle.\n");
}
return 0;
}
Explanation:
 The outer if checks if the sides satisfy the triangle inequality theorem.
 If valid, nested if-else statements determine the triangle type based on side equality.

Tips for Practice:


1. Modify the programs: Change conditions, add more cases, or handle edge cases (e.g.,
negative numbers, invalid inputs).
2. Combine concepts: Write a program that uses both nested if and if-else ladders.
3. Debug: Intentionally introduce errors (e.g., missing braces) to understand common mistakes.
4. Test with different inputs: Use boundary values (e.g., 0, 100 for marks) to ensure
robustness.

REALtechSmart
ÇgMkSu dh 'kku gj dne vkids lkFk
Trust Of 25 Years
Contact :9314397377 HINDAUN CITY

You might also like