PPS Chapter-3

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

Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

Subject
Programming for Problem Solving (3110003)

Chapter – 3
Control Structure in C

Prepared by – Prof. Viral H. Panchal 1


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

CONTENTS
1. INTRODUCTION
2. DECISION MAKING (BRANCHING) CONTROL STRUCTURES
2.1. Simple if statement
2.2. if … else statement
2.3. Nested if statement
2.4. if…else…if ladder (Multiple if…else) statement
2.5. Switch Statement
3. LOOPING CONTROL STRUCTURES
3.1. Types of Loops
3.2. while Loop
3.3. do…while Loop
3.4. Difference between while Loop and do-while Loop
3.5. for Loop
4. NESTING OF CONTROL STRUCTURES (NESTING OF LOOPS)
5. break STATEMENT
6. continue STATEMENT
7. goto STATEMENT
8. SOLVED PROGRAMS

Prepared by – Prof. Viral H. Panchal 2


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

1. INTRODUCTION
• The important and essential part of a programming language is their control
structures. The programming languages support following control structures.
o Decision making (Branching)
o Looping

Figure 1.1: Types of C control statements

• When a program breaks the sequential flow and jumps to another part of the
code, it is called branching.

• Conditional branching statements checks the condition first and then control is
transfer to another part of the code. Example: if, switch.

• Unconditional branching statements do not check the condition and control is


transferred to another part of the code. Example: goto, break, continue.

• Looping is the process of repeatedly executing a block of statements until some


conditions for the termination of the loop are satisfied.

Prepared by – Prof. Viral H. Panchal 3


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

2. DECISION MAKING (BRANCHING) CONTROL STRUCTURES

2.1. Simple if statement


• This is the simplest statement which checks a condition. If the condition is true then
the statements are executed, otherwise they are not executed.
• The syntax of the statement is:

if (condition)
statement(s);

• If there are more than one statements to be executed then they are put within { }
symbols.

2.2. if … else statement


• In certain cases, we want to execute one set of statements if condition is satisfied
and other set of statements if condition is false. In that case, we can use if … else
statement.
• The syntax is:
if (condition)
statement(s)1;
else
statement(s)2;

Prepared by – Prof. Viral H. Panchal 4


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

• If condition is true then statement(s)1 are executed, otherwise statement(s)2 are


executed.
• Following program explains the use of if statement.

/* Write a program to find larger of two numbers using if


statement */

#include <stdio.h>
#include <conio.h>
main ()
{
float num1, num2;
clrscr();
printf(“Enter first number\n”);
scanf(“%f”,&num1);
printf(“Enter second number\n”);
scanf(“%f”,&num2);
if(num1 > num2)
printf(“%f is larger\n”,num1);
else
printf(“%f is larger\n”,num2);
getch();
}

Output:
Enter first number
4.3
Enter second number
5.7
5.700000 is larger

Prepared by – Prof. Viral H. Panchal 5


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

2.3. Nested if statement


• For complex problems we need to check more than one condition. The if
statement can be used inside another if statement. This type of use of if statement
is called as nested if statement. The nesting can be up to any level.
• The syntax is:
if (condition1)
{
if (condition2)
statement(s)1;
else
statement(s)2;
}
else
{
statement(s)3;
}
statement(s)N;

• If the condition1 is false, the statement(s)3 will be executed; otherwise, it


continues to perform the second test. If the condition2 is true, the statement(s)1
will be evaluated; otherwise, the statement(s)2 will be evaluated and then the
control is transferred to the statement(s)N.

/* Write a program to find maximum of three numbers */

#include <stdio.h>
#include <conio.h>
main ()
{
int num1, num2, num3;
int max;
clrscr();
printf(“Enter three numbers\n”);
scanf(“%d%d%d”,&num1,&num2,&num3);
if (num1 > num2)
{
if (num1 > num3)
max = num1;
else
max = num3;
}
else

Prepared by – Prof. Viral H. Panchal 6


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

{
if (num2 > num3)
max = num2;
else
max = num3;
}
printf(“Maximum of %d %d and %d is = %d\n”,
num1,num2,num3,max);
getch();
}

Output-1:
Enter three numbers
32 43 15
Maximum of 32 43 and 14 is = 43

Output-2:
Enter three numbers
-5 -36 -54
Maximum of -5 -36 and -54 is = -5

