Professional Documents
Culture Documents
Loop Programs
Loop Programs
#include<iostream.h>
#include<conio.h>
void main()
{
char ch;
do{
clrscr();
ch=' ';
int choice=0;
cout<<"what do you want to do \? \n";
cout<<"\t \t \t 1. generate even numbers \n";
cout<<"\t \t \t 2. generate odd numbers \n";
cout<<"\t \t \t 3. generate the multiplicative table of number \n";
cout<<"Enter the choice"<<endl;
cin>>choice;
switch(choice)
{
case 1:
{
int n=0, i=0, x=0;
cout<<"enter the number of terms"<<endl;
cin>>n;
for(; i<=n ; i+=1)
{
x=i*2;
cout<<x<<endl;
}
}
break;
case 2:
{
int n=0, i=0, x=0;
cout<<"enter the number of terms"<<endl;
cin>>n;
for(; i<=n ; i+=1)
{
x=i*2;
cout<<x<<endl;
}
}
getch();
cout<<"do you want to do some other thing \?";
cout<<"\n (enter y for yes and n for no)"<<endl;
cin>>ch;
}while(ch=='y');
}
void main()
{
char ch;
do{
clrscr();
ch=' ';
int n=0, flag=0;
cout<<"enter the number \n";
cin>>n;
cout<<"the factors are \n"<<"1 \n";
for(int i=2; i<n; i+=1 )
{
if(n%i==0)
{
flag=1;
cout<<i<<"\n";
}
}
cout<<n<<"\n";
if(flag==1)
cout<<"the number is not prime \n";
else
cout<<"the number is prime \n";
getch();
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n=0, flag;
cout<<"enter the range \n";
cin>>n;
for(int i=2; i<=n; i+=1)
{
flag=0;
for(int y=2; y<i; y+=1)
{
if(i%y==0)
flag=1;
}
if(flag==0)
cout<<i<<"\n";
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n=0;
cout<<"enter the number of series \n";
cin>>n;
for(int i=1; i<=n; i+=1)
{
if(i%2==0)
cout<<-i<<" ";
else
cout<<i<<" ";
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n=0;
float sum=0;
cout<<"enter the number of terms \n";
cin>>n;
cout<<"the series is \n";
for(float i=1; i<=n; i+=1)
{
float x=0;
x= 1 / i;
sum = sum + x;
cout<<x<<" ";
}
cout<<"\nthe sum is "<<sum;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n=0, x=0, y=1, z=0, i=1;
cout<<"enter the number of terms \n";
cin>>n;
cout<<z<<" "<<y<<" ";
for(;i<=n-2;i+=1)
{x= y + z;
z=y;
y=x;
cout<<x<<" ";
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n=0;
double factorial=1;
cout<<"enter the number \n";
cin>>n;
for(int i=1; i<=n ; i+=1)
factorial = factorial * i;
cout<<"the factorial of "<<n<<" is "<<factorial;
getch();
}
SERIES PROGRAMS