0% found this document useful (0 votes)
1 views35 pages

Control Structures (while, do-while, for loop)

Control structures for C program

Uploaded by

mridul-25024001
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)
1 views35 pages

Control Structures (while, do-while, for loop)

Control structures for C program

Uploaded by

mridul-25024001
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/ 35

Control Structures

(while, do-while, for loop)

2
Fig: packing robot
Do you remember?
•Sequencing - putting instructions in the right order
•Repetition - repeating one small task many times
Ref:
https://www.bbc.co.uk/bitesize/topics/zs7s4wx/articles/zxjsfg8
3
Different types of control structures in
C
• Sequential :default mode. Sequential execution of code statements (one
line after another).
• Selection :used for decisions, branching -- choosing between 2 or more
alternative paths.
• if
• if else
• switch
• Iteration/repetition :used for looping, i.e. repeating a piece of code multiple
times
• while
• do/while
• for

4
•Iteration/repetition :used for looping, i.e. repeating a piece of code
multiple times

while Loop

The statements within the loop must modify variables


that are used in the condition; otherwise, the value of
the condition will never change and we will never be
able to exit the loop.

NB: An infinite loop is generated if the condition in


a while loop is always true.
5
Write a program to generate a conversion table for converting degrees to
radians (note that the degree values start at 0°, increment by 10°, and go
through 360°):

Pseudocode

6
7
Example: Write a C program to determine whether a number is
prime or not.
• A prime number is said to be prime if it is divisible only by 1 or itself.
• Logic: All we have to do to test whether a number is prime or not, is to
divide it successively by all numbers from 2 to one less than itself.
• If remainder of any of these divisions is zero, the number is not a prime.
• If no division yields a zero then the number is a prime number.

8
# include <stdio.h>
int main( )
{
int num, i ;
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
i=2;
while ( i <= num - 1 )
{
if ( num % i == 0 )
{
printf ( "Not a prime number\n" ) ;
break ;
}
i++ ;
}
if ( i == num )
printf ( "Prime number\n" ) ; 9
Example: Write a Program to compute x to the
power n using while loop.

10
11
do…while Loop

• The condition is tested at the end of the loop instead of


at the beginning of the loop.
• The do/while loop is always executed at least once;

12
for Loop

13
Example

14
Perfect Number

15
16
Prime Number

17
18
Write a C program to print N-th Fibonacci number

19
20
21
22
23
24
Multiplication Table

25
26
27
28
29
30
31
32
33
34
Practice Problem 1:
Write a C program to generate a maintenance schedule for a fleet of N
aircraft over a period of twelve months. The program will determine and
display whether each aircraft requires maintenance in each month based on
specific scheduling criteria:

•The maintenance requirements are determined by the following rules:


Even-numbered aircraft require maintenance every 3rd month.
•Odd-numbered aircraft require maintenance every 5th month.
•If an aircraft does not meet the criteria for maintenance, the output
should indicate "No Maintenance".

35
Practice Problem 2:
Write a C program that simulates the tracking of fuel consumption for N
different aircraft models over a period of seven days. The program should
prompt the user to input the fuel consumption for each aircraft model each
day and display the results. The program also to calculate and display the
average fuel consumption for each model over the week after all data has
been entered.

36

You might also like