2.4. if…else…if ladder (Multiple if…else) statement


• The two-way decision provided by if…else statement is not sufficient when we
are having many choices, C language provides a multiple if…else statement for
that purpose.
• The syntax is:
if (condition1)
statement(s)1;
else if (condition2)
statement(s)2;
else if (condition3)
statement(s)3;
.
.
.
else if (conditionN)
statement(s)N;
else
default_statement(s);

• From the syntax, it is very clear that only one set of statement(s) will be
executed, depending on the condition being true.

Prepared by – Prof. Viral H. Panchal 7


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a program which asks day number and prints


corresponding day name i.e., if input is 5, it will print the
fifth day of week which is Thursday. Sunday being the first day.
*/

#include <stdio.h>
#include <conio.h>
main ()
{
int day;
clrscr();
printf(“Enter day number between 1 – 7\n”);
scanf(“%d”,&day);
if (day == 1)
printf(“Sunday\n”);
else if (day == 2)
printf(“Monday\n”);
else if (day == 3)
printf(“Tuesday\n”);
else if (day == 4)
printf(“Wednesday\n”);
else if (day == 5)
printf(“Thursday\n”);
else if (day == 6)
printf(“Friday\n”);
else if (day == 7)
printf(“Saturday\n”);
else
printf(“Wrong input\n”);
getch();
}

Output-1:
Enter day number between 1 – 7
3
Tuesday

Output-2:
Enter day number between 1 – 7
8
Wrong input

Prepared by – Prof. Viral H. Panchal 8


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a program to check the category of given character.


Digit, Upper Case, Lower Case or Other symbol */

#include <stdio.h>
#include <conio.h>
main ()
{
char ch;
clrscr();
printf(“Enter one character\n”);
scanf(“%c”,&ch);
if ((ch >= ‘0’) && (ch <= ‘9’))
printf(“The character %c is digit\n”,ch);
else if ((ch >= ‘a’) && (ch <= ‘z’))
printf(“The character %c is Lower Case\n”,ch);
else if ((ch >= ‘A’) && (ch <=’Z’))
printf(“The character %c is Upper Case\n”,ch);
else
printf(“The character %c is other
symbol\n”,ch);
getch();
}

Output-1:
Enter one character
5
The character 5 is digit

Output-2:
Enter one character
b
The character b is Lower Case

Output-3:
Enter one character
H
The character H is Upper Case

Output-4:
Enter one character
*
The character * is other symbol

Prepared by – Prof. Viral H. Panchal 9


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

2.5. Switch Statement


• C language provides a switch statement. In a complex problem, if many
alternative values are available, then writing the code using multiple if…else
becomes lengthy and difficult to manage. At that time switch statement which is a
multi-way decision statement is handy because management of code becomes
easy.
• The syntax of switch statement is:

switch (variable_name or expression)


{
case value1 : statement(s)1;
braek;
case value2 : statement(s)2;
break;
.
.
.
case valueN : statement(s)N;
break;
default : default_statement(s);
}

• Here, statement(s)1 will be executed if the value of variable_name or expression


is value1. Similarly, statement(s)2 will be executed if the value of variable_name
or expression is value2.
• If the value of variable_name or expression does not match any values from
value1 to valueN, then the default_statement(s) are executed.
• It is to be noted that writing the default value and its corresponding
defaut_statement(s) is optional.
• The value of expression or variable written after the switch statement must be
either character or integer value.

/* Write a program which asks day number and prints


corresponding day name by using switch statement */

#include <stdio.h>
#include <conio.h>
main ()
{
int day;
clrscr();
printf(“Enter day number between 1 – 7\n”);

Prepared by – Prof. Viral H. Panchal 10


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

scanf(“%d”,&day);
switch (day)
{
case 1 :
printf(“Sunday\n”);
break;
case 2 :
printf(“Monday\n”);
break;
case 3 :
printf(“Tuesday\n”);
break;
case 4 :
printf(“Wednesday\n”);
break;
case 5 :
printf(“Thursday\n”);
break;
case 6 :
printf(“Friday\n”);
break;
case 7 :
printf(“Saturday\n”);
break;
default :
printf(“Wrong input\n”);
}
getch();
}

Output-1:
Enter day number between 1 – 7
4
Wednesday

