C Nov Class4.
C Nov Class4.
C Nov Class4.
50:60;
1 2 1 3 2 3 4 4 5 5
| |--| |--| | |--| |--|
|----------------|
=3>4?(2>1?10:5>6?20:30):(6>8?40:3>2?50:60
);
=6>8?(40):(3>2?50:60);
=3>2?50:60
=50
?=5
:=5
|-------------------|
a=2?1?-1?10?20:30:40:50:60 ;
1 2 3 4 1 2 3 4
| | |--| | |
| |--------| |
|--------------|
2?(1?-1?10?20:30:40:50):60
1?(-1?10?20:30:40):50
-1?(10?20:30):40
10?20:30
20
.........................................
...............................
loops
main purpose of loop is code repetition
void main()
{
int n=100 ;
int i ;
i=1;
while(i<=n) //condition
{
}
}
1
2
3
...
100
...................
syntax
while (condition)
{
statements ;
iteration statement ;
}
.........................................
............................
to find sum and average of numbers
#include<stdio.h>
void main()
{
int n ;
int sum ,avg ;
int i , t;
printf("enter a number : \n);
scanf("%d " ,&n);
sum=0;
i=1;
while( i<=n)
{
printf("enter a value ");
scanf("%d",&t);
sum=sum+t;
i=i+1;
}
avg =sum /n;
printf("average is %d and sum is %d ",
avg ,sum );
}
n=4
sum=0
sum=0+5=5
i=2
sum=5+6 =11
i=3
sum=5+6+7
i=4
sum=5+6+7+8 =26
.........................................
...................................
printing pattern
*
* *
* * *
* * * *
* * * * *
outer loop : i=1 i<=n ;i=i+1
........
void main()
{
int n;
int i ,in;
printf("enter no of stars :");
scanf("%d ",&n);
i=1;
while(i<=n) //n=5
{
printf("\n");
in=1;
while(in<=i)
{
printf("* ");
in=in+1;
}
i=i+1;
1<=1 //true
i=1
in=2
2<=1 //false
i=2 in=2
in=1
1<=2
in=2
2<=2
in=3
3<=2 //false
i=3
*
* *
* * *
* * * *
* * * * *
..........................
*
* *
* * *
* * * *
* * * * * to print no of lines in a
decreasing fashion outer loop
modify inner loop constraints
...............................
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5 outer loop in increasing and
decreasing fashion
....................
hello
hello
hello
hello
hello
i=1;
i=5;
while(i<=5)
while( i>0)
{ {
printf("hello \n);
printf("hello \n" );
i=i-1;
i=i+1;
} }
hello hello
hello hello
hello
hello
hello