Lab 4
Lab 4
Lab 4
Spring 2024
Conditional Structure
Objective:
1. Understand if, else if, and else statements.
2. Explore nested conditionals and switch statements.
3. And use of logic operators (‘&&’, ‘||’, ‘!’) in conditions.
Task 1:
Write a program that calculates the grade of a student based on their score. Use conditional
statements to determine the grade (e.g., A, B, C, D, F) according to following score ranges.
90-A
80-B
70-C
60-D
Less than 60-F
Code:
1. #include <stdio.h>
2. int main() {
3. int marks;
4. printf("Enter the marks of the student between 100 and 0 : ");
5. scanf("%d", &marks);
6. if (marks >= 90 && marks <= 100)
7. printf("Marks entered are %d \nYour grade is A",marks);
8. else if (marks >= 80 && marks < 90)
9. printf("Marks entered are %d \nYour grade is B",marks);
10. else if (marks >= 70 && marks < 80)
11. printf("Marks entered are %d \nYour grade is C",marks);
12. else if (marks >= 60 && marks < 70)
13. printf("Marks entered are %d \nYou grade is D",marks);
14. else if (marks >= 0 &&marks < 60)
15. printf("Marks entered are %d \nYou grade is F",marks);
16. else if (marks >100)
17. printf("You Entered more than 100");
18. else if (marks < 0)
19. printf("You Entered less than 0");
20. return 0;
21. }
Output:
Explanation:
The C program is designed to assess and assign grades based on a student's input marks.
On line 3, an integer variable “marks” is declared to store the student's marks. The user is
prompted to input marks on line 4, and the value is stored using “scanf” on line 5. Subsequent
lines (6-19) feature a series of “if-else” statements to check the entered marks against predefined
ranges, assigning grades accordingly. The program handles invalid inputs with checks for marks
exceeding 100 (line 16) or falling below 0 (line 18), accompanied by relevant error messages.
The program concludes by returning 0 on line 20 to indicate successful completion.
Task 2:
Create a simple number guessing game where the program generates a random number, and the
user must guess it.
Provide feedback to the user using conditional statements based on whether their guess is
too high, too low, or correct.
Give your user three attempts to guess right number.
Hint: you can use these functions rand(), srand() and its library <stdlib.h>.
Code:
1. #include <stdio.h>
2. #include <stdlib.h>
3. int main() {
4. int guess, take;
5. guess = rand();
6. for (int i = 0; i < 3; i++) {
7. printf("Enter the number: ");
8. scanf("%d", &take);
9. if (take == guess) {
10. printf("Your guess is correct!\n\n");
11. break;}
12. else if (guess > take) {
13. printf("Entered number is smaller than the target number.\n\n");}
14. else if (take > guess) {
15. printf("Entered number is larger than the target number.\n\n");}
16. if (i == 2) {
17. printf("You did not guess the number in three turns, the number was %d\n", guess);}
18. }
19. return 0;}
Output:
Explanation:
This C program (lines 1-19) is a basic number guessing game. It includes essential header
files declares variables “guess” and “take”, and generates a random number using “rand()” (lines
3-5).The game logic is encapsulated in a “for” loop, allowing the user three attempts to guess the
number (lines 6-18). In each iteration, user input is obtained using “scanf” (lines 7-8). The
program checks if the input matches the target number, providing feedback and breaking the loop
if correct (lines 9-11). If the guess is smaller or larger, corresponding hints are given (lines 12-
15).The loop includes a check (line 16) for exhausting three attempts without a correct guess. In
such a case, the correct number is revealed. The program concludes by returning 0 (line 19),
indicating successful execution.
Task 3:
Design a simple authentication system where the user enters a username and password. Use
conditional statements to check if the entered credentials match predefined values and provide
access or deny access accordingly.
Hint: strcmp() function is used to compare strings if true then it will return 0. Don’t forget the
library <string.h>.
Code:
1. #include <stdio.h>
2. #include <string.h>
3. int main() {
4. char input_pass[421];
5. char user[] = "Musa";
6. char input_user[421];
7. char password[] = "550755";
8. printf("Enter user name :");
9. scanf("%s", input_user);
10. printf("Enter password : ");
11. scanf("%s", input_pass);
12. if (strcmp(input_pass, password) == 0 && strcmp(input_user, user) == 0)
13. printf("Access granted ");
14. else
15. printf("Access denied ");
16. return 0;
17. }
Output:
Explanation:
This C program (lines 1-17) serves as a basic username and password authentication
system. It includes necessary header files (lines 1-2) and initializes character arrays for input and
predefined values (lines 4-7). Users input their username and password on lines 8-11 using
“printf” and “scanf”. Comparison with predefined values (line 12) occurs using `strcmp`. If both
match, "Access granted" is printed; otherwise, "Access denied" is displayed (lines 13-15). The
program returns 0 (line 16) to signify successful completion.
Task 4:
Implement a basic calculator program that takes two numbers and an arithmetic operator (+, -,
*, /) as input from the user and performs the corresponding operation using switch statements.
Code:
1. #include <stdio.h>
2. int main() {
3. int num_1,num_2;
4. char oper;
5. printf("Enter the first number : ");
6. scanf("%d", & num_1);
7. printf("Enter the operators : ");
8. scanf (" %c", &oper);
9. printf("Enter the second number : ");
10. scanf (" %d",&num_2);
11. switch (oper){
12. case '+' :
13. printf ("The sum is : %d",num_1+num_2);
14. break ;
15. case '-' :
16. printf ("The difference is : %d",num_1-num_2);
17. break ;
18. case '*' :
19. printf ("The product is : %d",num_1*num_2);
20. break ;
21. case '/' :
22. printf ("The quotient is : %d",num_1/num_2);
23. break ;
24. }
25. return 0
26. }
Output:
Explanation:
This C program (lines 1-25) acts as a basic calculator, taking user input for two numbers
and an operator. It declares integer variables “num_1” and “num_2” to store the input numbers,
and a character variable “oper” for the operator (lines 3-4). Users enter the first number, operator,
and the second number, all of which are then processed by a “switch” statement (lines 11-24) to
perform the corresponding operation. The program concludes by returning 0 (line 25), indicating
successful execution.
Conclusion:
In this lab, we acquired the knowledge of conditional structure for writing smart C
program which can take decisions on the basis of some conditions. Conditional structure is
different from the sequential structure, as it can run certain parts of the code depending on some
conditions. There are different types or ways of writing conditional statements such as if
statement, if-else statement, if-else if-else statement and switch statement. If statement checks
for only one condition and executes the code if the condition is true. If-else statement checks for
a condition and executes a block of code if it is true and executes another block when the
condition is false. If-else if-else statement checks for multiple conditions and executes different
blocks depending on the condition. Switch statement works much like If-else if-if statement, but
it takes an expression as input and depending on the value of the expression different blocks of
code are executed. Using the knowledge of these structures, we performed 4 tasks in the lab in
order to test our understanding of these concepts.