0% found this document useful (0 votes)
24 views

L3-Managing Input - If Else - Looping - PGD!

Uploaded by

rayanrafin9
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)
24 views

L3-Managing Input - If Else - Looping - PGD!

Uploaded by

rayanrafin9
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/ 39

MANAGING INPUT/ OUTPUT

AND
CONTROL STATEMENTS
Md. Ariful Islam Khandaker
Assistant Professor, IICT
Khulna University of Engineering & Technology.
INPUT / OUTPUT OPERATOR
❑ All input/output operations are carried out through functions called as
printf and scanf.

❑ There exist several functions that have become standard for input and
output operations in C. These functions are collectively known as
standard i/o library.

Each program that uses standard I/O function must contain the statement

#include< stdio.h >

The file name stdio.h is an abbreviation of standard input-output header file.

6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
SAMPLE EXAMPLE
Problem: Create a program which will
calculate monthly salary of an employee
according to the following conditions.

1. Basic Salary
2. House Rent(45% of Basic Salary)
3. Medical Allowance(10% of Basic Salary)
4. Technical Allowance(5% of Basic Salary)
5. Tax payment(5% of Basic Salary)

6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
SOLUTION OF THE PROBLEM
#include <stdio.h>
#include <conio.h>
int main( ) {
float basic, hr, ma, ta, tax, salary;
printf (“ Enter Basic Salary\n”);
scanf ( “%f”, &basic);
hr = basic * .45;
ma = basic * .10;
ta = basic * .05;
tax = basic * .05;
salary = (basic + hr + ma + ta) – tax;
printf ( “ Monthly Salary is = %f “, salary );
return 0;
}
6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
READING AND WRITING A CHARACTER
❑ Reading a single character can be done by using the function getchar( ). The
getchar ( ) takes the following form:
variable_name = getchar( );
/*Reading a character from terminal*/
#include<stdio.h>
int main( )
{
char ans;

printf( “ Would you like to know my name? \n ” );


printf( “ Type Y for yes and N for no ” );

ans = getchar( );

if( ans == ’Y’ || ans = = ’y’ ) {


printf(“\n\n My name is India \n”); }
else {
printf(“\n\n You are good for nothing \n”); }
return 0;
}
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024
READING AND WRITING A CHARACTER (CONTD…)
❑ Like getchar( ), there is an analogous function putchar( ) for writing characters
one at a time to the terminal. It takes the form as shown below:

putchar (variable_name);
/*A program to read a character from keyboard and then prints it in reverse case*/

#include<stdio.h>
OUTPUT
#include<ctype.h> Enter An alphabet
int main() { a
char alphabet; A
printf(“Enter an alphabet”); Enter An alphabet
putchar(‘\n’); Q
alphabet = getchar(); q
if ( islower ( alphabet ) ) { Enter An alphabet
putchar ( toupper ( alphabet ) ); } z
Z
else {
putchar ( tolower ( alphabet ) ); }
return 0; }
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024
FORMATTED INPUT

Formatted input refers to an input data that has been arranged in a particular
format.
The general form of scanf is

scanf ( “control string” , arg1 , arg2 , ….arg n );

Inputting integer numbers


The field specification for reading an integer number is

%wd

Eg: scanf( “ %2d %5d ”, &num1, &num2);

An input field may be skipped by specifying * in the place of field width.

For eg: scanf( “%d %*d %d ”, &a, &b);

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


Program
/* Reading integer numbers */ OUTPUT
int main( ) {
int a, b, c, x, y, z; Enter three integer numbers
int p, q, r; 1 2 3
printf(“Enter three integer numbers \n”); 1 3 –3577
Enter two 4-digit numbers
scanf( “ %d %*d %d ”, &a, &b, &c);
6789 4321
printf(“ %d %d %d \n \n ”, a, b, c); 67 89
printf( “Enter two 4-digit numbers \n” ); Enter two integer numbers
scanf( “%2d %4d ” , &x, &y); 44 66
printf(“ %d %d \n \n” , x, y); 4321 44
printf(“Enter two integer numbers \n”); Enter a nine digit numbers
scanf(“%d %d”,&a, &x); 123456789
printf(“%d %d \n \n”,a, x); 66 1234 567
Enter two three digit numbers
printf(“Enter a nine digit numbers \n”);
123 456
scanf(“%3d %4d %3d”,&p, &q, &r); 89 123
printf(“%d %d %d \n \n”,p, q, r);
printf(“Enter two three digit numbers \n”);
scanf(“%d %d”,&x, &y);
printf(“%d %d \n \n”,x, y);
return 0; }
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024
Inputting Real Numbers