Output-2:
Enter day number between 1 – 7
9
Wrong input

• If the code to be executed for different cases is same, then the code for different
cases can be grouped together by writing those cases with their values as
follows.

Prepared by – Prof. Viral H. Panchal 11


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

• For example, if x is an integer variable, whose valid values are from 1 and 3,
and if code for value 1 and value 3 are same, then it can be written like this.

switch (x)
{
case 1 :
case 3 :
statement(s);
break;
case 2 :
statement(s);
break;
default :
statement(s);
}

/* Write a program to print number of days in a given month. The


program requires month number (between 1 to 12) as an input and
the display the days in that month.
Here the code for many cases is same */

#include <stdio.h>
#include <conio.h>
main ()
{
int month;
clrscr();
printf(“Give month number between 1 – 12\n”);
scanf(“%d”,&month);
switch (month)
{

/* number of days in months 1,3,5,7,8,10 and 12 are


31, so same case for all */

case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
printf(“Number of days in month number %d is =
31\n”, month);

Prepared by – Prof. Viral H. Panchal 12


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

break;

/* number of days in months 4,6,9 and 11 are 30, so


same case for all */

case 4 :
case 6 :
case 9 :
case 11 :
printf(“Number of days in month number %d is =
30\n”, month);
break;
case 2 :
printf(“Number of days in month number %d is =
28 or 29\n”, month);
break;
default :
printf(“Wrong input\n”);
}
getch();
}

Output-1:
Give month number between 1 – 12
10
Number of days in month number 10 is = 31

Output-2:
Give month number between 1 – 12
14
Wrong input

Prepared by – Prof. Viral H. Panchal 13


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

3. LOOPING CONTROL STRUCTURES

3.1. Types of Loops


• Depending on where the condition is checked, we can have two types of loop
structures:
1. Entry Control
2. Exit Control
• In entry control loop, the condition is written first and then the body of statements.
While in exit control loop, the body of the statements is written first and then
condition is written. This means that body of statements in exit control loop will be
executed at least once.
• Figure 3.1.1 (a) explains Entry control loop while figure 3.1.1 (b) explains Exit
control loop.

Figure 3.1.1: Types of Loops

3.2. while Loop


• While loop is entry control loop. It is helpful, when we do not know in advance,
how many times the statements are to be repeated.
• The syntax of while loop is:

while(condition)
{
statement(s);
}
• The statements within the { } symbols are known as body part of the loop.

Prepared by – Prof. Viral H. Panchal 14


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

• The { } are not required if there is only one statement in the body part.
• Here, the condition is checked first and then only the statements are executed.
That’s why while loop is an entry-controlled loop.
• The statements in the body part will be executed till the condition is true.
• As soon as the condition becomes false, the control will come out of the while
loop. If the condition remains true always then it will lead to infinite loop. So,
writing a valid condition is important.
• For example, the while loop can be written as follows to print 1 to 10.
....
i=1;
while(i<=10)
{
printf(“%d\n”,i);
i=i+1;
}
....
• Here, first i is initialized to 1. The value of i is printed and then i is incremented.
This process is repeated if i is less than or equal to 10. Once the i is 11, the
condition in while is false and control comes out of loop.

/* Write a program to print the sum of first n integer numbers


using while loop */

