Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Assignment#2

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

INTRO TO COMPUTER

PROGRAMMING
COURCE CODE#CSC141

ASSIGNMENT#2
SUBMITTED TO: SIR NASIR MEHDI

SUBMITTED BY: WASIM AKRAM


Roll No# SP19-MCS-016
INTRO TO COMPUTER PROGRAMMING
1. Write a C++ program that the largest factor of a number. For
example user gives a number 30, the largest factor is15.
#include <iostream>

using namespace std;

int main()

int n,i,f;

cout<<"Enter a Number = ";

cin>>n;

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

if(n%i==0)

f=i;

cout<<endl<<"Largest factor"<<" = "<<f<<endl;

return 0;

OUTPUT:
INTRO TO COMPUTER PROGRAMMING

2. Write a C++ program that print empty diamond of stars. User


gives the height.

#include<iostream>

using namespace std;

int main()

cout<<"Enter size of Daimond: ";

int size;

cin>>size;

int z=1;

for ( int i=0; i<=size; i++)

{
INTRO TO COMPUTER PROGRAMMING
for (int j=size; j>i; j--)

cout<<" "; // printing space here

cout<<"*"; // printing asterisk here

if ( i>0)

for ( int k=1; k<=z; k++)

cout<<" ";

z+=2;

cout<<"*";

cout<<endl; // end line similar to \n

z-=4;

for (int i=0; i<=size-1; i++)

for (int j=0; j<=i; j++)

{
INTRO TO COMPUTER PROGRAMMING
cout<<" ";

cout<<"*";

for (int k=1; k<=z; k++)

cout<<" ";

z-=2;

if (i!=size-1)

cout<<"*";

cout<<endl;

system("pause");

return 0;

OUTPUT:
INTRO TO COMPUTER PROGRAMMING

3. Write a C++ program to display a multiplication table. User gives the


size.

1 2 3 4 5 6

------

1 2 3 4 5 6

2 4 6 8 10 12

3 6 9 13 15 18

4 8 12 16 20 24

5 10 15 20 25 30

6 12 18 24 30 36

#include<iostream>

using namespace std;


INTRO TO COMPUTER PROGRAMMING
int main()

int n,i,j;

cout<<"Enter a number = ";

cin>>n;

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

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

cout<<i*j<<" ";

} cout<<endl;

cout<<endl;

return 0;

OUTPUT:
INTRO TO COMPUTER PROGRAMMING

4. Write C++ programs that Display the sum of the digits of a given
number. User gives a number. For example.435 and its sum=12.

#include<iostream>
INTRO TO COMPUTER PROGRAMMING
using namespace std;

int main()
{
int val, num, sum = 0;

cout << "Enter the number : ";


cin >> val;
num = val;
while (num != 0)
{
sum = sum + num % 10;
num = num / 10;
}
cout << "The sum of the digits of " << val << " is " << sum<<endl;

system("pause");
return 0;
}
OUTPUT:
INTRO TO COMPUTER PROGRAMMING

You might also like