Assignement of programming & problem solving u.s ass.(1)

21
Assignment No.1 Submitted To: Adnan Nawaz Submitted By: Syed Muhammad Umair Shah Subject: Programming &Problem Solving Department: Computer Science Semester: 2 nd Roll No. 12

Transcript of Assignement of programming & problem solving u.s ass.(1)

Page 1: Assignement of programming & problem solving u.s ass.(1)

Assignment No.1

Submitted To: Adnan Nawaz

Submitted By: Syed Muhammad Umair Shah

Subject: Programming &Problem Solving

Department: Computer Science

Semester: 2nd 

Roll No. 12

Indus International Institute D.G.Khan

Page 2: Assignement of programming & problem solving u.s ass.(1)

/*Write a program that generates the following output using a single nested loop

$

##

$$$

#### */

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int i,j;

for(i=1;i<=4;i++)

{

for(j=1;j<=i;j++)

if(i%2==0)

cout<<"#";

else

cout<<"$";

cout<<"\n";

}

getch();

}

Page 3: Assignement of programming & problem solving u.s ass.(1)

Out Put

/*Show 1st 8 FIBONACCI Number */

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

long int a=21,b=0,c=1,sum;

while(b<a)

{

cout<<c<<" ";

sum=b+c;

b=c;

c=sum;

}

getch();

}

Page 4: Assignement of programming & problem solving u.s ass.(1)

Out Put

/*Write a program that input two number interchange the value of and then display them without using third variable.*/

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int a,b;

cout<<"Entr 1st num : ";

cin>>a;

cout<<"Entr 2nd num : ";

cin>>b;

cout<<"Value Befor Swapping:"<<endl;

cout<<"1st num="<<a<<endl;

cout<<"2nd num="<<b<<endl;

a=a+b;

b=a-b;

a=a-b;

cout<<"Value After Swapping:"<<endl;

cout<<"1st num="<<a<<endl;

cout<<"2nd num="<<b<<endl;

Page 5: Assignement of programming & problem solving u.s ass.(1)

getch();

}

Out Put

/*Show out

****

***

**

* */

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int i,j;

for(i=1;i<=4;i++)

{

for(j=i;j<=4;j++)

{

cout<<"*";

}

Page 6: Assignement of programming & problem solving u.s ass.(1)

cout<<"\n";

}

getch();

}

Out Put

/*Triangle */

#include<iostream.h>

#include<conio.h>

void main() //if(i==1&&i==4&&j==1&&j==x)

{

clrscr();

int i,j,sp,x=1;

for(i=1;i<=4;i++,x=x+2)

{

for(sp=1;sp<=4-i;sp++) cout<<" ";

for(j=1;j<=x;j++)

{

if(i>1&&i<4&&j>1&&j<x)

cout<<" ";

else

cout<<"*";

Page 7: Assignement of programming & problem solving u.s ass.(1)

}

cout<<"\n";

}

getch();

}

Out Put

/*Take input of Three n.o if the are not Equal,then find the Smallest n.o*/

#include<iostream.h>

#include<conio.h>

void main(){

clrscr();

int a,b,c;

cout<<"Entr 1st n.o = ";

cin>>a;

cout<<"Entr 2nd n.o = ";

cin>>b;

cout<<"Entr 3rd n.o = ";

cin>>c;

if((a<b)&&(a<c))

cout<<"Smallest n.o is = "<<a;

Page 8: Assignement of programming & problem solving u.s ass.(1)

else if((c<a) && (c<b)){

cout<<"Smallest n.o is = "<<c;}

else if((b<a) && (b<c)){

cout<<"Smallest n.o is = "<<b;}

else{

cout<<"All n.o are Equal";}

getch();

}

Out Put

/*Take input of two n.o if 2nd n.o is greater than 1st,display the division

as follow i.e. 1st=16 2nd=5 Ans=5*3+1=16*/

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int n1,n2;

cout<<"Entr 1st no. = ";

cin>>n1;

cout<<"Entr 2nd no. = " ;

cin>>n2;

if(n2>n1)

Page 9: Assignement of programming & problem solving u.s ass.(1)

{

cout<<"invalide input";

}

else

cout<<n2<<" * "<<n1/n2<<" + "<<n1%n2<<" = "<<n1<<"\n";

getch();

}

Out Put

/*Show out

*****

*****

***** */

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int i,j;

for(i=1;i<=3;i++)

{

Page 10: Assignement of programming & problem solving u.s ass.(1)

for(j=1;j<=5;j++)

{

cout<<"*";

}

cout<<"\n";

}

getch();

}

Out Put