#include <stdio.h>
#include <conio.h>
main ()
{
int n, sum=0;
int i=1;
clrscr();
printf(“Give integer number: ”);
scanf(“%d”,&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf(“Sum of first %d numbers = %d\n”,n,sum);
getch();
}
Output:
Give integer number: 6
Sum of first 6 numbers = 21

Prepared by – Prof. Viral H. Panchal 15


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

3.3. do…while Loop


• do…while is an exit control loop, where the condition is checked later.
• In do…while loop, the body of the loop will be executed at least once.
• Whenever you want to make sure that loop statements must execute at least one
time, you should use do…while loop instead of while loop.
• The syntax is:
do
{
statement(s);
} while(condition);

• Here, the statement(s) are executed till the condition is true. As soon as the
condition evaluates to false, the loop terminates and the next statement after
do…while loop is executed.
• All the programs written in while loop can always be written using do…while
loop.

/* Write a program to print the sum of first n integer numbers


using do…while loop */

#include <stdio.h>
#include <conio.h>
main ()
{
int n, sum=0;
int i=1;
clrscr();
printf(“Give integer number: ”);
scanf(“%d”,&n);
do
{
sum=sum+i;
i++;
} while(i<=n);
printf(“Sum of first %d numbers = %d\n”,n,sum);
getch();
}

Output:
Give integer number: 6
Sum of first 6 numbers = 21

Prepared by – Prof. Viral H. Panchal 16


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

3.4. Difference between while Loop and do-while Loop


while loop do-while loop
It is an entry-controlled loop. It is an exit-controlled loop.
Condition is given at the beginning of Condition is given at the end of
loop. loop.
Loop statements never execute if Loop statements execute once even
condition does not satisfy. if condition does not satisfy.
Syntax: Syntax:
while(condition) do
{ {
statement(s); statement(s);
} } while(condition);

3.5. for Loop


• If we do not know number of iterations required, we can use while or do…while
loop. But, in certain cases, we need to execute some steps certain number of
times. In such situations where we know the number of iterations in advance, then
we can use for loop.
• The syntax of for loop is:

for(initialization; condition; increment/decrement)


{
statement(s);
}

• We require one variable which is used to control the loop, which is called as
control variable.
• The control variable is initialized in initialization expression. This statement
executes only once.
• The condition expression checks the value of control variable. If the condition
expression is true, then the statements written are executed.
• After every execution of statements, the last expression increment/decrement is
executed. Then again, the condition is checked, and body is executed if the
condition evaluates to true.
• Loop terminates when the condition evaluates to false.

Prepared by – Prof. Viral H. Panchal 17


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

• For example, following for loop will print the numbers 1 to 10 each on separate
line.

....
....
for(i=1; i<=10; i++)
printf(“%d\n”,i);
....
....
• Here, i=1; statement is initialization which will execute only once.
• Then the second expression i<=10 will be evaluated. It is true, so loop body will
be executed i.e., printf() statement will be executed, then i++ statement will
execute, which will make i=2, then again the condition i<=10 will be evaluated,
which is true, so again printf() statement will be executed.
• This sequence will go on till the condition is true. When i becomes 11, at that time
condition evaluated to false, so loop is over.

/* Write a program to print the sum of first n integer numbers


using for loop */

#include <stdio.h>
#include <conio.h>
main ()
{
int n, i, sum=0;
clrscr();
printf(“Give integer number: ”);
scanf(“%d”,&n);
for(i=1; i<=n; i++)
{
sum=sum+i;
}
printf(“Sum of first %d numbers = %d\n”,n,sum);
getch();
}

Output:
Give integer number: 6
Sum of first 6 numbers = 21

Prepared by – Prof. Viral H. Panchal 18


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

4. NESTING OF CONTROL STRUCTURES (NESTING OF LOOPS)


• Nesting of loops means there is a loop inside a loop. The nesting can be up to
any level. For certain problems, nesting of loops is needed.
• When nesting is done, some care is required. The outer loop should not end
between the starting of inner loop and ending of inner loop.
• Following example shows nesting up to 2 levels.

• In above example, for(i=1; i<=3; i++) is outer for loop. We can say that
control variable for outer loop is ‘i’.
• for(j=1; j<=3; j++) is inner for loop. Variable ‘j’ is control variable for inner
for loop. For each value of outer loop control variable i.e., ‘i’, all the values of
inner loop control variable are tried i.e., ‘j’.
• Following table explains the set of values for variable ‘i’ and ‘j’ for above
example.
i j
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
• From above table, it is very clear that for i=1, j goes from 1 to 3. When all
values of j are done, i gets next value i.e., i=2, again j goes from 1 to 3.
Similarly for i=3, j goes from 1 to 3.

Prepared by – Prof. Viral H. Panchal 19


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a program to print the following pattern of stars


*
**
***
****
*****
.
.
*/

[In this program, from the pattern, it is clear that number of stars in line i is equal
to i. So this example can be solved using nesting of loops. The outside loop will
count number of lines and provide newline character while inner loop will count
number of starts in current line.]

#include <stdio.h>
#include <conio.h>
main ()
{
int i,j,n;
clrscr();
printf(“How many lines?\n”);
scanf(“%d”,&n);
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
{
printf(“*”);
}
printf(“\n”);
}
getch();
}