Unlike integer numbers, the field width of real numbers is not to be specified.

Therefore, scanf reads real numbers using simple specification %f for


both the notations,

namely, decimal point notation and exponential notation.

Eg: scanf( “ %f %f %e ”, &x, &y, &z);

If the number to be read is of double type, then the specification should


be %lf instead of simple %f.

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


Inputting Character
Strings

Following are the specifications for reading character strings:

%ws or %wc

Some versions of scanf support the following conversion specifications for


strings:

%[characters] and %[^characters]

Example: %[A-Z a-z] and %[^a-z]

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


FORMATTED OUTPUT

Output of Integer Numbers

The format specification for printing an integer number is

%wd
Output of Real Numbers

The output of real numbers may be displayed in decimal notation using the
following format specification:

%w.p f
The integer w indicates the minimum number of positions that are to be
used for the display of the value and the integer p indicates the number of
digits to be displayed after the decimal point.
We can also display real numbers in exponential notation by using the
specification
%w.p e
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024
FORMATTED OUTPUT

Format Output
printf ( “ %d “ , 9876 ); 9 8 7 6
printf ( “ %6d “ , 9876 ); 9 8 7 6
printf ( “ %2d “ , 9876 ); 9 8 7 6
printf ( “ %-6d “ , 9876 ); 9 8 7 6

printf ( “ %06d “ , 9876 ); 0 0 9 8 7 6


Real Number Output

Format Output
printf ( “ %7.4f “ , 98.7654 ); 9 8 . 7 6 5 4
printf ( “ %7.2f “ , 98.7654 ); 9 8 . 7 7
printf ( “ %-7.2f “ , 98.7654); 9 8 . 7 7

printf ( “ %10.2e “ , 98.7654); 9 . 8 8 e + 0


1
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024
CONTROL STATEMENTS

C language supports the following statements


known as control or decision making statements.
1. if statement
2. switch statement
3. conditional operator statement
4. goto statement

6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
CONDITIONAL STATEMENTS
IF STATEMENT
The if statement is used to control the flow of execution of statements and is
of the form
if(test expression)

It allows the computer to evaluate the expression first and then, depending on
whether the value of the expression is ‘true’ or ‘false’, it transfers the control to
a particular statement.
Entry

Test
Exprs
Flase

True
6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
CONDITIONAL STATEMENTS (CONT…)
The if statement may be implemented in different forms depending on the
complexity of conditions to be tested.
1. Simple if statement
2. if…..else statement
3. Nested if…..else statement
4. elseif ladder

❑ SIMPLE IF STATEMENT
If the test expression is true, the statement-block will be executed;
otherwise the statement-block will be skipped and the execution will jump
to the statement-x.
if(category = = SPORTS)
if(test exprn)
{
{
marks = marks + bonus_marks;
statement-block;
}
}
printf(“%f “,marks);
statement-x; ……….
……….
6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
CONDITIONAL STATEMENTS (CONT…)
❑ THE IF…ELSE STATEMENT
The if….else statement is an extension of simple if statement. The general form is

if ( test expression )
{
True-block statement(s)
}
else
{
False-block statement(s)
}
statement-x

If the test expression is true, then the true block statements are executed;
otherwise the false block statement will be executed.
6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
EXAMPLE OF IF AND IF…ELSE STATEMENT
#include <stdio.h> #include <stdio.h>

int main ( ) { int main ( ) {


int number; int number;
printf ( “ Enter any number\n”); printf ( “ Enter any number\n”);
scanf ( “%d”, &number); scanf ( “%d”, &number);

if (number < 0) { if (number < 0) {


printf ( “ %d is negative number\n”, printf ( %d is negative number\n”,
number); number);
} }
if (number > =0) { else {
printf ( “ %d is positive number \n”, printf (“%d is positive number\n”,
number); number);
} }
return 0; return 0;
} }

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


