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

C-Lang Programss List - If, Switch, Loops, Break and Continue

This document discusses programs on if-else, switch, and loop constructs in C language. It includes examples of programs using simple if-else, nested if-else, switch, nested switch, and loops. Some key programs covered are to find even/odd numbers, greatest of three numbers, check leap year, billing programs, and programs to find factors and primality of numbers. The document provides over 25 code examples to demonstrate the usage of various branching and iteration statements in C.

Uploaded by

rakeshbairi2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

C-Lang Programss List - If, Switch, Loops, Break and Continue

This document discusses programs on if-else, switch, and loop constructs in C language. It includes examples of programs using simple if-else, nested if-else, switch, nested switch, and loops. Some key programs covered are to find even/odd numbers, greatest of three numbers, check leap year, billing programs, and programs to find factors and primality of numbers. The document provides over 25 code examples to demonstrate the usage of various branching and iteration statements in C.

Uploaded by

rakeshbairi2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Programs on if-else, switch and loops

Contents
S.No. Content Page No.

1. Simple-if and if –else ................................................................................................. 2

2. Nested-if (if-else with in if-else) ............................................................................... 2

3. Switch .......................................................................................................................... 4

4. Nested switch ............................................................................................................. 5

5. LOOPS—CONCEPT .................................................................................................. 6

6. Programs on digits of a number-- Loops ............................................................... 8

7. Nested loop ............................................................................................................... 10

8. Programs to use break statement .......................................................................... 13

9. Programs to use continue statement ..................................................................... 14

Programs on Branching and Iteration statements in 'C'-language. (1CSM1- AY 2023-24) Page 1 of 14


1. Simple-if and if –else
1. Write a program to verify whether the given number is in the range of 1-20 or
not
2. Write a program to verify whether the given no. is even or odd (using simple -if
and if –else).
3. Write a program to display whether the given no. is even or odd (using simple –
if and if-else).
4. Write a program to read two values and display a message if they are equal
(using simple-if).
5. Write a program to read two values and display whether they are equal or not
(using if-else).
6. Write a program to display whether the given no. is divisible by 7 or not.
7. Write a program to verify whether the given no. is a factor of 6.
8. Write a program to verify the given no. is divisible by 2,3and 4.
9. Write a program to verify whether the given no. is divisible by 11 or 7.
10. Write a program to display the smallest no. among three no’s. (using simple-if).
11. Write a program to verify whether the given age is eligible for voting or not.
12. Write a program to verify whether the given character is vowel or consonant.
13. Write a program to verify whether the student has passed in all three
subjects(pass mark:40).
14. Write a program to read two no’s and display the greatest no.
15. Write a program to read two no’s and display the smallest no.
16. Write a program to read two no’s and display the smallest and greatest .

2. Nested-if (if-else with in if-else)

1. Write a program to display the greatest among three no’s.


2. Write a program to display the greatest among two no’s. Check even for equality.
3. Write a program to display smallest no. among three no’s (integer type
variables).
4. Write a program to smallest no. among three given no’s (floating value).
5. Write a program to display smallest no. among the four no’s.

Programs on Branching and Iteration statements in 'C'-language. (1CSM1- AY 2023-24) Page 2 of 14


6. Write a program to display the colour name depending on the given value
1 blue
2 red
3green
4 yellow
other no.: white
7. Write a program to display the grade of a student by considering average marks
100-91→ A
90-81→B
80-71→C
70-61→D
60-51→E
<40→fail.
8. Write a program to read three subject marks and display total, average and
grade.if the student is failed in at least in one subject, the grade is fail.(pass
marks=40).
9. Write a program to display whether the given year is leap year or not.
10. Write a program to generate the current bill by reading present and previous
readings
no. of units cost per unit

0-50 2.90
51-100 3.25
101-150 4.88
151-200 5.63
201-350 7.38
>350 8.38
11. write a program to read the no. of items in each type and generate a bill with the
discount from following data
type cost
saree→2000
shirt→750
pant→1250
shoes→895

Programs on Branching and Iteration statements in 'C'-language. (1CSM1- AY 2023-24) Page 3 of 14


total amount discount

2000to<3000→10%
3000to<5000→20%
>=5000→30%

3. Switch
1. write a program to read two no’s and display +or –or *or / or remainder
depending on user choice(menu driven program)

output should be as followed:


1. addition
2. subtraction
3. multiplication
4. quotient
5. remainder
2. write a program to read two values and display the requested calculation.
request the user enter the choice either +, -, *, /, %
3. write a program to read two no’s and display +or –or *or /or remainder
depending on user choice for the following options
addition, subtraction, multiplication, division(use floating type values).
4. write a program to read a character and display it is vowel or consonant.
5. write a program to read a value and display in words.
6. Write a program to find the area of rectangle, square, triangle, circle depending
on user choice(menu driven program).
7. Write a program for the following information:
100
90-99
80-89→A
70-79
60-69→B
50-59
40-49→C
<40→fail.

