Documente Academic
Documente Profesional
Documente Cultură
Assumptions:
Pattern 1
1 2 3 4
* 1 *
** 2 * *
*** 3 * * *
**** 4 * * * *
i j
1 1
2 2
3 3
4 4
We got the relation as i==j so the j loop should execute till j becomes equal
to i.
i.e. j<=i
Code:
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<4;i++)
{
for(j=1;j<=i;j++)
{
printf(" *");
}
printf("\n");
}
}
1
123
12345
1234567
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<5;i++)
{
for(j=1;j<5-i;j++)
{
printf(" ");
}
for(j=1;j<=2*i-1;j++)
{
printf("%d",j);
}
printf("\n");
}
}
switch(expression1)
{
case expression 2: statement 1;
statement 2;
statement 3;
statement n;
case expression m:
statement 1;
statement 2;
statement 3;
statement n;
default: statement 1;
}
The expression 1 is the switch variable. It can be of either type integer or
character.
The expression 2 to expression m will be either an integer of character enclosed in
single quotes and are referred as case labels.
For example
case 1 or case a
Here statement n will be break statement to jump out the switch statement, if not
used the following cases will be executed automatically.
The last statement of switch will be default it is generally used to handle the
values for which we dont have the cases and is generally contains the error
messages.
case 'B':
case 'b': c=a*a;
printf("Area of Square is %f",c);
break;
default : printf("Wrong Choice");
}
return;
}
Write a program using switch case to find the greatest among the given
two numbers
#include<stdio.h>
void main()
{
char choice;
int a,b,c;
printf("Enter Value of a");
scanf("%d",&a);
printf("Enter Value of b");
scanf("%d",&b);
switch(a<b)
{
case 0 : printf("A is grater than B");
break;
Break Statement:
The break statement is used to terminate the execution of loops and to exit from
the switch statement. It is a simple statement as
break;
The example of break in switch has been already discussed.
Now, we will discuss the example of use of breaks in loops.
# include <stdio.h>
int main(){
float num,average,sum;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
break; //for loop breaks if num<0.0
sum=sum+num;
}
average=sum/(i-1);
printf("Average=%.2f",average);
return 0;
}
Continue Statement
The continue statement is used to skip the remaining statement of current iteration
loops and directly jump to the next iteration. It is a simple statement as
continue;
Wap a program to find the product of n numbers and skip the particular number if it
is zero.
# include <stdio.h>
int main(){
float num,average,sum=1;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num==0.0)
continue; //for loop skip the iteration if num==0.0
sum=sum*num;
}
printf("Average=%.2f",sum);
return 0;
}
Arrays:
Types of arrays
In example one array elements are assigned the value 1,2,3,5,6 respectively.
In second example we have not specified the size so the size of array will be two
based on the element to be assigned.
In third case first element is assigned a value 1 and remaining element are assigned
value 0. That is the default value for remaining element but if no element is
assigned a value it will be equal to garbage.