Output:
How many lines?
6
*
**
***
****
*****
******

Prepared by – Prof. Viral H. Panchal 20


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

5. break STATEMENT
• The break statement is used to jump out of a loop. When a break statement is
encountered inside a loop, the loop is immediately exited, and the program
continues with the statement immediately following the loop.
• Example:

main()
{
int i;
for (i=1;i<=10;i++)
{
if(i==6)
break;
printf(“%d ”,i);
}
getch();
}

Output:
1 2 3 4 5

6. continue STATEMENT
• The continue statement causes the loop to continue with the next iteration skipping
the remaining statements in that loop.
• Example:

main()
{
int i;
for(i=1;i<=5;i++)
{
printf(“%d”,i);
if(i==3)
continue;
printf(“**”);
}
getch();
}

Output:
1**2**34**5**

Prepared by – Prof. Viral H. Panchal 21


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

7. goto STATEMENT
• The goto statement changes the normal sequence of the program execution by
transferring the control to other part of the program.
• Example:

sample: .... ; //label


....
if()
{
....;
....;
}
else
goto sample; //goto statement

/* Write a program to print the sum of 1 to N using goto


statement */

#include <stdio.h>
#include <conio.h>
main ()
{
int i=1, sum=0, n;
clrscr();
printf(“Enter value of N\n”);
scanf(“%d”,&n);

next:
sum=sum+i;
i+=1;
if(i<=n)
goto next;
printf(“Sum = %d\n”,sum);
getch();
}

Output:
Enter value of N
4
Sum = 10

Prepared by – Prof. Viral H. Panchal 22


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

8. SOLVED PROGRAMS
/* Write a program to find out whether number is even or odd */

#include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
printf(“Enter any Number\n”);
scanf(“%d”,&a);
if(a%2==0)
printf(“The %d is even\n”,a);
else
printf(“The %d is odd\n”,a);
getch();
}

Output 1:
Enter any Number
4
The 4 is even

Output 2:
Enter any Number
9
The 9 is odd

/* Write a program to accept name and marks in 6 subjects, find


out percentage marks and print grade to students according to
following condition.
If student is failed in one subject, then grade is FAIL.
If Percentage >= 75 then Grade = Distinction.
If Percentage <75 and Percentage >=60, then grade = First Class.
If Percentage <60 and Percentage >=50, then grade = Second Class.
If Percentage <50 and Percentage >=40, then grade = Third Class.
*/
#include<stdio.h>
#include<conio.h>
main()
{
char name[40];
int m1,m2,m3,m4,m5,m6,total;

Prepared by – Prof. Viral H. Panchal 23


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

float percent;
clrscr();
printf(“Enter name of the student\n”);
scanf(“%s”,&name);
printf(“Enter marks in 6 subjects\n”);
scanf(“%d%d%d%d%d%d”,&m1,&m2,&m3,&m4,&m5,&m6);
total=m1+m2+m3+m4+m5+m6;
percent=total/6;
if((m1<40)||(m2<40)||(m3<40)||(m4<40)||(m5<40)||(m6<40))
printf(“Name: %s \nTotal: %d \nGrade: Fail”
,name,total);
else if(percent >= 75)
printf(“Name: %s \nTotal: %d \nGrade:
Distinction”,name,total);
else if(percent >= 60)
printf(“Name: %s \nTotal: %d \nGrade: First
Class”,name,total);
else if(percent >= 50)
printf(“Name: %s \nTotal: %d \nGrade: Second
Class”,name,total);
else
printf(“Name: %s \nTotal: %d \nGrade: Third
Class”,name,total);
getch();
}

Output 1:
Enter name of the student
Ajay
Enter marks in 6 Subjects
65 66 89 77 76 76
Name: Ajay
Total: 449
Grade: First Class

Output 2:
Enter name of the student
Radha
Enter marks in 6 Subjects
55 66 77 1 88 99
Name: Radha
Total: 386
Grade: Fail

Prepared by – Prof. Viral H. Panchal 24


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a program to print the individual digits of a given


integer number using while statement */

#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
clrscr();
printf(“Give Interger Number: “);
scanf(“%d”,&n);
while(n!=0)
{
i = n % 10; /* separate the digit */
printf(“The digit is = %d \n”,i);
n = n/10; /* new value of n after separation
of digit */
}
getch();
}

