[go: up one dir, main page]

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

C Tutorial Day 7

The document discusses control structures in C programming, focusing on decision-making statements such as 'if', 'if-else', and 'switch-case'. It provides algorithms and corresponding C code examples for calculating billing amounts, finding the greatest of two numbers, and determining the first day of a given year. Additionally, it includes exercises for practicing various programming tasks related to control structures.

Uploaded by

Deepak
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)
9 views10 pages

C Tutorial Day 7

The document discusses control structures in C programming, focusing on decision-making statements such as 'if', 'if-else', and 'switch-case'. It provides algorithms and corresponding C code examples for calculating billing amounts, finding the greatest of two numbers, and determining the first day of a given year. Additionally, it includes exercises for practicing various programming tasks related to control structures.

Uploaded by

Deepak
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/ 10

Programming With C-Control Structure

Control Structure
It is sometime necessary to skip some statement/s or sometime we need to repeat some statements. For
these actions, certain control statements are available. These statements alter the flow of control.
Decision making statements
a. if statement
b. if __ else __ statement
c. switch _ case __ default statement
d. goto statement
if statement
Discount of 20% is offered on the purchase of Rs. 250/- or more. Find billing amount.
This can be written as in algorithm:
1.Begin
2. Display ”Enter purchase amount : ”
3. Accept pamt
4. Discount  0.0
5. If pamt ≥ 250 Then
6. Discount  pamt * 0.20
7. End if

Created and verified By: B. K. Deepak 53


Dated:20/12/2004
Rev:00
Programming With C-Control Structure

8. Bill_amt=pamt - Discount
9. Display ”Billing amount is Rs: ” , Bill_amt
10. End

Line 1 is the, begin of function block.


Line 2 is used to display the message to the user asking for input data.
Line 3 is used to accept the input given by the user and store it in the address of variable pamt.
Line 4 is initializing the value of variable Discount as zero.
Line 5 is a conditional statement where it is checked that, whether the value of pamt is greater than or equal
to 250. The value of which may be true or false.
Line 6 will be only evaluated if the value of conditional statement at line 5 becomes true.
Line 7 is end of If statement.
Line 8 is calculating the value of Bill_amt.
Line 9 is used to display the Bill_amt as output.
Line 10 is end of program.

Created and verified By: B. K. Deepak 54


Dated:20/12/2004
Rev:00
Programming With C-Control Structure

It can be written in C language as follows:


/* Program to calculate Billing amount */
/* bill.c */
#include <stdio.h>
void main()
{
float pamt, bill_amt, discount;
printf(”\n\tEnter purchase amount : ”);
scanf(”%f”, &pamt);
discount=0.0;
if( pamt >= 250 )
discount=pamt * 0.20;
bill_amt=pamt – discount;
printf(”\n\tBill amount is Rs. : ”, bill_amt);
}
/* end of program */
The above C program is written according the algorithm.

Created and verified By: B. K. Deepak 55


Dated:20/12/2004
Rev:00
Programming With C-Control Structure

1. if ( pamt >= 250.00 )


discount= pamt * 0.20;

Test Expression False


example of single statement body.

if is the keyword
True pamt>=250.00 is the test expression
Body of if
discount= pamt * 0.20; is statement of if body

2. if ( pamt >= 550.00 )


{
discount= pamt * 0.20;
gift_amt= pamt * 0.05;
}
Statement
Example of compound statement body

Created and verified By: B. K. Deepak 56


Dated:20/12/2004
Rev:00
Programming With C-Control Structure

if ____ else STATEMENT

Find out greatest among two numbers. /* great.c */


#include <stdio.h>
Algorithm can be written as : void main()
Begin {
int num_a, num_b, num_great;
Display ”Enter two numbers : ”
Accept num_a, num_b printf(”\n\tEnter two numbers : ”);
scanf(”%d %d”, &num_a, &num_b );
If (num_a > num_b ) Then
num_great  num_a if ( num_a > num_b )
num_great=num_a;
else
else
num_great  num_b num_great=num_b;
end if
printf(”\n\tThe greatest number is : %d”, num_great);
Display ”The greatest number is : ” , num_great
}
End
/* end of program */