Programs on Branching and Iteration statements in 'C'-language. (1CSM1- AY 2023-24) Page 4 of 14


4. Nested switch
1. Write a program to read the user choice is either tea or coffee.
If the option is tea then again ask the user regarding type of tea(green tea, black
tea, lemon tea).
If the option is coffee then again ask the user regarding the type of coffee(bru
coffee, sunrise coffee, cold coffee, Nescafe coffee). Display the appropriate
message.
2. Write a program to find either area or perimeter.
If the option is area then find the area of either rectangle, triangle or square.
If the option is perimeter then find the perimeter of rectangle, triangle or circle.
Read the required input and display the request value.

Verify whether the following program can be written with switch or if-else.
3. Write a program for comparing range of no’s
100-95→A
94-75→B
74-40→C
<40→fail.

Some programs on if-else with characters ,ASCII values

1. i/p o/p
a → A
B → b

2. verify whether the given character is: alphabet or digit or a special character.

Programs on Branching and Iteration statements in 'C'-language. (1CSM1- AY 2023-24) Page 5 of 14


5. LOOPS—CONCEPT

1. Write a program to display your name for five times.


2. Write a program to display “pen” for five times. Even print the count in every
line before name.
3. Write a program to display your name for ten times. Even print the count in
every line after the name.
4. Write a program to display the first 15 natural no’s.
5. Write a program to display the value from 10 to 1 vertically.
6. Write a program to display the value from 0.0 to 1.0.
7. Write a program to display from 1 to 10 horizontally.
8. Write a program to display from 10 to 1 horizontally.
9. Write a program to display the sum of first 10 natural no’s.
10. Write a program to display the factorial of given no.
11. WHAT IS INFINITE LOOP..?
12. Write a program to find the sum and average of first 20 no’s.
13. Write a program to display first 10 even natural no’s.
14. Write a program to display the sum of even no’s from 1 to 10.
15. Write a program to display the sum of odd no’s from 1 to 20.

16. Write a program to find the sum of even no’s and sum of odd no’s from 1 to 10
17. Write a program to count number of even numbers from 1 to 10.
18. Write a program to display the even no’s, sum of even no’s, average of even no’s from 1 to
40.
19. Write a program to display odd no’s, sum of odd no’s, average of odd no’s from 1 to 15.
20. Write a program to display odd no’s in horizontal manner and in next line even no’s
from 1 to 10 as follow
output:
13579
2 4 6 8 10
21. Write a program to display even numbers, sum of even numbers, average of even
numbers, odd numbers, sum of odd numbers, average of odd no’s from 1 to 10 as follow:
output:
even no’s: 2 4 6 8 10
sum of even no’s = 30

Programs on Branching and Iteration statements in 'C'-language. (1CSM1- AY 2023-24) Page 6 of 14


average of even no’s = 6
--------------------------------
Odd no’s: 1 3 5 7 9
Sum of odd no’s = 25
Average of odd no’s = 5

22. Write a program to display the factors of a given no.


23. Write a program to count no. of factors for a given no.
Eg:
i/p: 12 i/p: 11
o/p: 6 o/p: 2
24. Write a program to verify whether the given no. is prime or not.
25. Write a program to display the sum of factors for a given no.
Eg:
6➔1+2+3=6
26. Write a program to verify whether the given no. is perfect number or not
27. Write a program to display the multiplication table of a given no.
28. Write a program to print ASCII values and ASCII characters from 32 to 126.

29. WAP to find the sum of squares of numbers from 1 to 10.


30. Write a program to find the result of x power y.
31. Write a program to evaluate the following expression
V = P(1+r)n
Where p is the principal amount, r is rate of interest, and n is the number of years.
32. WAP to find the LCM of 2 given numbers.
33. WAP to find the GCD of two given numbers.
34. WAP to display Fibonacci series upto N numbers.
35. WAP to display Fibonacci series upto Nth number.

Programs on Branching and Iteration statements in 'C'-language. (1CSM1- AY 2023-24) Page 7 of 14


6. Programs on digits of a number-- Loops

36. Write a program to consider no. as 123 and display the digits of no. in reverse
order.
o/p:
n = 123
3
2
1
37. Write a program to read a no. and display the digits of that no. in reverse order.
38. Write a program to read a no. and display the digits of that no. in words.
39. Write a program to read a no. and display the digits of that no. in words (in
reverse order).
40. Write a program to find the length of a given no.
41. Write a program to find the reverse of the number (example n=123, output: 321.)
42. Write a program to read a no. and display the no. in words.
43. Write a program to read the sum of digits of a given number.
44. Write a program to find the sum of even digits for a given no.
45. Write a program to find the sum of odd digits for a given no.
46. Write a program to find the sum and average of even and odd digits for a given
no.
47. Write a program to find the sum of digits of a given no. except first and last digits.
48. Write a program to find the sum of first and last.
49. Write a program to find the no. of even and odd digits for a given no.
50. Write a program to read a no. and find whether the given no. is palindrome or
not.
51. Write a program to find given no. is Armstrong no. or not.
52. WAP to find the digital root of a number. (Example: n=1283; o/p: 5)
53. WAP to check whether given number is strong number or not.

