for loop examples
//sum of first 10 natural numbers
//use header files syntax according to the compiler
#include<conio.h>
#include<iostream.h>
void main()
{
int sum=0;
for (int i=1;i<=10;i++)
{
sum=sum+i;
}
cout<<sum;
getch();
}
#include<iostream.h>
void main()
{
int sum=0;
for (int i=1;i<=10;i++)
{
sum=sum+i;
}
cout<<sum;
getch();
}
// product of first 10 natural numbers
//use header files syntax according to the compiler
#include<conio.h>#include<iostream.h>
void main()
{
int product=1;
for (int i=1;i<=10;i++)
{
product=product*i;
}
cout<<product;
getch();
}
// factorial of a number
//use header files syntax according to the compiler
#include<conio.h>#include<iostream.h>
void main()
{
unsigned int fact=1;
int num;
int num;
cout<<"enter a number ";
cin>>num;
for (int i=1;i<=num;i++)
{
fact=fact*i;
}
cout<<fact;
getch();
}
for (int i=1;i<=num;i++)
{
fact=fact*i;
}
cout<<fact;
getch();
}
//use header files syntax according to the compiler
#include<conio.h>
#include<iostream.h>
void main()
{
#include<iostream.h>
void main()
{
int a,b,c;
a=0,b=1;
cout<<a<<" "<<b<<" ";
for (int i=1;i<8;i++)
{
c=a+b;
cout<<c<<" ";
a=b;
b=c;
}
a=0,b=1;
cout<<a<<" "<<b<<" ";
for (int i=1;i<8;i++)
{
c=a+b;
cout<<c<<" ";
a=b;
b=c;
}
getch()
}