For Loop in C-programming

 For Loop 

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Syntax-
for(variable int; condition ;variable update)
{
    statement;
}

program1-write a program to print "hello Word" five time 
code-
#include<stdio.h>
void main()
{
int i;
for(i=0;i<=5;i++)
{
printf("hello\n");
}
}
output-
hello
hello
hello
hello
hello
hello

--------------------------------
program2-Write a program to print 0to 10 number using while loop
code-
#include<stdio.h>
void main()
{
int i;
for(i=0;i<=10;i++)
{
printf("%d\n",i);
}
}
output-
0
1
2
3
4
5
6
7
8
9
10
--------------------------------
Program3-write a program to find sum of series 1!+1/2!+1/3!+1/4!+1/5!..........
code-
#include<stdio.h>
void main()
{ float fact=1,sum=0,i,n;
printf("Enter the value of n");
scanf("%f",&n);
for(i= 1;i<=n;i++)
{
fact=fact*i;
sum =sum+1/fact;
}
printf("Result is %f",sum);
}
Output-
Enter the value of n7
Result is 1.718254
--------------------------------
program4-Write a program to print the fibonacci series :0 1 1 2 3 5 8 13............n usig for loop.
code-
#include<stdio.h>
int main()
{
int t1=0,t2=1,t3,i,n;
printf("Enter no of terms ") ;
scanf("%d",&n);
printf("\nFiboacci series:\n %d \n %d",t1,t2);
for(i=1;i<=n;i++)
{
t3=t2+t1;
printf("\n %d",t3);
t1=t2;
t2=t3;
}
}
Output-
Enter no of terms 5

Fiboacci series:
 0
 1
 1
 2
 3
 5
 8
-------------------------------------------------------
Program5-Write a program to find out factorial using for loop.
code-
#include<stdio.h>
 int main ()
{
int  number ,fact=1 ;
printf("Enter the number ");
scanf("%d",&number);
for(int i=1;i<=number;i++)
{
fact=fact*i;
}
printf("The factorial of the given number is %d",fact);
}    
output-
Enter the number 6
The factorial of the given number is 720
----------------------------------------------------------------------------------------------------------------


Comments

Popular posts from this blog

CYBER SECURITY & CRACKING PASSWORDS

"The Dark Side of Social Media: How It Fuels Digital Addiction and Impacts Mental Health"

Online Platforms to Learn Coding