PRACTICE PROGRAMS
1. Write a program which will take a number as input and
display the number is even or odd.

[Hints: Use modulo operator (%) to test the number


has remainder or not]

2. Construct a program which will test a year is leap year


or not.

[Hints: Try to mod by 4 using modulo operator(%)]

6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
NESTING OF IF…..ELSE STATEMENTS
When a series of decisions are involved, we may have to use more than one
if….else statements, in nested form as follows.

if ( test condition 1) {

if ( test condition 2) {
statement 1;
}
else {
statement 3;
}
}
else {
statement 2;
}

6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
EXAMPLE OF NESTED IF…ELSE
/*Selecting the largest of three values*/
int main( ) {
float A, B, C;
printf(“Enter three values \n”); OUTPUT
scanf( “ %f %f %f ”, &A, &B, &C);
printf(“\nLargest value is\n:”); Enter three values:
if(A > B) False
{ 5 8 24
if (A > C)
printf(“%f \n”,A); Largest value is
else
printf(“%f \n”,C); 24
}
else True
{
if (C > B) True
printf(“%f \n”,C);
else
printf(“%f \n”,B);
}
return 0; }
6/27/2024 Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
ELSE IF LADDER
The general form is
if ( condition 1) {
statement 1;
}
else if ( condition 2) {
statement 2;
}
else if ( condition 3) {
statement 3;
}
…………………………….
……………………………..
else if ( condition n) {
statement n;
}

else {
default statement;
}

statement-x;
6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
COMPARISON BETWEEN IF…ELSE AND ELSEIF LADDER
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
int main( ) { int main(){
int mark; float mark;
printf(“ Enter Mark\n”);
scanf(“%d”, &mark); printf("Enter Mark\n");
if(mark>=80 && mark<=100) { scanf("%f",&mark);
printf( “Your Grade is=%s”, “A+”); } if(mark>=80 && mark<=100){
if(mark>=70 && mark<=79) { printf("Your Grade is=%s", "A+"); }
printf( “Your Grade is=%s”, “B+”); } else if(mark>=70){
if(mark>=60 && mark<=69) { printf("Your Grade is=%s", "B+"); }
printf( “Your Grade is=%s”, “B”); } else if(mark>=60){
if(mark>=50 && mark<=59) { printf("Your Grade is=%s", "B"); }
printf( “Your Grade is=%s”, “C+”); } else if(mark>=50){
if(mark>=0 &&mark<=49) { printf("Your Grade is=%s", "C+"); }
printf( “Your Grade is=%s”, “F”); } else {
return 0; printf("Your Grade is=%s", "F"); }
} return 0;
}

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


THE SWITCH STATEMENT
Switch statement is used for complex programs when the number of alternatives
increases.
The switch statement tests the value of the given variable against the list of case
values and when a match is found, a block of statements associated with that
case is executed.
The general form of switch statement is
switch(expression)
{
case value-1 :
block-1
break;
case value-2:
block-2
break;
…….
…….
default:
default-block
break;
}
statement-x;
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024
SWITCH STATEMENT Example
float marks;
int index = marks / 10;
switch ( index ) {
case 10:
case 9:
case 8:
printf( “Honours”);
break;
case 7:
case 6:
printf( “first division”);
break;
case 5:
printf(“second division”);
break;
case 4:
printf( “third division”);
break;
default:
printf(“Fail”); }
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024
THE GOTO STATEMENT

C supports the goto statement to branch unconditionally from one point of


the program to another.

The goto requires a label in order to identify the place where the branch is to
be made.
A label is any valid variable name and must be followed by a colon.

Forward Jumping Backward Jumping


Note: A label can be anywhere in the program, either before or after the
goto label; statement.

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


REASONS TO AVOID GOTO STATEMENT

Using goto statement makes the logic of the program


complex and tangled. In modern programming, goto
statement is considered a harmful construct and a bad
programming practice.

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


DECISION MAKING AND LOOPING
Looping
In looping, a statement or a sequence of statements are executed until some
conditions for termination of the loop are satisfied.

A program loop therefore consists of two segments, one known as the body
of the loop and the other known as the control statements.