Programs on Branching and Iteration statements in 'C'-language. (1CSM1- AY 2023-24) Page 8 of 14


54. WAP to display the output as follow:
sum of 1 to 1= 1
sum of 1 to 2= 3
sum of 1 to 3= 6
sum of 1 to 4= 10

55. WAP to display the output as follow:


1! = 1
2! = 2
3! = 6
4! = 24

56. WAP to display as follow

1 10
2 9
3 8
4 7
5 6
6 5
7 4
8 3
9 2
10 1

Programs on Branching and Iteration statements in 'C'-language. (1CSM1- AY 2023-24) Page 9 of 14


7. Nested loop

1. Write a program to display as follow (using simple loop and nested loop).
1) 1 2 3 4 5 2) 1 1 1 1 1 3) 1 2 3 4
12345 11111 123
12345 11111 12
12345 11111 1

4) _ _ _ _ 5 5) 5 4 3 2 1
_ _ _4 5 54321 6) 1 0 1 0 1
__345 54321 0 101
_2345 54321 10 1
12345 01
1

7) 1 9) 4
12 8) 4 3 2 1 43
123 321 432
1234 21 4321
12345 1
123456

10) 11) 12)


1 55555 54321
22 4444 4321
333 333 321
4444 22 21
55555 1 1

Programs on Branching and Iteration statements in 'C'-language. (1CSM1- AY 2023-24) Page 10 of 14


13) 1 14) 1 2 3 4 5 15) 5
21 2345 45
321 345 345
4321 45 2345
54321 5 1 2 3 4 5

16) 5 17) 5 4 3 2 1 18) 1


54 5432 23
543 543 456
5432 54 7 8 9 10
54321 5

19) 1
12 20) 4 3 2 1 21) 5 4 3 2 1
123 321 _4321
1234 21 __321
4321 1 ___21
321 23 ____1
21 234
1 2345

22) 1 1 1 1 23) _ _ _ 4 24) 3 3 3 3 3


_111 __34 222
__11 _234 1
___1 1234

Programs on Branching and Iteration statements in 'C'-language. (1CSM1- AY 2023-24) Page 11 of 14


25) 4 4 4 4 4 4 4 26) 1 27) 1
33333 222 22
222 33333 333
1 4444444 4444

29) 1 2 3 4 5 30) 1 1
28) 1 2345 12 21
23 345 123 321
456 45 1234 4321
7 8 9 10 5 123454321

32) 1 1 1 1 1 1 1 33) 1 2 3 4 5 4 3 2 1
31) 1 22222 1234 4321
21 333 123 321
321 4 12 21
4321 1 1

34) 1 1 1 1 1
11111
11111

35. WAP to display PRIME numbers with in given range.


36. WAP to display PERFECT numbers with in given range.
37. WAP to display ARMSTRONG numbers with in given range.

Programs on Branching and Iteration statements in 'C'-language. (1CSM1- AY 2023-24) Page 12 of 14


8. Programs to use break statement

1. Write a program to read a value and display whether it is prime or not.use


the break statement whenever necessary.
2. Write a program to find the sum between 1 to 100. Stop the process when
sum exceeds 501 also display the value of loop control variable when the sum
exceeds.
3. Write a program to find the sum from 1 to 50 and stop the process if the sum
is 55 and also display the value on which the sum has reached 55.
4. Write a program to find the total and average of 6 given subjects. Stop
reading the values if the marks is not in the range of 0 to 100 and display
error message saying that the marks should be within the range of 0 to 100.
Display the total, average and grade if all the subjects are within the range
also verify whether the student has passed in all the subjects or not.

5. Write a program to read 6 subject marks and display whether the student has
passed in all subjects or not, if the student has failed then display the total no.
of subjects he had failed (note: don’t create variable for each subject). Stop
reading the values if the marks is not in the range of 0 to 100.

6. Find LCM of 2 numbers (use break and infinite loop concepts.)

Programs on Branching and Iteration statements in 'C'-language. (1CSM1- AY 2023-24) Page 13 of 14


9. Programs to use continue statement
1. Write a program to display as follow
o/p:1
2
3
4
5
6
8
9
10
End
2. Write a program to display no’s from 1 to 20 skip the values when the no. is
divisible by 4.
3. Write a program to read 10 values and display the sum of only positive
values.
4. Write a program to read 6 subject marks and display no. of subjects passed
and no. of subjects failed, if the marks entered is not in the range of 0 to 100
then display the error message and again read the same subject marks.
5. Write a program to display no’s from 1 to 10 but skip the values 5 and 7.

Programs on Branching and Iteration statements in 'C'-language. (1CSM1- AY 2023-24) Page 14 of 14

You might also like