You are on page 1of 22

Assignment 1

SIMPLE PROGRAMS
1. Write a C++ program to find all Pythagoras
triplets(a,b,c) where each a,b,c is in between 1 & 100
with c 2=a2+b 2

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr(); //Clear Screen Function
cout<<"\n\nProgram for finding Pythogoras triplets (a,b,c) \n";
for(int a=1;a<=100;a++)
for(int b=1;b<=100;b++)
for(int c=1;c<=100;c++)
if((c*c)==(a*a + b*b))
cout<<"("<<a<<","<<b<<","<<c<<")\t";
getch(); //Getch Function
}
OUTPUT:

Program for finding Pythogoras triplets (a,b,c)


(3,4,5) (4,3,5) (5,12,13) (6,8,10) (7,24,25) (8,6,10) (8,15,17)
(9,12,15) (9,40,41) (10,24,26) (11,60,61) (12,5,13) (12,9,15) (12,16,20)
(12,35,37) (13,84,85) (14,48,50) (15,8,17) (15,20,25) (15,36,39) (16,12,20) (16,30,34)
(16,63,65) (18,24,30) (18,80,82) (20,15,25) (20,21,29) (20,48,52) (21,20,29) (21,28,35)
(21,72,75) (24,7,25) (24,10,26) (24,18,30) (24,32,40) (24,45,51) (24,70,74) (25,60,65)
(27,36,45) (28,21,35) (28,45,53) (28,96,100) (30,16,34) (30,40,50) (30,72,78)
(32,24,40) (32,60,68) (33,44,55) (33,56,65) (35,12,37) (35,84,91) (36,15,39) (36,27,45)
(36,48,60) (36,77,85) (39,52,65) (39,80,89) (40,9,41) (40,30,50) (40,42,58) (40,75,85)
(42,40,58) (42,56,70) (44,33,55) (45,24,51) (45,28,53) (45,60,75) (48,14,50) (48,20,52)
(48,36,60) (48,55,73) (48,64,80) (51,68,85) (52,39,65) (54,72,90) (55,48,73) (56,33,65)
(56,42,70) (57,76,95) (60,11,61) (60,25,65) (60,32,68) (60,45,75) (60,63,87) (60,80,100)
(63,16,65) (63,60,87) (64,48,80) (65,72,97) (68,51,85) (70,24,74) (72,21,75)
(72,30,78) (72,54,90) (72,65,97) (75,40,85) (76,57,95) (77,36,85) (80,18,82) (80,39,89)
(80,60,100) (84,13,85) (84,35,91) (96,28,100)
 
 
2. Write a C++ program to print first 20 terms of
Fibonacci sequence numbers 1,1,2,3,5,...

#include<iostream.h>
#include<conio.h>
void main()
{

int f1=1,f2=1,f; //variable declaration


clrscr(); //clear screen function

cout<<"First 20 terms of the fibonacci series :=> \n";


Que. 2 (continued)

for(int i=1;i<=20;i++) //for loop


{
cout<<f1<<"\t";
f=f1+f2; //actual fibonacci series
f1=f2;
f2=f;
}
getch(); // getch function
}
Solution:
First 20 terms of the Fibonacci series :=>
1 1 2 3 5 8 13 21 34 55 89 144 233
377 610 987 1597 2584 4181 6765
 
3. Write a C++ program to check whether the positive
integer n is prime or not.

#include<iostream.h>
#include<conio.h>
void main()
{
int flag=0,n; //variable declaration
clrscr(); // clear screen function
cout<<"Enter a No. ";
cin>>n;
for(int i=2;i<n;i++)
if(n%i==0) //prime logic
{
flag =1;
break;
}
Que 3 (continued)

if(flag)
cout<<"\n"<<n<<" is not a Prime number !";
else
cout<<"\n"<<n<<" is a Prime number !";
getch();
}

OUTPUT:
Enter a No. 30
30 is not a Prime number !
4. Write a C++ program to generate following output :

11
11 13
11 13 15
11 13 15 17
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
int n,k;
clrscr(); //Clear screen function
cout<<"Enter No. of levels ";
cin>>n;
Que. 4 (continued)

for(int i=0;i<n;i++) //for loop


{
k=11;
for(int j=0;j<=i;j++) //nested for
{
if(k%2!=0)
cout<<setw(4)<<k;
k=k+2;
}
cout<<endl; //New line operator
}
getch();
}
OUTPUT:

Enter No. of levels 6


11
11 13
11 13 15
11 13 15 17
11 13 15 17 19
11 13 15 17 19 21
 
 
5. Write a C++ program to find the roots of quadratic
equation ax 2 +bx+c = 0.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
double a,b,c,root;
clrscr();
cout<<"\n to find roots of Quadratic Equation ax2 +bx+c = 0.\n";
cout<<"\n Enter value of a :";
cin>>a;
cout<<"\n Enter value of b :";
cin>>b;
cout<<"\n Enter value of c :";
cin>>c;
Que. 5 (continued)

//Calculation of the quadratic equation


root = b*b - 4*a*c;
if(root < 0)
cout<<"\nx1 = "<< (-b/2*a) << " +i " <<sqrt(abs(root))/2*a;
cout<<"\tx2= "<< (-b/2*a) << " -i " << sqrt(abs(root))/2*a;
else
cout<<"\nx1 = "<< ( b/2*a) + sqrt(abs(root))/2*a;
cout<<"\tx2 = "<< (-b/2*a) - sqrt(abs(root))/2*a;
getch();
}
OUTPUT:

To find roots of Quadratic Equation ax2 +bx+c = 0.


Enter value of a :2
Enter value of b :4
Enter value of c :2
x1 = 4 x2 = -4
6. Write a C++ program to read the values of a,b,c and
display the value of x , where x = a/(b-c).

#include<iostream.h>
#include<conio.h>
 
void main()
{
int a,b,c,x;
clrscr();
cout<<"\n Enter value of a :=> ";
cin>>a;
cout<<"\n Enter value of b :=> ";
cin>>b;
cout<<"\n Enter value of c :=> ";
cin>>c;
Que. 6 (continued)

x = a/(b -c);
cout<<"\n Value of Expression : x = a/(b-c) is "<< x;
getch();
}

OUTPUT:
Enter value of a :=> 10
 
Enter value of b :=> 5
 
Enter value of c :=> 3
 
Value of Expression : x = a/(b-c) is 5
 
7. Write a C++ program to find product of digits of an
integer number n. 

#include<iostream.h>
#include<conio.h>
 
void main()
{
unsigned long n,product=1;
clrscr();
cout<<"\nEnter a No. ";
cin>>n;
Que. 7 (continued)

while(n>10) //while loop


{
product*=(n%10);
n=n/10;
}
product*=n;
cout<<"\n\nProduct of digits of n is :=>"<<product;
getch();//getch function
}
 
 
OUTPUT:

Enter a No. 123456789


 
Product of digits of n is :=>362880
 
 
 
8. Write a C++ program to find minimum of 10
numbers.

#include<iostream.h>
#include<conio.h>
 
void main()
{
int i,min,a[10];
 
clrscr();
cout<<"\n Enter ten numbers :";
for(i=0;i<10;i++)//for loop
{
cout<<"\nEnter A["<<i+1<<"] :=>";
cin>>a[i];
}
Que. 8 (continued)

min = a[0];//initialization of min variable


for(i=0;i<10;i++)//for loop
if(min>a[i])//if loop

min = a[i];
cout<<"The minimum number entered by you is :=>"<<min;
getch(); //getch function
}
OUTPUT:

Enter ten numbers :


Enter A[1] :=>1
Enter A[2] :=>0
Enter A[3] :=>-5
Enter A[4] :=>78
Enter A[5] :=>94
Enter A[6] :=>-23
Enter A[7] :=>75
Enter A[8] :=>12
Enter A[9] :=>34
Enter A[10] :=>8
The minimum number entered by you is :=> -23

You might also like