Created and verified By: B. K. Deepak 57


Dated:20/12/2004
Rev:00
Programming With C-Control Structure

1.

if ( test expression)
False statement;
Test Expres-
sion else
statement;
True
Body of if Body of else 2.

if ( test expression )
{
statements;
}
statement else
{
statements;
}
3.
Statement inside if can also be if statement

Created and verified By: B. K. Deepak 58


Dated:20/12/2004
Rev:00
Programming With C-Control Structure

switch ___ case statement


A switch statement allows user to choose a statement or group of statement among several alternatives. The
switch statement is useful when a variable is to be compared with different constants, and in case it is equal
to a constant, a set of statements are to be executed. The case statement is used to compare the values. It
may be char or int data type only. Syntax
1. switch(n)
{
case 1:
statement;
statement;
break;
case 2:
statement;
break;
...
...
default : statement;
}

Created and verified By: B. K. Deepak 59


Dated:20/12/2004
Rev:00
Programming With C-Control Structure

Problem: WAP and algorithm to find out first day of any year.
(Hint:1st January 1900 was Monday.)

Ans:

Begin
Display ”Enter any year between 1900 to 2099 : ”
Accept year
Diff  year – 1900
Leap  Diff/4
Diff  Diff + leap
If year is leap year Then
Diff  Diff – 1
End if
Diff  remainder of (Diff / 7)
If Diff = 0 Then
Day  Monday
Else if Diff = 1 Then
Day  Tuesday
Else if Diff = 2 Then
Day  Wednesday
Else if Diff = 3 Then
Day  Thursday
Else if Diff = 4 Then
Day  Friday
Else if Diff = 5 Then
Day  Saturday
Else if Diff = 6 Then
Day  Sunday
End if
Display “The day is : “, Day
End

Created and verified By: B. K. Deepak 60


Dated:20/12/2004
Rev:00
Programming With C-Control Structure

/* firstday.c */
#include <stdio.h>
void main()
{
int year,diff,leap;
printf(“\n\t Enter year in YYYY format range 1900 -2099 : “);
scanf(“%d”, &year);
diff = year – 1900;
leap = diff / 4 ;
diff = diff + leap ;
if ((( year % 4 == 0) && ( year % 100 !=0)) || (year % 400 == 0))
diff = diff – 1;
diff = diff % 7 ;
switch ( diff )
{
case 0 : printf(“\n\t It is Monday”);
break;
case 1 : printf(“\n\t It is Tuesday”);
break;
case 2 : printf(“\n\t It is Wednesday”);
break;
case 3 : printf(“\n\t It is Thursday”);
break;
case 4 : printf(“\n\t It is Friday”);
break;
case 5 : printf(“\n\t It is Saturday”);
break;
case 6 : printf(“\n\t It is Sunday”);
break;
} /* end of switch statement */
} /*end of program */

Created and verified By: B. K. Deepak 61


Dated:20/12/2004
Rev:00
Programming With C-Control Structure

Exercise:
1. Write a Program to find out greatest among three numbers. Accept input through keyboard.
2. WAP to find out lowest among three numbers. Accept input through keyboard.
3. WAP to reverse digits of a five digit integer number. Test whether it is palindrome?
4. WAP to find out middle number among three numbers. Accept input through keyboard.
5. Write a program to determine is given year is the leap year.
6. Write a program to accept number and check whether it is odd or even.
7. WAP to find out roots of a quadratic equation.
8. WAP to display whether a person can vote or not in general election 2019. Accept age as input.
9. WAP to display greetings as per choice of user input. E.g. For Holi greetings user will enter H or h and
program will display Happy Holi.
10. WAP to find out sum of first 10 Integer number from 1.
11. WAP to find out sum of first 100 integer number from 1.

Created and verified By: B. K. Deepak 62


Dated:20/12/2004
Rev:00

You might also like