Lab report on CSE , C loop operations

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

CSE 1202: Computer Programming Sessional

EXPERIMENT NO: 03
EXPERIMENT NAME: Study of basic input/ output and logical operation using C programming
OBJECTIVES
• To familiar with input-output operator
• To understand the Decision Statements (if, if-else, if-else if ladder, switch-case)
THEORY

Formatted Functions
The formatted input/output functions read and write all types of values

Input Output
scanf( ) printf( )

Unformatted Functions
The unformatted input/output functions only work with the character data type
Input Output
getch()
putch()
getche()
putchar()
getchar()
put()
gets()

if statement:
The general syntax of simple if statement is:
if (condition)
statement_to_execute_if_condition_is_true;
or
if (condition)
{
statement 1;
statement 2;
_ _ _ _;
}
if – else statement:
The if statement is used to execute only one action. If there are two statements to be executed
alternatively, then if-else statement is used.
The general syntax of simple if - else statement is:

if (condition)
statement_to_execute_if_condition_is_true;
else
statement_to_execute_if_condition_is_false;
CSE 1202: Computer Programming Sessional

Where, statement may be a single statement, a block, or nothing, and the else statement is optional.
It is important to remember that an if statement in C can execute only one statement on each branch
(T or F). If we desire that multiple statements be executed on a branch, we must block them inside
of a {and } pair to make them a single compound statement.
Flowchart Segment:

Example:
main()
{
int num;
printf(“ Enter a number :”);
scanf(“%d”,&num);
if (num % 2 == 0)
printf(“Even Number ”);
else
printf(“ Odd Number”);
}

Nested if statement:
The ANSI standard specifies that 15 levels of nesting must be supported. In C, an else statement
always refers to the nearest if statement in the same block and not already associated with if.
The marks obtained by a student in 5 different subjects are input through the keyboard. The student
gets a division as per the following rules:

Percentage above or equal to 60 - First division


Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 - Fail
Write a program to calculate the division obtained by the student.

Solution using nested if-else:

main( )
{
int m1, m2, m3, m4, m5, per ;
printf ( "Enter marks in five subjects " ) ;
CSE 1202: Computer Programming Sessional

scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ;


per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )
printf ( "First division ") ;
else
{
if ( per >= 50 )
printf ( "Second division" ) ;
else
{
if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "Fail" ) ;
}
}
}

if-else-if Ladder:
In this case every else is associated with its previous if. The last else goes to work only if all the
conditions fail. Even in else if ladder the last else is optional.
main( )
{
int m1, m2, m3, m4, m5, per ;
per = ( m1+ m2 + m3 + m4+ m5 ) / per ;
if ( per >= 60 )
printf ( "First division" ) ;
else if ( per >= 50 )
printf ( "Seconddivision" ) ;
else if ( per >= 40 )
printf ( "Thirddivision" ) ;
else
printf ( "fail" ) ;
}

Note that the else if clause is nothing different. It is just a way of rearranging the else with the if
that follows it.

The switch case statement:


The switch-case statement is used when an expression ‘s value is to be checked against several
values. If a match takes place, the appropriate action is taken. The general form of switch case
statement is:
CSE 1202: Computer Programming Sessional

switch (expression)
{
case constant1:
statement;
break;
case constant2:
statement;
break;
default:
statement;
break;
}

The break statements inside the switch statement are optional. When a break statement is
encountered, control proceeds to the end of the switch - case statement. If the break statement is
omitted, execution will continue on into the next case statements even though a match
has already taken place until either a break or the end of the switch is reached. The keyword
case may only be constants, they cannot be expressions. They may be integers or
characters, but not floating point numbers or character string. Case constants may not be
repeated within a switch statement.

Example 1:
main()
{
char gender;
printf (“Enter Gender code:(M/F):”);
scanf (“%c”, &gender);
switch (gender)
{
case ‗M‘ : printf (“Male”);
break;
case ‗F‘ : printf (“Female”);
break;
default : printf (“Wrong code”);
}
}

Try to understand the code. Another example is given below.


/* Program to evaluate simple expressions of the form number operator number */

#include <stdio.h>
int main (void)
{
float value1, value2;
CSE 1202: Computer Programming Sessional

char operator;
printf ("Type in your expression.\n");
scanf ("%f %c %f", &value1, &operator, &value2);
switch (operator)
{
case '+':
printf ("%.2f\n", value1 + value2);
break;
case '-':
printf ("%.2f\n", value1 - value2);
break;
case '*':
printf ("%.2f\n", value1 * value2);
break;
case '/':
if ( value2 == 0 )
printf ("Division by zero.\n");
else
printf ("%.2f\n", value1 / value2);
break;
default:
printf ("Unknown operator.\n");
break;
}
return 0;
}

LAB WORK:

1. Write a program to determine whether a year is leap year (a year is leap if it is divisible by 4
and not by 100 or divisible by 400.). You should take the year as input from keyboard.

i) Use simple if-else.


ii) Use only nested if-else but do not use logical operator.
iii) Use switch-case.

2. Use nested if-else to determine the largest number among 4 numbers. Do not use any logical
operator.

HOME WORK:

1. Write a program to read the values of coefficients a, b and c of a quadratic equation ax2 + bx +
c = 0 and find roots of the equation. Consider all possible cases with appropriate messages.
CSE 1202: Computer Programming Sessional

2. Write a program to determine the grade of a student in a subject if its mark is given.

i) Use if-elseif ladder.


ii) Use nested if-else but do not use any logical operator.

The grading system is given below

Marks Grade
≥80 A+
75~ ˂80 A
70~ ˂75 A-
65~ ˂70 B+
60~ ˂65 B
55~ ˂60 B-
50~ ˂55 C
40~ ˂50 D
˂40 F

3. Write a program to take a character input from keyboard and check if it is a number or alphabet
(uppercase or lowercase) or special character i) using ASCII CODE. ii) using
functions below:
a. Alphanumeric => isalnum()
b. Blank character => isblank()
c. Alphabetic => isalpha()
d. Control character => iscntrl()
e. Number-digit => isdigit()
f. Upper case => isupper()
g. Lower case => islower()

4. The program reads four values a, b, c, and d from the terminal and evaluates the ratio of (a + b)
to (c-d) and prints the result, if c –d is not equal to zero.

5.
CSE 1202: Computer Programming Sessional

6.

7. Write a c program that has the capability to show the result of a candidate with his name and roll by
taking the marks of certain subjects and roll number.

8. Write a C program that takes the temperature as centigrade and displays a suitable message for the
weather.

9. Write a C programming to determine whether he is eligible to cast his vote or not.

You might also like