Unit 2 (A)
Unit 2 (A)
Control
Statements
#include <stdio.h>
int main() {
int number;
printf(“Good");
return 0;
}
Ex – Program to find the largest number of the three.
1.#include <stdio.h>
2.int main()
3.{
4. int a, b, c;
5. printf("Enter three numbers?");
6. scanf("%d %d %d",&a,&b,&c);
7. if(a>b && a>c)
8. {
9. printf("%d is largest",a);
10. }
11. if(b>a && b > c)
12. {
13. printf("%d is largest",b);
14. }
15. if(c>a && c>b)
16. {
17. printf("%d is largest",c);
18. }
19. if(a == b && a == c)
20. {
21. printf("All are equal");
22. }
23.}
Ex – Program to check whether a person is eligible to
vote or not.
1.#include <stdio.h>
2.int main()
3.{
4. int age;
5. printf("Enter your age?");
6. scanf("%d",&age);
7. if(age>=18)
8. {
9. printf("You are eligible to vote...");
10. }
11. else
12. {
13. printf("Sorry ... you can't vote");
14. }
15.}
Ex – Check whether an integer is odd or even
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
return 0;
}
1.#include <stdio.h>
2.int main() Ex – Program to calculate the grade
3.{ of the student according to the
4. int marks; specified marks.
5. printf("Enter your marks?");
6. scanf("%d",&marks);
7. if(marks > 85 && marks <= 100)
8. {
9. printf("Congrats ! you scored grade A ...");
10. }
11. else if (marks > 60 && marks <= 85)
12. {
13. printf("You scored grade B + ...");
14. }
15. else if (marks > 40 && marks <= 60)
16. {
17. printf("You scored grade B ...");
18. }
19. else if (marks > 30 && marks <= 40)
20. {
21. printf("You scored grade C ...");
22. }
23. else
24. {
25. printf("Sorry you are fail ...");
26. }
Loops
• The looping can be defined as repeating
the same process multiple times until a
specific condition satisfies.
Why?
It enables us to alter the flow of the
program so that instead of writing the
same code again and again, we can repeat
the same code for a finite number of times.
Ex- Print first 10 natural numbers.
Types of C Loops
• There are three types of loops in
C language that is given below:
1.do while
2.while
3.for
do-while loop
• The do-while loop continues until a
given condition satisfies. It is also
called post tested loop.
Syntax:
1.do{
2.//code to be executed
3.}while(condition);
prints numbers from 1 to 5
1.#include <stdio.h>
2.int main() {
3.inti = 1;
4.do {
5.printf("%d\n", i);
6.i++;
7.} while (i<= 5);
8.return 0;
9.}
prints the multiplication table of a
given number N
1. #include <stdio.h>
2. int main() {
3. int N;
4. printf("Enter a number to generate its multiplication table: "
);
5. scanf("%d", &N);
6. inti = 1;
7. do {
8. printf("%d x %d = %d\n", N, i, N * i);
9. i++;
10.} while (i<= 10);
11.return 0;
12.}
while loop
• The while loop in c is to be used in the
scenario where we don't know the number of
iterations in advance. The block of
statements is executed in the while loop
until the condition specified in the while loop
is satisfied. It is also called a pre-tested loop.
Syntax:
1.while(condition){
2.//code to be executed
3.}
Flowchart
program of while loop that prints table of 1.
1.#include<stdio.h>
2.int main(){
3.int i=1;
4.while(i<=10){
5.printf("%d \n",i);
6.i++;
7.}
8.return 0;
9.}
print table for the given number
using while loop in C
1. #include<stdio.h>
2.int main(){
3.int i=1,number=0,b=9;
4. printf("Enter a number: ");
5. scanf("%d",&number);
6.while(i<=10){
7. printf("%d \n",(number*i));
8. i++;
9. }
10.return 0;
11.}
Examples
1.#include<stdio.h>
2.void main ()
3.{
4. while()
5. {
6. printf("hello Javatpoint");
7. }
8.}
for loop
• The for loop is used in the case where we
need to execute some part of the code until
the given condition is satisfied. The for loop
is also called as a per-tested loop. It is
better to use for loop if the number of
iteration is known in advance.
Syntax:
1.for(initialization;condition;incr/decr){
2.//code to be executed
3.}
Flowchart
program of for loop that prints
table of 1.
1.#include<stdio.h>
2.int main(){
3.int i=0;
4.for(i=1;i<=10;i++){
5.printf("%d \n",i);
6.}
7.return 0;
8.}
Print table for the given number
using C for loop
1. #include<stdio.h>
2.int main(){
3.int i=1,number=0;
4. printf("Enter a number: ");
5. scanf("%d",&number);
6.for(i=1;i<=10;i++){
7. printf("%d \n",(number*i));
8. }
9.return 0;
10.}
Examples
1.#include <stdio.h>
2.int main()
3.{
4. int a,b,c;
5. for(a=0,b=12,c=23;a<2;a++)
6. {
7. printf("%d ",a+b+c);
8. }
9.}
Factorial
• Factorial Program using loop
• Factorial Program using recursion
Factorial Program using loop
1. #include<stdio.h>
2. int main()
3. {
4. int i,fact=1,number;
5. printf("Enter a number: ");
6. scanf("%d",&number);
7. for(i=1;i<=number;i++){
8. fact=fact*i;
9. }
10. printf("Factorial of %d is: %d",number,fact);
11.return 0;
12.}
Factorial Program using recursion
1. #include<stdio.h>
2. long factorial(int n)
3. {
4. if (n == 0)
5. return 1;
6. else
7. return(n * factorial(n-1));
8. }
9. void main()
10. {
11. int number;
12. long fact;
13. printf("Enter a number: ");
14. scanf("%d", &number);
15. fact = factorial(number);
16. printf("Factorial of %d is %ld\n", number, fact);
17. return 0;
18. }
Jump Statements
• Break
• Continue
• Goto
Break Statement
• The break statement is used to terminate the loop or statement
in which it present. After that, the control will pass to the
statements that present after the break statement, if available.
If the break statement present in the nested loop, then it
terminates only those loops which contains break statement.
• The break statement can also be used to jump out of a loop.
• Examples:
int i;