Output:
Give Integer Number: 15689
The digit is = 9
The digit is = 8
The digit is = 6
The digit is = 5
The digit is = 1

Prepared by – Prof. Viral H. Panchal 25


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a program to reverse a given integer number */

#include<stdio.h>
#include<conio.h>
main()
{
int num,i;
clrscr();
printf(“Give Integer Number\n“);
scanf(“%d”,&num);
printf(“Reverse of %d is = ”,num);
while(num!=0)
{
i = num % 10;
printf(“%d”,i);
num = num/10;
}
getch();
}

Output:
Give Integer Number
2156
Reverse of 2156 is = 6512

Prepared by – Prof. Viral H. Panchal 26


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a program to check the given number is Armstrong number


or not */
/* Armstrong number is equal to summation of cubes of its
individual digits. */

#include<stdio.h>
#include<conio.h>
main()
{
int n,sum=0; /* sum initialized to 0 */
int i,num; /* separated digit stored in i */
clrscr();
printf(“Give Integer Number: “);
scanf(“%d”,&n);
num = n; /* n stored in num, because n will change
in loop */
while(n!=0)
{
i = n % 10; /* separate the digit */
sum = sum + i*i*i; /* find cube of digit and
add to sum */
n = n/10; /* new value of n after separation
of digit */
}
if (sum == num)
printf(“Given number %d is Armstrong\n”,num);
else
printf(“Given number %d is not Armstrong\n”,num);
getch();
}

Output 1:
Give Integer Number
245
Given Number 245 is not Armstrong

Output 2:
Give Integer Number
153
Given Number 153 is Armstrong

Prepared by – Prof. Viral H. Panchal 27


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a program to print first N Fibonacci numbers */

#include<stdio.h>
#include<conio.h>
main()
{
int num1,num2,num3,n;
int count;
clrscr();
num1 = 0;
num2 = 1;
printf(“How many Fibonacci Numbers?\n”);
scanf(“%d”,&n);
if(n > 2)
{
printf(“The Fibonacci Numbers are\n”);
printf(“%d%5d”,num1,num2);
count = 2;
while(count < n)
{
num3 = num1 + num2;
printf(“%5d”,num3);
num1 = num2;
num2 = num3;
count++;
}
}
else
printf(“By definition first two numbers
are 0 1\n”);
getch();
}

Output 1:
How many Fibonacci Numbers?
9
The Fibonacci Numbers are
0 1 1 2 3 5 8 13 21

Output 2:
How many Fibonacci Numbers?
1
By definition first two numbers are 0 1

Prepared by – Prof. Viral H. Panchal 28


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a program to sum the individual digits of a given


positive number */

#include<stdio.h>
#include<conio.h>
main()
{
int n,sum=0;
int digit;
clrscr();
printf(“Give Positive Integer Number: “);
scanf(“%d”,&n);
printf(“Given Number = %d\n”,n);
if(n < 0)
printf(“Please Give Positive Number\n”);
else
{
do
{
digit = n % 10;
sum = sum + digit;
n = n/10;
} while(n > 0);
printf(“Summation of Individual Digits
= %d\n”,sum);
}
getch();
}

Output 1:
Give Positive Integer Number: 2314
Given Number = 2314
Summation of Individual Digits = 10

Output 2:
Give Positive Integer Number: -21
Given Number = -21
Please Give Positive Number

Prepared by – Prof. Viral H. Panchal 29


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a program to reverse a given integer number and also


check for palindrome */
/* Palindrome is a number which is same after reversal also */

#include<stdio.h>
#include<conio.h>
main()
{
int num,temp;
int i,rev=0;
clrscr();
printf(“Give Integer Number\n”);
scanf(“%d”,&num);
printf(“Reverse of %d is = “,num);
temp = num;
while(num != 0)
{
i = num % 10;
rev = rev * 10 + i;
num = num/10;
}
printf(“%d\n”,rev);
if(temp == rev)
printf(“Given Number %d is Palindrome\n” ,temp);
else
printf(“Given Number %d is not Palindrome\n”
,temp);
getch();
}

Output 1:
Give Integer Number
123
Reverse of 123 is = 321
Given Number 123 is not Palindrome

Output 2:
Give Integer Number
121
Reverse of 121 is = 121
Given Number 121 is Palindrome

