You are on page 1of 41

C Programming

C for Loop

In programming, a loop is used to repeat a block of code until the specified condition is met.

C programming has three types of loops:

1. for loop
2. while loop
3. do...while loop
for Loop
The syntax of the for loop is:

for (initializationStatement; testExpression; updateStatement)


{
// statements inside the body of loop
}
How for loop works?
● The initialization statement is executed only once.
● Then, the test expression is evaluated. If the test expression is evaluated to false, the for loop
is terminated.
● However, if the test expression is evaluated to true, statements inside the body of the for loop
are executed, and the update expression is updated.
● Again the test expression is evaluated.

This process goes on until the test expression is false. When the test expression is false, the loop
terminates.
for loop Flowchart
// Print numbers from 1 to 10
#include <stdio.h>

int main() {
int i;

for (i = 1; i < 11; ++i)


{
printf("%d ", i);
}
return 0;
}

Output :-
1 2 3 4 5 6 7 8 9 10
1. i is initialized to 1.
for loop Flowchart 2. The test expression i < 11 is evaluated.
Since 1 less than 11 is true, the body of
// Print numbers from 1 to 10 for loop is executed. This will print the 1
#include <stdio.h> (value of i) on the screen.
3. The update statement ++i is executed.
int main() { Now, the value of i will be 2. Again, the
int i; test expression is evaluated to true, and
the body of for loop is executed. This will
for (i = 1; i < 11; ++i) print 2 (value of i) on the screen.
{ 4. Again, the update statement ++i is
printf("%d ", i); executed and the test expression i < 11 is
} evaluated. This process goes on until i
return 0; becomes 11.
} 5. When i becomes 11, i < 11 will be false,
and the for loop terminates.

Output :-
1 2 3 4 5 6 7 8 9 10
// Program to calculate the sum of first n natural numbers
Example 2: for loop // Positive integers 1,2,3...n are known as natural numbers

#include <stdio.h>
int main()
{
int num, count, sum = 0;

printf("Enter a positive integer: ");


Output :-
scanf("%d", &num);
Enter a positive integer: 10
Sum = 55
// for loop terminates when num is less than count
for(count = 1; count <= num; ++count)
{
sum += count;
}

printf("Sum = %d", sum);

return 0;
}
Example 2: for loop {
The value entered by the user is stored int num, count, sum = 0;
in the variable num. Suppose, the user
entered 10. printf("Enter a positive integer: ");
scanf("%d", &num);
The count is initialized to 1 and the
test expression is evaluated. Since the
test expression count<=num (1 less // for loop terminates when num is less than count
than or equal to 10) is true, the body of for(count = 1; count <= num; ++count)
for loop is executed and the value of {
sum will equal to 1.
sum += count;
Then, the update statement ++count is }
executed and count will equal to 2.
Again, the test expression is evaluated. printf("Sum = %d", sum);
Since 2 is also less than 10, the test
expression is evaluated to true and the
body of the for loop is executed. Now, return 0;
sum will equal 3. }
// Program to calculate the sum of first n natural numbers
Example 2: for loop // Positive integers 1,2,3...n are known as natural numbers

#include <stdio.h>
int main()
{
This process goes on and the sum is int num, count, sum = 0;
calculated until the count reaches 11.
printf("Enter a positive integer: ");
When the count is 11, the test scanf("%d", &num);
expression is evaluated to 0 (false),
and the loop terminates. // for loop terminates when num is less than count
for(count = 1; count <= num; ++count)
Then, the value of sum is printed on {
the screen. sum += count;
}

printf("Sum = %d", sum);

return 0;
}
Solve by using for loop:-
1. Write a C program to find the sum of first 10 natural numbers.
2. Write a program in C to read 10 numbers from keyboard and find their sum and average.
3. Write a program in C to display the multiplication table Horizontally of a given integer.
4. Write a program in C to display the multiplication table vertically from 1 to n.
5. Write a program in C to display the pattern like right angle triangle using an asterisk.

The pattern like :

**

***

****
6. Write a C program to calculate the factorial of a given number.
Solve by using for loop:-

7. Write a program in C to display the n terms of harmonic series and their sum.

1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms

Test Data :

Input the number of terms : 5

Expected Output :

1+1/1 + 1/2 + 1/3 + 1/4 + 1/5 +

Sum of Series upto 5 terms : 2.283334


Solve by using for loop:-
8. Write a program in C to make such a pattern like a pyramid with a number which will repeat the number in
the same row.

