Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

/*write a program to generate even number, odd number or multiplicative

table of a number taken from user.*/

#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;
}

HARSHIT GUPTA (XI A7)


}
break;
case 3:
{
int n=0, i=1, x=0;
cout<<"enter the number"<<endl;
cin>>n;
for(; i<=10 ; i+=1)
{
x=n*i;
cout<<n<<"*"<<i<<"="<<x<<endl;
}
}
break;
default:
cout<<"error choice\n";

}
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');
}

HARSHIT GUPTA (XI A7)


/*write a program to find the factors of a number and display whether it is prime
or not.*/
#include<iostream.h>
#include<conio.h>

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();

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');
}

HARSHIT GUPTA (XI A7)


//Write a program to generate all the prime numbers upto a given range

#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();
}

HARSHIT GUPTA (XI A7)


//write a program to print the series -2,3,-4.....till n terms.

#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();
}

HARSHIT GUPTA (XI A7)


/*Write a program to print and find the sum of the series whose nth
term is given by (1/n).*/

#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();
}

HARSHIT GUPTA (XI A7)


/*write a program to generate fibonacci series.*/

#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();
}

HARSHIT GUPTA (XI A7)


/* Write a program to find the factorial of a given number.*/

#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();
}

HARSHIT GUPTA (XI A7)


ASSIGNMENT 3

SIMPLE LOOPING PROGRAMS

1. Write a program to generate even number, odd number or


multiplicative table of a number taken from user.
2. Write a program to find the factors of a number and
display whether it is prime or not.
3. Write a program to generate all the prime numbers upto a
given range

SERIES PROGRAMS

1. Write a program to print the series -2,3,-4.....till n


terms.
2. Write a program to print and find the sum of the series
whose nth term is given by (1/n).
3. Write a program to generate fibonacci series.
4. Write a program to find the factorial of a given number.

HARSHIT GUPTA (XI A7)

You might also like