Prepared by – Prof. Viral H. Panchal 30


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a program to find factorial of a given number */

#include<stdio.h>
#include<conio.h>
main()
{
int i,n;
long int fact;
clrscr();
printf(“Give Positive Integer Number\n”);
scanf(“%d”,&n);
if(n < 0)
printf(“Factorial of –ve number not
defined\n”);
else
{
fact = 1;
for(i=1;i<=n;i++)
fact = fact * i;
printf(“Factorial of %d = %ld\n”,n,fact);
}
getch();
}

Output 1:
Give Positive Integer Number
5
Factorial of 5 = 120

Output 2:
Give Positive Integer Number
-6
Factorial of –ve number not defined

Prepared by – Prof. Viral H. Panchal 31


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a program to print following pattern of numbers.


1
12
123
1234
12345
....
....
*/
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n;
clrscr();
printf(“How many lines?\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf(“%d”,j);
printf(“\n”);
}
getch();
}

Output:
How many lines?
5
1
12
123
1234
12345

Prepared by – Prof. Viral H. Panchal 32


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a program to print following pattern of numbers for n = 6.


1
12
123
1234
12345
123456
*/
/* Here, in first line 5 blanks are followed by number 1. In 3rd
line, 3 blanks are followed by number 123. So, in general, in line
i, n-i blanks are followed by number 1 to I */

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k,n;
clrscr();
printf(“How many lines?\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++) /* controls number of lines */
{
for(j=1;j<=n-i;j++) /* controls number of
blanks */
printf(“ “,j);
for(k=1;k<=i;k++) /* controls number
printing */
printf(“%d”,k);
printf(“\n”);
}
getch();
}

Output :
How many lines?
6
1
12
123
1234
12345
123456

Prepared by – Prof. Viral H. Panchal 33


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a program to print following pattern of numbers.


1
22
333
4444
55555
....
*/
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n;
clrscr();
printf(“How many lines?\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf(“%d”,i);
printf(“\n”);
}
getch();
}

Output :
How many lines?
6
1
22
333
4444
55555
666666

Prepared by – Prof. Viral H. Panchal 34


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a program to print following pattern using loop statement


for n row.
1
13
135
1357
.....
*/
#include<stdio.h>
#include<conio.h>
main()
{
int i,n,loop,count;
clrscr();
printf(“How many rows you want?\n”);
scanf(“%d”,&n);
for(loop=1;loop<=n;loop++)
{
for(i=1,count=1;count<=loop;i=i+2,count++)
printf(“%d”,i);
printf(“\n”);
}
getch();
}

Output:
How many rows you want?
5
1
13
135
1357
13579

Prepared by – Prof. Viral H. Panchal 35


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a C program to display the following triangle for N lines.


1
AB
123
ABCD
12345
*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,number=1,alpha=0,ch=’A’,loop,count;
clrscr();
printf(“How many rows you want?\n”);
scanf(“%d”,&n);
for(loop=1;loop<=n;loop++)
{
for(count=0;count<loop;count++)
{
if(alpha==0)
printf(“%d”,count+1);
else
printf(“%c”,ch+count);
}
printf(“\n”);
if(alpha==0)
alpha=1;
else
alpha=0;
}
getch();
}

Output :
How many rows you want?
5
1
AB
123
ABCD
12345

Prepared by – Prof. Viral H. Panchal 36


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

/* Write a C program to display the following triangle for N lines.


1
AB
234
CDEF
56789
*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,number=1,alpha=0,ch=’A’,loop,count;
int countn=1,counta=0;
clrscr();
printf(“How many rows you want?\n”);
scanf(“%d”,&n);
for(loop=1;loop<=n;loop++)
{
if(alpha==0)
{
for(count=0;count<loop;count++)
{
printf(“%d”,countn);
countn++;
}
}
else
{
for(count=0;count<loop;count++)
{
printf(“%c”,ch+counta);
counta++;
}
}
printf(“\n”);
if(alpha==0)
alpha=1;
else
alpha=0;
}
getch();
}

Prepared by – Prof. Viral H. Panchal 37


Programming for Problem Solving (3110003) Chapter – 3: Control Structure in C

Output:
How many rows you want?
5
1
AB
234
CDEF
56789

Prepared by – Prof. Viral H. Panchal 38

You might also like