23

456

7 8 9 10
9. Write a program in C to display the sum of the series [ 9 + 99 + 999 + 9999 ...].
Test Data :
Input the number or terms :5
Expected Output :
9 99 999 9999 99999
The sum of the series = 111105
10 . Write a program in C to display the number in reverse order. Go to the editor

Test Data :

Input a number: 12345

Expected Output :

The number in reverse order is : 54321


C while loop
The syntax of the while loop is:

while (testExpression) {
// the body of the loop
}
Flowchart of while loop
Here, we have initialized i to 1.
// Print numbers from 1 to 5
1. When i = 1, the test expression i <= 5 is true.
#include <stdio.h>
Hence, the body of the while loop is executed.
int main() {
This prints 1 on the screen and the value of i is
int i = 1;
increased to 2.
2. Now, i = 2, the test expression i <= 5 is again
while (i <= 5) {
Output: true. The body of the while loop is executed
printf("%d\n", i);
1 again. This prints 2 on the screen and the
++i;
2 value of i is increased to 3.
}
3 3. This process goes on until i becomes 6. Then,
4 the test expression i <= 5 will be false and the
return 0;
5 loop terminates.
}
Solve by using While loop:-
1. C program to print ODD numbers from 1 to N using while loop.
2. C program to print EVEN numbers from 1 to N using while loop.
3. C program to print all uppercase alphabets using while loop.
4. C program to print all lowercase alphabets using while loop.
5. C Program to check entered number is ZERO, POSITIVE or
NEGATIVE until user does not want to quit.
6. C Program to find factorial of a number using while loop.
7. C program to print all prime numbers from 1 to N using while loop.
8. C program to print all leap years from 1 to N using while loop.
Solve by using While loop:-
9. C program to find HCF of two numbers by user input using
while loop.
Solve by using While loop:-

10 .C program to find LCM of two numbers by user input using while loop.
Switch statement -
Introduction

• The C switch case statement allows you to choose from many


statements based on multiple selections by passing control to one of the
case statements within its body.

• The switch statement executes the case


corresponding to the value of the expression.

• The switch statement can include any number of case instances.


Syntax
switch(Character or Integer expression)
{
case condition 1:
// Statement sequence(block 1);
break;
case condition 2:
// Statement sequence(block 2);
break;
default:
// Statement sequence(default block);
}
Break and Default Statement
● You can use the break statement to end processing of a particular case within the
switch statement and to branch to the end of the switch statement. Without break, the
program continues to the next case, executing the statements until a break or the end
of the statement is reached. In some situations, this continuation may be desirable.
● The default statement is executed if no case constant-expression is equal to the value
of switch (expression). If the default statement is omitted, and no case match is
found, none of the statements in the switch body are executed. There can be at most
one default statement. The default Statement need not come at the end; it can
appear anywhere in the body of the switch statement. A case or default label can only
appear inside a switch statement.
Break and Default Statement
● The type of switch expression and case constant- expression must
be integral. The value of each case constant-expression must be
unique within the statement body.

● The case and default labels of the switch statement body are
significant only in the initial test that determines where execution
starts in the statement body. Switch statements can be nested. Any
static variables are initialized before executing into any switch
statements.
SOME SWITCH QUESTIONS

1. Write a C program to print day of week name using switch case.


2. Write a C program print total number of days in a month using switch case.
3. Write a C program to check whether an alphabet is vowel or consonant using
switch case.
4. Write a C program to find maximum between two numbers using switch case.
5. Write a C program to check whether a number is even or odd using switch case.
6. Write a C program to check whether a number is positive, negative or zero using s
witch case.
7. Write a C program to find roots of a quadratic equation using switch case.
8. Write a C program to create Simple Calculator using switch case.
9 . Write a C program to take time between 6:00 AM to 8:00 PM
from the user and prints “Hello good morning” / “Good
AfterNoon” / “Good Evening” / “Good Night” as per entered time.(
use Switch statement somewhere in the program ).
Output
This is Greeting Program Please
Enter 1 for AM
Enter 2 for PM
2
Okay You wanna go with PM
Now enter time between 12 :00 PM to
8:00 PM
Enter hour : 7
Enter minute:56
Your Time is 7:56 PM

****Good Evening*******
Ques - Write a program to take input as given below and print it as
given format.

Sample Input -

21.6666

Sample Output -

