Class 2
Class 2
Class 2
9. #include <stdio.h>
int main()
{
int c = 5, no = 10;
do {
no /= c;
} while(c--);
int main()
{
int n,i=1, sum=0;
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);
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;
{
case 1: c=a+b; //syntax: case value1: statements; break;
printf("The result is=%f\n",c);
break;
printf("program is done!!");
}
int main()
{
int a,b,c;
char op;
{
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;
printf("program is done!!");
}
int main() {
int i;
int number, sum = 0;
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: