0% found this document useful (0 votes)
80 views9 pages

Class 2

The document discusses various C programming concepts like control structures, loops, switch case statements, break and continue statements. It provides examples of using loops like for, while, do-while loops. It explains the output of programs using different loop conditions, initialization, increment/decrement. Examples of switch case are provided to explain different cases, default case. Break and continue statements are discussed with examples to show how they terminate/skip the current loop iteration. Practice problems on various algorithms using loops and switch case are listed at the end.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views9 pages

Class 2

The document discusses various C programming concepts like control structures, loops, switch case statements, break and continue statements. It provides examples of using loops like for, while, do-while loops. It explains the output of programs using different loop conditions, initialization, increment/decrement. Examples of switch case are provided to explain different cases, default case. Break and continue statements are discussed with examples to show how they terminate/skip the current loop iteration. Practice problems on various algorithms using loops and switch case are listed at the end.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Department of Computer Science and Engineering

Amrita School of Engineering, Bengaluru


Topic: Control Structures-Loops
1. Observe the output of the following programs:
1. #include<stdio.h>
int main()
{
float a=5.2;
if(a == 5.2)
{
printf("equal");
}
else
{
printf("not equal");
}
}
NOTE:Float data type doesnt work with == operator , hence the output is not equal.
The same way %(modulo operator) also can’t be used with float data type.

2. What will be the output of the following C program?


#include <stdio.h>
Int i=10;
void main()
{
int i =20,n;
for(n=0;n<=i;n++)
{
int i=10;
i++;
}
printf("%d", i);
}
3. What will be output of this C snippet?
void main()
{
int i,j=1;
for(i=0;i<10;i++);
{
j=j+1;}
printf("%d %d",i,j);
}
4. Find the output of the c program
#include <stdio.h>
void main()
{
int i=20,j,k=0;
for(j=1;j >i;j++)
{
k+=j<10?4:3;
}
printf("%d", k);
}

5. Find the output?


#include<stdio.h>
int main()
{
int i = 1, j = 1;
for(--i && j++ ; i<10; i+=2)
{
printf("loop ");
}
return 0;
}
6. Find the output of given program below
#include<stdio.h>
int main()
{
for(5;2;2)
printf("Hello");
return 0;
}
7. Find the output of given program below
#include<stdio.h>
int main()
{
for(5;2;2) ;
printf("Hello");
return 0;
}
8. int main()
{
int x = 3, k;
while (x-- >= 0) {
printf("%d", x);
}
return 0;
}

9. #include <stdio.h>
int main()
{
int c = 5, no = 10;
do {
no /= c;
} while(c--);

printf ("%d\n", no);


return 0;
}
10. int main()
{
int i;
for(i=0;i<5;i++);
{
printf("hello\n");
}
}
NOTE: Semicolon isn't an "operator". It terminates a "statement", and it's valid for the statement to contain nothing.
o/p : hello
11. int main()
{
int i;
for(i=0;i<5;i++)
{
printf("hello\n");
}
}
o/p:
hello
hello
hello
hello
hello
12. Observe the output
int main()
{
int i;
for(;i<5;)
{
printf("hello\n");
}
}
13. Observe the output
int main()
{
int i;
for(i=0;i<5;)
{
printf("hello\n");
}
}
14. Observe the output
int main()
{
int i;
for(; ;i++)
{
printf("hello\n");
}
}
15. while
#include<stdio.h>

int main()
{
int n,i=1, sum=0;

printf("enter the value of n\n");


scanf("%d",&n);

while(i<=n) //syntax while(expression){ body of the loop} initilization of the loop variable, condition
check, loop variable updation
{
sum=sum+i;
i=i+1;
}
printf("the sum is=%d\n",sum);
}
16. do while
#include<stdio.h>

int main()
{
int n,i=1, sum=0;
printf("enter the value of n\n");
scanf("%d",&n);
do //synatax do{body of the loop} while(condition);
{
sum=sum+i;

i=i+1;
}while(i<=n);

printf("the sum is=%d\n",sum);


}

Switch case:
1. Find the output of the following code:
#include <stdio.h>
int main()
{
int i = 0;
switch (i)
{
case '0': printf("Choice is zero");
break;
case '1': printf("Choice is one");
break;
default: printf("Default Choice");
}
return 0;
}
O/P:

2. Switch case with menu options and expression returning integer,and explain various options of cases,
case not having content , case having more that one ststemnet etc...
#include<stdio.h>

int main()
{
float a,b,c;
int choice;

printf("1: Addition 2: Sub 3: Mul 4: Div\n");


printf("Enter a\n");
scanf("%f",&a);
printf("Enter b\n");
scanf("%f",&b);
printf("Enter your operator/choice\n");
scanf("%d",&choice);
switch(choice) //syntax: switch(expression/variablename) {case 1, case1......, default}

{
case 1: c=a+b; //syntax: case value1: statements; break;
printf("The result is=%f\n",c);
break;

case 2: c=a-b; //syntax: case value2: statements; break;


printf("The result is=%f\n",c);
break;

case 3: c=a*b; //syntax: case value3: statements; break;


printf("The result is=%f\n",c);
break;
case 4: c=a/b; //syntax: case value4: statements; break;
printf("The result is=%f\n",c);
break;
default: printf("invalid choice\n"); //syntax: default : statemnets;
}

printf("program is done!!");
}

3. Switch case with menu options and expression returning integer


#include<stdio.h>

int main()
{
int a,b,c;
char op;

//printf("1: Addition 2: Sub 3: Mul 4: Div\n");


printf("Enter a\n");
scanf("%d",&a);
printf("Enter b\n");
scanf("%d",&b);
printf("Enter your operator\n");
scanf(" %c",&op);

switch(op) //syntax: switch(expression/variablename) {case 1, case1......, default}

{
case '+': c=a+b; //syntax: case value1: statements; break;
printf("The result is=%d\n",c);
break;
case '-': c=a-b; //syntax: case value2: statements; break;
printf("The result is=%d\n",c);
break;

case '*': c=a*b; //syntax: case value3: statements; break;


printf("The result is=%d\n",c);
break;
case '/':c=a/b; //syntax: case value4: statements; break;
printf("The result is=%d\n",c);
break;
default: printf("invalid choice\n"); //syntax: default : statemnets;
}

printf("program is done!!");
}

3. Find the output of the give program


#include <stdio.h>
int main()
{
int ip = 5;
switch (ip)
{
case 0+1: printf("Blue");
break;
case 1+2: printf("Red");
break;
case 2+3: printf("Green");
break;
default: printf("Yellow");
}
return 0;
}

Break and Continue


1. //This program calculates the sum of a maximum of 5 numbers.
// if the user enters a negative number, the break statement is executed. This will end the for loop, and
the sum is displayed.
//he break statement ends the loop immediately when it is encountered. Its syntax is:

//The break statement ends the loop immediately when it is encountered.


#include <stdio.h>

int main() {
int i;
int number, sum = 0;

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


{
printf("Enter a n%d: ", i);
scanf("%d", &number);

// if the user enters a negative number, break the loop


if (number < 0) {
break;
}

sum += number; // sum = sum + number;


}

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

2. #include<stdio.h>
int main()
{
int i;
for(i=1; i<=10; i++)
{

if(i==5)
{
continue; //skips the current iteration and continue with next iteration
}
//break;
printf("%d\t", i); //1 2 3 4 break
// 1 2 3 4 6 7 8 9 10 continue
}

}
Practice questions:

1. Write a C program to find roots of a quadratic equation using switch case.


2. Write a C program that prints a triangle of stars. Print 1 star in row 0, 3 stars in row 1, 5 stars in
row 2, and so on using loops.Print if a given number is an Armstrong number or not.
3. Print factorial of all the numbers between a given range.
4. Print the Fibonacci series upto n.
5. Print the Fibonacci series upto n terms.
6. Program to swap the first and the last digit of a given number.
7. Print the product of all the numbers entered by the user, until the user enters any value divisible
by 5.
8. Print all the prime numbers between a given range.
9. Print multiplication table for all the numbers between a given range.
10. Print the sum of the following series upto n terms 1/1! + 2/2! + ……. + n/n!.
11. Print the sum of the following series upto n terms 1/1! + 22/2! +……….. + nn/n!.

You might also like