Depending on the position of the control statements in the loop, a control


structure may be classified into two types
1. entry-controlled loop
2. exit-controlled loop.

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


ENTRY CONTROLLED AND EXIT CONTROLLED LOOP
Entry Controlled Loop Exit Controlled Loop

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


TYPES OF LOOPS

The C language provides for three loop constructs for performing loop
operations. They are

❑ The for Loop


❑ The do Loop
❑ The while Loop

for loop and while loop are entry controlled loop and do…while loop is exit
controlled loop.

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


FOR LOOP
The for loop entry-controlled loop that provides a more concise loop
control structure.

The general form of the for loop is

for (initialization ; test-condition ;


increment)
{
body of the loop
}
The execution of the for statement is as follows:
➢ Initialization of the control variables is done first.
➢ The value of the control variable is tested using the test-condition. If the
condition is true, the body of the loop is executed; otherwise the loop is
terminated and the execution continues with the statement that immediately
follows the loop.
➢ When the body of the loop is executed, the control is transferred back to
the for statement after evaluating the last statement in the loop. Now, the
control variable is either incremented or decremented as per the condition.
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024
SIMPLE EXAMPLE OF FOR LOOP
A simple program using for loop for displaying “KUET” 5 times

#include<stdio.h> OUTPUT

int main(){ KUET


int i; KUET
for(i=0; i<=5; i++) KUET
{ KUET
KUET
printf("KUET\n");
}
return 0;
}

Now it is your task to make it dynamic that is, how many time
“KUET” will print, it will depend on users.
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024
SPOT TEST
Create a program that will print n number to m numbers with increment
by 5.

Example:
Enter start number
100
Enter end number
120
It will display
100
105
110
115
120

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


EVEN-ODD PROGRAM WITHIN A RANGE
#include<stdio.h> OUTPUT
int main(){
int i, start, end; Display the EVEN NUMBER
printf(“Display the EVEN Enter start point
NUMBER"); 100
printf("\nEnter start point\n"); Enter end point
scanf("%d", &start); 120
printf("Enter end point\n"); EVEN Numbers are
scanf("%d", &end);
100 102 104 106
printf("EVEN Numbers are\n");
for(i=start; i<=end;i++)
108 110 112 114
{ 116 118 120
if(i%2==0) {
printf("%d\t",i); }
}
return 0;
}

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


FACTORIAL PROBLEM
❑ Write a program which will calculate factorial of N number.

Idea:

Factorial 5( 5!) means 5*4*3*2*1=1*2*3*4*5


Factorial 6( 6!) means 6 * 5 * 4 * 3 * 2 * 1 = 1 * 2 * 3 * 4 * 5 * 6
Factorial 7( 7!) means 7 * 6 * 5 * 4 * 2 * 1 = 1 * 2 * 3 * 4 * 5 * 6 * 7
……………………………………………………………………………..
……………………………………………………………………………...
Factorial N (N!) means N * ( N – 1) * ………….4 * 3 * 2 * 1

What is the solution???

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


SOLUTION OF FACTORIAL
#include<stdio.h>
int main(){
int i, f = 1, n;
printf("It is a Factorial Problem");
printf("\nEnter a number\n");
scanf("%d", &n);

for(i=1; i<=n;i++)
{
f = f * i;
}
printf("Factorial of %d is %d",n, f);

return 0;
}

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


ASSIGNMENT

1. Create a program which will calculate the summation of numbers


where starting points and end points are given by the users.

2. Create a program which will calculate the following series

❑ 12 + 22 + 32 + 42 +……………………………………………….. + n2
❑ 1/12 + 1/22 + 1/32 + 1/42 + …………………………………….. + 1/n2

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


NESTING OF FOR LOOPS

Example

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


PYRAMID PROBLEM
#include<stdio.h>

int main( ){ OUTPUT


int i, j, n; It is a Pyramid Problem
Enter a number
printf("It is a Pyramid Problem"); 5
printf("\nEnter a number\n"); *
scanf("%d", &n); **
for(i = 1; i< = n; i++) {
***
for( j = 1; j<= i; j++) { ****
printf("*"); } *****
printf("\n");
}
return 0;
}

Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024


THANKS TO ALL…

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