/* Show out

*

**

***

**** */

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

Page 11: Assignement of programming & problem solving u.s ass.(1)

int i,j,sp;

for(i=1;i<=4;i++){

for(sp=1;sp<=4-i;sp++)

{

cout<<" ";

}

for(j=1;j<=i;j++)

{

cout<<"*";}

cout<<"\n";

}

getch();

}

Out Put

/*Take input a n.o and find if it is divisible by 10 or not*/

#include<iostream.h>

#include<conio.h>

void main()

{

Page 12: Assignement of programming & problem solving u.s ass.(1)

clrscr();

int a;

cout<<"Entr any number = ";

cin>>a;

if(a%10==0)

{

cout<<"it is Divisible by 10";

}

else

{

cout<<"it is not Divisible by 10";

}

getch();

}

Out Put

/*Inverted Triangle */

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

Page 13: Assignement of programming & problem solving u.s ass.(1)

int w=6;

for(int g=0;g<9;g++)

{

cout<<"*";

}

cout<<"\n";

for(int a=1;a<=3;a++)

{

for(int b=0;b<a;b++)

{

cout<<" ";

}

cout<<"*";

for(int c=1;c<w;c++)

{

cout<<" ";

}

cout<<"*"<<"\n";

w=w-2;

}

for(int e=1;e<=1;e++)

{

for(int f=4;f>=e;f--)

{

Page 14: Assignement of programming & problem solving u.s ass.(1)

cout<<" ";

}

cout<<"*";

}

getch();

}

Out Put

/* d=++a + a-- - b++ + --c = 1*/

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int a=2,b=4,c=0,d;

d=++a + a-- - b++ + --c;

cout<<d;

getch();

}

Page 15: Assignement of programming & problem solving u.s ass.(1)

Out Put

/*SUM OF 1ST 6TH PRIME NUMBER*/

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int i,j,sum=0,flag=1,x;

//outer Loop

for(i=1,x=0;x<=5;i++)

{

//inner Loop

for(j=2;j<=i/2;j++)

{

if(i%j==0)

{

flag=0;

break;

}

}

if(flag)

Page 16: Assignement of programming & problem solving u.s ass.(1)

{

x++;

sum+=i;

cout<<i<<" + ";

}

else

{

flag=1;

}

}

cout<<"\b\b = "<<sum;

getch();

}

Out Put

/*entr 10 student age and show average age*/

#include <iostream.h>

#include<conio.h>

void main()

{

clrscr();

float age1,age2,age3,age4,age5,age6,age7,age8,age9,age10;

float TotalAge;

Page 17: Assignement of programming & problem solving u.s ass.(1)

float AverageAge;

cout <<" Please Enter The Age Of Student 1:" ;

cin >> age1;

cout <<" Please Enter The Age Of Student 2:" ;

cin >> age2;

cout <<" Please Enter The Age Of Student 3:" ;

cin >> age3;

cout <<" Please Enter The Age Of Student 4:" ;

cin >> age4;

cout <<" Please Enter The Age Of Student 5:" ;

cin >> age5;

cout <<" Please Enter The Age Of Student 6:" ;

cin >> age6;

cout <<" Please Enter The Age Of Student 7:" ;

cin >> age7;

cout <<" Please Enter The Age Of Student 8:" ;

cin >> age8;

cout <<" Please Enter The Age Of Student 9:" ;

cin >> age9;

cout <<" Please Enter The Age Of Student 10:" ;

cin >> age10;

TotalAge=age1+age2+age3+age4+age5+age6+age7+age8+age9+age10;

AverageAge=TotalAge/10 ;

cout << " Average Age Of Class Is : " << AverageAge ;

Page 18: Assignement of programming & problem solving u.s ass.(1)

getch();

}

Out Put

/*Enter The Amount Of The Bill And Discount At The Rate Of 10% */

#include <iostream.h>

#include<conio.h>

void main ()

{

clrscr();

double amount,discount,netpayable;

cout << " Please Enter The Amount Of The Bill ";

cin >> amount;

if(amount>5000)

{

discount = amount*(15.0/100);

netpayable = amount-discount;

cout <<" The Discount At The Rate Of 15 % is Rupees " << discount << endl;

Page 19: Assignement of programming & problem solving u.s ass.(1)

cout << " The Payable Amount is Rupees " << netpayable;

}

else

{

discount = amount*(10.0/100);

netpayable = amount-discount;

cout <<" The Discount At The Rate Of 10 % is Rupees " << discount << endl;

cout << " The Payable Amount is Rupees " << netpayable;

}

getch();

}

Out Put