Looping
In computer programming, a loop is a sequence of commands or statements that are repeated many times.
Looping statements in C language
- for statement
- while statement
- do…while statement
Let us learn C programming Looping concept by executing following programs.
Sample Program based on Looping concept
#include<stdio.h>
int main()
{
int i;
for(i=1; i<=10; i++)
{
printf("%d ", i);
}
return 0;
}
Output of program
1 2 3 4 5 6 7 8 9 10
C programs based on for loop
1.C program to print 1 to 10 numbers using for loop.
#include<stdio.h>
int main(){
int i;
for(i=1; i<=10; i++)
{
printf("%d ", i);
}
return 0;
}
Output of the program
1 2 3 4 5 6 7 8 9 10
2.C program to print 10 to 1 numbers using for loop.
#include<stdio.h>
int main()
{
int i;
for(i=10; i>0; i--)
{
printf("%d ", i);
}
return 0;
}
Output of the program:
10 9 8 7 6 5 4 3 2 1
int main()
{
int i;
for(i=10; i>0; i--)
{
printf("%d ", i);
}
return 0;
}
Output of the program:
10 9 8 7 6 5 4 3 2 1
3.C program to find the sum of first 100 natural numbers.
#include<stdio.h>
int main()
{
int i, sum, temp;
sum=0;
temp=1;
for( i=0; i<100; i++ )
{
sum = sum + temp;
temp = temp + 1;
}
printf("%d",sum);
return 0;
}
Output of the program:
5050
int main()
{
int i, sum, temp;
sum=0;
temp=1;
for( i=0; i<100; i++ )
{
sum = sum + temp;
temp = temp + 1;
}
printf("%d",sum);
return 0;
}
Output of the program:
5050
4.C program to check whether entered number is odd or even.
#include<stdio.h>
void main()
{
int number;
printf("Enter number:");
scanf("%d",&number);
if(number%2 == 0)
printf("Entered number is even.");
else
printf("Entered number is odd.");
}
Output
Enter number:10
Entered number is even.
void main()
{
int number;
printf("Enter number:");
scanf("%d",&number);
if(number%2 == 0)
printf("Entered number is even.");
else
printf("Entered number is odd.");
}
Output
Enter number:10
Entered number is even.
5.Fibonacci Series in C language.
/* Fibonacci Series in C language.
About Fibonacci series: Each new number in a series is addition of its previous two numbers.
Example of Fibonacci series:0, 1, 1, 2, 3, 5, 8, 13,
*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, n, n1, n2, n3;
printf("Enter your number:");
scanf("%d",&n);
if(n<2)
{
printf("Please enter number > 2. \n");
exit(0);
}
n1=0;
n2=1;
printf("First %d numbers of Fibonacci series:\n",n);
printf("%d, %d, ", n1, n2);
n3 = n1 + n2;
for(i=0; i<n-2; i++)
{
printf("%d, ", n3);
n1=n2;
n2=n3;
n3=n1+n2;
}
return 0;
return 0;
}
Output of the Program:
Enter your number:5
First 5 numbers of Fibonacci series:
0, 1, 1, 2, 3,
6.Swastik Pattern
#include<stdio.h>
int main()
{
int i,j,n;
printf("Enter Swastik Size(n):");
scanf("%d",&n);
printf("* ");
for(i=0; i<n-2; i++)
printf(" ");
for(i=0; i<n; i++)
printf("* ");
printf("\n");
for(j=0; j<n-2; j++)
{
printf("* ");
for(i=0; i<n-2; i++)
printf(" ");
printf("* \n");
}
for(i=0; i<n*2-1; i++)
printf("* ");
printf("\n");
for(j=0; j<n-2; j++)
{
for(i=0; i<=n-2; i++)
printf(" ");
printf("* ");
for(i=0; i<n-2; i++)
printf(" ");
printf("* \n");
}
for(i=0; i<n; i++)
printf("* ");
for(i=0; i<n-2; i++)
printf(" ");
printf("* ");
return 0;
}
}
Output of the Program:
Enter Swastik Size(n):5
* * * * * *
* *
* *
* *
* * * * * * * * *
* *
* *
* *
* * * * * *
7.C Program to print A to Z using for loop.
#include<stdio.h>
Output of the program:
int main()
{
int i;
for(i=65; i<=90; i++)
{
printf("%c ",i);
}
return 0;
}
Output of the program:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
8.C program to print Z to A using for loop
Program Code 1:
#include<stdio.h>
int main()
{
int i;
for(i='Z'; i>='A'; i--)
printf("%c", i);
return 0;
}
Output of the program:
ZYXWVUTSRQPONMLKJIHGFEDCBA
Same program can also be generated using following code.
Program Code 2:
#include<stdio.h>
int main()
{
int i;
//90 is ASCII value of Z
for(i=90; i>=65; i--)
printf("%c", i);
return 0;
}
Output of the program:
ZYXWVUTSRQPONMLKJIHGFEDCBA
9.Sum of series 1 + 1/2 + 1/3 + 1/4 + ....N
C PROGRAM TO PRINT SUM OF SERIES 1 + 1/2 + 1/3 + 1/4 + ....N FOR THE GIVEN VALUE OF N.
For example, if N=5, output will be sum of 1 + 1/2 + 1/3 + 1/4 + 1/5, i.e. 2.28#include<stdio.h>
int main()
{
int i, N;
float sum=0;
printf("Enter N:");
scanf("%d", &N);
if(N<1){
printf("Enter value of N > 0.");
exit(1);
}
for(i=1; i<=N; i++)
{
if(i==1)
{
sum=1;
printf("Series = 1");
}
else
{
sum = sum + (float)1/i;
printf(" + 1/%d", i);
}
}
printf("\nSum of the series = %0.2f", sum);
return 0;
}
Output of program
Enter N:5
Series = 1 + 1/2 + 1/3 + 1/4 + 1/5
Sum of the series = 2.28
No comments:
Post a Comment