21.67
21.6666
21.666600
Now Some MCQs
1.
#include<stdio.h> A. Loop Loop Loop Loop Loop
int main()
{ B. Infinite Loop
int i = 0;
while(i < 3, C. Loop Loop Loop
i = 0, i < 5)
{
D. Prints Nothing

printf("Loop ");

i++;
}
2.
#include<stdio.h>
A. Loop
int main()
{
B. Loop Loop
int i =4, j = 7;
while(++i < --j){
C. Loop Loop Loop
printf("Loop");}
D. Infinite Loop
return 0;
}
3.

A. Loop Loop Loop Loop


#include<stdio.h>
int main(){ B. Loop Loop loop
int i = 4;
while(i == 4--){ C. Compilation Error

printf("Loop ");} D. Prints Nothing


return 0;
}
4.
#include<stdio.h>
#define NULL 0
A. Prints Nothing
int main()
{
B. Loop
while (NULL ==
0)
C. Loop Loop
{
D. Compilation Error
printf("Loop");
break;
}
return 0;
}
do...while loop
The syntax of the do...while loop is:

The do..while loop is similar to the


while loop with one important do {
difference. The body of do...while // the body of the loop
loop is executed at least once. Only }
then, the test expression is while (testExpression);
evaluated.
The syntax of the do...while loop is:

do...while loop do {
// the body of the loop
}
How do...while loop works?
while (testExpression);
1. The body of do...while loop is
executed once. Only then, the
testExpression is evaluated.
2. If testExpression is true, the
body of the loop is executed
again and testExpression is
evaluated once more.
3. This process goes on until
testExpression becomes false.
4. If testExpression is false, the
loop ends.
Example 2: do...while loop
Here, we have used a do...while
// Program to add numbers until the user enters zero
loop to prompt the user to enter a
number. The loop works as long as
#include <stdio.h>
the input number is not 0.
int main() {
double number, sum = 0;
The do...while loop executes at
least once i.e. the first iteration
// the body of the loop is executed at least once
runs without checking the
do {
condition. The condition is checked
printf("Enter a number: ");
only after the first iteration has
scanf("%lf", &number);
been executed.
sum += number;
} do {
while(number != 0.0); printf("Enter a number: ");
scanf("%lf", &number);
printf("Sum = %.2lf",sum); sum += number;
}
return 0; while(number != 0.0);
}
Example 2: do...while loop
So, if the first input is a non-zero
// Program to add numbers until the user enters zero
number, that number is added to
the sum variable and the loop
#include <stdio.h>
continues to the next iteration. This
int main() {
process is repeated until the user
double number, sum = 0;
enters 0.
// the body of the loop is executed at least once
But if the first input is 0, there will
do {
be no second iteration of the loop
printf("Enter a number: ");
and sum becomes 0.0.
scanf("%lf", &number);
sum += number;
Outside the loop, we print the value
} do {
of sum.
while(number != 0.0); printf("Enter a number: ");
scanf("%lf", &number);
printf("Sum = %.2lf",sum); sum += number;
}
return 0; while(number != 0.0);
}
Solve the Questions using Do while
1. Write a program that reads a set of integers, and then prints the sum of the even
and odd integers.

2. Write a program to enter the numbers till the user wants and at the end the
program should display the largest and smallest numbers entered.

3. Write a program that generates a random number and asks the user to guess
what the number is. If the user's guess is higher than the random number,
the program should display "Too high, try again." If the user's guess is lower
than the random number, the program should display "Too low, try again."
The program should use a loop that repeats until the user correctly guesses
the random number.

4. Write a program to compute sinx for given x. The user should supply x and a
positive integer n. We compute the sine of x using the series and the
computation should use all terms in the series up through the term involving
xn

sin x = x - x3/3! + x5/5! - x7/7! + x9/9! .......


Solve the Questions using Do while
5. Write a c program to check whether a given number is a perfect number or
not.

Input the number : 56


Expected Output :
The positive divisor : 1 2 4 7 8 14
28
The sum of the divisor is : 64
So, the number is not perfect.
Solve the Questions using Do while
6. Write a c program to find the perfect numbers within a given number
of range.

Input the starting range or number : 1

Input the ending range of number : 50

Expected Output :

The Perfect numbers within the given range : 6 28


Solve the Question using Do while
7 . Write a program in C to display the pattern like a diamond.

*
***
*****
*******
*********
*******
*****
***
*
Solve the Question using Do while

8. Write a program in C to check whether a number is a palindrome


or not.

Test Data :

Input a number: 121

Expected Output :

121 is a palindrome number.

You might also like