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

SP Lab 3-Section 1

The document outlines Lab Assignment #3 for the Structured Programming course, focusing on conditional statements and various programming concepts such as operators, selection statements, and function definitions. It includes detailed instructions for implementing a grade evaluation system, checking if a number is odd, even, or zero, and additional exercises involving character classification and modifying even digits in a number. The lab aims to enhance coding skills through practical exercises and code reuse.

Uploaded by

saw20219009
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)
9 views5 pages

SP Lab 3-Section 1

The document outlines Lab Assignment #3 for the Structured Programming course, focusing on conditional statements and various programming concepts such as operators, selection statements, and function definitions. It includes detailed instructions for implementing a grade evaluation system, checking if a number is odd, even, or zero, and additional exercises involving character classification and modifying even digits in a number. The lab aims to enhance coding skills through practical exercises and code reuse.

Uploaded by

saw20219009
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/ 5

King Hussein Faculty of Computing Sciences

Department of Computer Science

Structured Programming Lab – Section 1


Lab Assignment #3 – Conditional Statements
Sunday 16/3/2025 @ 2:00-5:00 PM
Spring 2024/2025

Total Mark / 10

Lab #3 Objectives
● equality and relational operators
● logical operators (AND, OR, NOT)
● Equality (==) and assignment (=) Operators.
● precedence of operators
● if selection statement
● if ...else selection statement
● The conditional operator ( ?:)
● nested if ... else statements
● switch multiple-selection statement

Page 1 of 5
Part I: Lab Example
Example – Student Grade Evaluator
The student will implement a simple grade evaluation system where:

 A random score between -10 and 110 is generated.


 The program assigns a letter grade based on the score.
 The program prints a message based on the assigned grade.

Introduction:
The student will implement a simple grade evaluation system where:

 A random score between -10 and 110 is generated.


 The program assigns a letter grade based on the score.
 The program prints a message based on the assigned grade.

Steps to Follow:
 Define a function getGrade() that:
o Takes the score as a parameter.
o Returns the letter grade based on standard grading criteria.
 If the score is out of range (less than 0 or more than 100), return '0'.
 If the score between 90 and 100 return A
 If the score between 80 and 90 return B
 If the score between 70 and 80 return C
 If the score between 60 and 70 return D
 If the score between 50 and 60 return E
 If the score less than 50 return F
 Define another function printMessage() that:
o Takes the letter grade as a parameter.
o Uses a switch statement to print feedback based on the grade (See code below).
 Inside the main () function:
o Declare an integer variable to store the randomly generated score.
o Use rand () to generate a random score between -10 and 110.
o Call getGrade() to determine the letter grade.
o Display the generated score and letter grade using printf().
Call printMessage() to display an appropriate message.

Sample Output

Your grade as number is: 85


Your grade as char is: B
Good job! Keep it up.

Page 2 of 5
Solution

Page 3 of 5
PART II: Lab Exercise
On the desktop, create a folder named Lab3 followed by your id inside it Create project named
Lab3Ex1.

Exercise 1 –Check if an entered number is Odd , Even, or Zero


Exercise Objectives
 Code reuse.
 Switch multiple-selection statement
 Using user-defined function.

Problem Description

Create a program that determines if the number is a Odd , or the number is Even or the number is equal
to zero. The program will print the state of the number and the number too.

Steps to follow:

 Declare integer variables for the number to be entered.


 Define a function named [ getNumState() ]that takes a number and:
o Returns 1 if the number is odd.
o Returns 0 if the number is 0.
o Returns 2 if the number is even.
 Inside the main () function:
o Ask the user to enter a number.
o Call getNumState() to get the number state ( odd, even or zero).
o Use the switch statement to print the number and its state as shown below.

Sample Output

Page 4 of 5
PART III: Extra Lab Exercises
Exercise 1 –Character Classification: Check if a Character is a Letter

Write a C program that do the following:


 Write a function named is_Letter that takes as argument a single char and returns 1 if the
character is a letter or 0 otherwise.
 Write a main function that test the previous functions

Note:
 Capital letter Alphabets (A-Z) lie in the range 65-91 of the ASCII value
 Small letter Alphabets (a-z) lie in the range 97-122 of the ASCII value
 Any other ASCII value is a non-alphabetic character.

Sample Output

Enter Character
A
A is letter

Enter Character
5
5 is not letter

Exercise 2 –Modify Even Digits with Random Even Numbers

Write a C program that does the following:

 Write a function named modifyNumber that takes a 4-digit integer as an argument.


o The function should modify the number such that:
 If a digit is even, replace it with a random even number (between 0 and 8).
 If a digit is odd, leave it unchanged.
o The function should return the modified number.
 In the main function:
o Prompt the user to enter a 4-digit number.
o If the entered number is not a valid 4-digit number, display an error message and
exit.
o Call the modifyNumber function and display both the original and modified
numbers.

Sample Output

Enter a 4-digit number: 2486


Original number: 2486
Modified number: 2266

Page 5 of 5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy