2/19/2016

C programming using for loop

 Contents:-
for loop
even number
square of a no.
piramid structure
fibonacci series
Floyd's triangle
harmonic series
and more programmings

example: -for(i=0;i<=4;i++)
This means the process which is inside the loop is done 5 times and first time i value is 0,second time i value is 1,third time i value is 2,fourth time i value is 3,fifth time i value is 4.
If we write
 for(i=4;i<=6;i++)
then
   1st time: i=4
    2nd time: i=5
    3rd time: i=6


Now understand the following important programming

#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=0;i<=4;i++)
{
printf("%d",i);
}
getch();

}


Output:
01234
------------------------------

#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=4;i<=8;i++)
{
printf("%d\n",i);
}
getch();
}

Output:
4
5
6
7
8

------------------------------



#include<conio.h>
#include<stdio.h>
int main()
{
int i;
for(i=0;i<=4;i++)
{
printf("Test\n");
}
getch();
}

Output:
Test
Test
Test
Test
Test

--------------------------

A program to print the even numbers
between 0 to 20.

#include<stdio.h>
#include<conio.h>
int main()
{
int i;
int a=0;
for(i=0;i<=8;i++)
{
a=a+2;
printf("%d\n",a);
}
getch();
}

Output:
2
4
6
8
10
12
14
16
18


Note:-  
a=0
 i=0,a=0
a=a+2
new value of a=2
i=1,a=2
a=a+2
new value of a=4
similarly..…
-----------------------

#include<stdio.h>
#include<conio.h>
int main()
{
int i;
int a=1;
for(i=0;i<=3;i++)
{
a=a+i;
printf("%d\n",a);
}
getch();
}

Output:
1
2
4
7


Note:
a=1
i=0,a=1
a=a+i
new value of a=1
i=1,a=1
a=a+i
new value of a=2
i=2,a=2
a=a+i
new value of a=4
a=4,i=3
a=a+i
new value of a=7

-------------------------

#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=0;i<=3;i++)
{
printf("*");
}
getch();
}

Output:
****

----------------------
A program to display the table of square of the number 
0 to 10

#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=0;i<=10;i++)
{
printf("%d\n",i*i);
}
getch();
}

Output:
0
1
2
9
16
25
36
49
64
81
100

-----------------------
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=0;i<=3;i++)
{
for(j=1;j<=2;j++)
{
printf("%d\n",j);
}
}
getch();
}

Output:
1
2
1
2
1
2
1
2


Note:-
when value of i=0
then j value goes 1 to 2
when the value of i=1
then j value goes 1 to 2
similarly……

----------------------------
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=0;i<=3;i++)
{
for(j=0;j<=2;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}

Output:
12
12
12
12

-----------------
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=0;i<=3;i++)
{
for(j=1;j<=2;j++)
{
printf("%d\n",i);
}
}
getch();
}


Output:
0
0
1
1
2
2
3
3


-----------------------
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=0;i<=3;i++)
{
for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}

Output:
*
**
***
****

Note:-
for the value i=0
j=0;
i=1
then j=0,1;
i=2
then j=0,1,2


---------------------------------
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i+3;j++)
{
if(j<=4-i)
{
printf(" ");
}
else
{
printf("*");
}
}
printf("\n");
}
getch();
}


Output:
   *
  ***
 *****
*******


-----------------------------
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=7;i++)
{
if(i==1  ||  i==4  ||  i==7)
{
printf("*******\n");
}
if(i==2  ||  i==3)
{
printf("*\n");
}
if(i==5  ||  i==6)
{
printf("      *\n");
}
}
getch();
}


Output:
*******
*
*
*******
           *
           *
*******

-------------------------------
#include<stdio.h>
#include<conio.h>
int main()
{
int i.j;
for(i=1;i<=6;i++)
{
if(i==4)
{
printf("  *******");
}
else
{
for(j=1;j<=i+5;j++)
{
if(j==7-i  ||  i==i+5)
{
printf("*");
}
else
{
printf(" ");
}
}
}
printf("\n");
}
getch();
}


Output:
      *
     * *
   *      *
  *******
 *            *
*               *


-------------------------


#include<stdio.h>
#include<conio.h>
int main()
{
int main()
{
int i=2;
for(;i<=20;i++)
{
if(i%2==0)
{
printf("%d\n",i);
}
}
getch();
}

Output:
2
4
6
8
10
12
14
16
18
20

……………………………………………………

#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=2;;i++)
{
if(i%2==0)
{
printf("%d\n",i);
}
if(i==20)
{
getch();
}
}
getch();
}

Output:
2
4
6
8
10
12
14
16
18
20

……………………………………………………

#include<stdio.h>
#include<conio.h>
int main()
{
int i=2;
for(;;i++)
{
if(i%2==0)
{
printf("%d\n",i);
}
if(i==20)
{
getch();
}
}
getch();
}

Output:
2
4
6
8
10
12
14
16
18
20



--------------------------------
A programming in C to find the Fibonacci series.

#include<stdio.h>
#include<conio.h>
int main()
{
int n,f0=0,f1=1,f2=1;
printf("How many numbers are to be displayed\n");
scanf("%d",&n);
printf("Fibonacci series are:\n0   1   1");
for(int i=1;i<=(n-3);i++)
{
f0=f1;
f1=f2;
f2=f0+f1;
printf("   %d",f2);
}
getch();
}


Output:
How many numbers are to be displayed
9
0   1   1   2   3   5   8   13   21


……………………………………………
……………………………………………
A program in C for print sum of series
1 + 1/2 + 1/3 + 1/4 + ... + 1/N. 


#include<stdio.h>
#include<conio.h>
int main()
{

    float sum,i,n;
clrscr();

printf("\n Please give the value of N:  ");
scanf("%f",&n);

for(i=1;i<=n;i++)
{
sum = sum + (1/i);
if(i==1)
printf("\n 1 +");
        else if(i==n)
     printf(" (1/%.0f) ",i);
else
printf(" (1/%.0f) + ",i);
}

printf("\n\n THE SUM OF THIS SERIES IS %.6f",sum);
getch();
}



Output:

Please give the value of N:5
1 + (1/2) + (1/3) + (1/4) + (1/5)

THE SUM OF THE SERIES IS 2.283334


……………………………………………
……………………………………………

Write a program for Floyd's triangle in which the number of rows is entered by the user.
EXAMPLE:-
 First four rows of Floyd's triangle are as follows :-
1
2 3
4 5 6
7 8 9 10



#include <stdio.h>

int main()
{
int n, i,  c, a = 1;

printf("Enter the number of rows of Floyd's triangle to print\n");
scanf("%d", &n);
printf("Floyd's triangle\n");
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
printf("%d  ",a);
a++;
}
printf("\n");
}

return 0;
}


Output:
Enter the number of rows of Floyd's triangle to print
5
Floyd's triangle
1
2  3
4  5  6
7  8  9  10
11  12  13  14  15



CLICK FOR SEARCH TOPIC
                                                 

Please just write comment in comment box.You can also send any programming question.




No comments:

Post a Comment