Assignment 2

You might also like

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

NUST COLLEGE OF ELECTRICAL & MECHANICAL

ENGINEERING

assiGNMENT 2

NaMEs:

 asC sHabaHaT HasNaiN


 NC abDUl rEHMaN
 NC TalHa JaHaNGir
 GC zEEsHaN ali
 syN(C)

Task 1:

A positive integer n is said to be prime (or, "a prime") if and only if n is


greater than 1 and is divisible only by 1 and n. For example, the integers 17
and 29 are prime, but 1 and 38 are not prime. Write a program that takes a
number from user and detect
detec if it is prime number or not.
CODE:

#include<iostream>
usingnamespacestd;
int main()
{
int n, i;
bool prime = true;
cout<<"Enter
"Enter a positive integer: ";
"
cin>> n;
if(n==1||n==0)
{prime=false;}
for(i = 2; i <= n /2 ; ++i)
{
if(n % i == 0)
{
prime = false;
break;
}
}
if (prime)
cout<<"This is a prime number"<<endl;
else
cout<<"This is not a prime number"<<endl;

system("pause");
return 0;
}
OUTPUT:

Task 2:
Write program that takes two positive integer arguments and calculates the greatest common divisor of
those two integers. If number entered by user is not positive then the program should retake the value
from user. For example Greatest common divisor of 50 and 60 is 10. Greatest common factor is 256 and
625 is 1.
CODE:

#include<iostream>
usingnamespacestd;
int main()
{int a, b;

do
{
cout<<"Enter two numbers: ";
cin>> a >> b;

} while(a==0||b==0);

while(a!=b)
{ if(a>b)
a -= b;
else
b -= a;

}
cout<<"GCD = "<< a<<endl;

system("pause");
return 0;
}
OUTPUT:

OUTPUT:

Task 3:
Write a program in C++ to create and display unique three-digit number using 1, 2, 3, 4. Also count how
many three-digit numbers are there. Sample Output: The three-digit numbers are: 123 124 132 134 142
143 213 214 231 234 241 243 312 314 321 324 341 342 412 413 421 423 431 432 Total number of the
three-digit-number is: 24CODE:

CODE:
#include<iostream>
usingnamespacestd;
int main()
{
inta,b,c,sum=0;

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

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

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

{if(a!=b&&b!=c&&c!=a)

{cout<<a<<b<<c<<endl;
sum++;}}}}

cout<<"sum is equal to:"<<sum<<endl;

system("pause");}

OUTPUT:

Task 4:
Write a C++ program to print the given number pattern using for loop. How to print the given number
pattern of m rows and n columns using for loop .
Example
Input Input rows: 5 Input columns: 5
CODE:
#include<iostream>
usingnamespacestd;
int main()
{int rows, cols, i, j;
cout<<"Enter number of rows:";
cin>>rows;;
cout<<"Enter number of columns: ";
cin>>cols;

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


{
for(j=i; j<=cols; j++)
{cout<<""<<j;}

for(j=i-1; j>=1; j--)


{cout<<""<<j;}

cout<<"\n";
}
system("pause");
return 0;
}
OUTPUT:

Task 5:
Write a C++ program to print pascal triangle up to n rows using loop
CODE:

#include<iostream>
Using namespace std;
int main()
{
int n, line;

cout<<"Please provide the number of rows of the triangle: ";


cin>> n;

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


{
// used to represent C(line, i)
int C = 1;

for (int space = 1; space < (n - line + 1); space++)


{
cout<<" ";
}

for (int i = 1; i <= line; i++)


{
// The first value in a line is always 1
cout<< C <<" ";
C = C * (line - i) / i;
}
cout<<"\n";
}

// Pause console
system("pause");

return 0;
}
OUTPUT:
Task 6:
Write a C++ program to print all Armstrong numbers between 1 to n. An Armstrong number is a n-digit
number that is equal to the sum of nth power of its digits. For example, 6 = 61 = 6 371 = 33 + 73 + 13 = 371

CODE:

#include<iostream>
usingnamespacestd;
int main(){
int sum, num;
cout<<"Armstrong numbers between 1 and 1000: "<<endl;
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
for(int k = 0; k < 10; k++) {
num = i * 100 + j * 10 + k;
sum = (i*i*i) + (j*j*j) + (k*k*k);
if(num == sum)
cout<<num<<" "<<endl;
}
}
}
system("PAUSE");
return 0;
} OUTPUT:
Task 7:
Write a C program to count frequency of digits in a given number.

CODE:

#include <iostream>
using namespace std;
int main()
{
int num,num1=0,num2=0,num3=0,num4=0,num5=0,num6=0,num7=0,num8=0,num9=0,num0=0;
int a,b=0;

cout<<"\n";
cin>>num;
a=num;
while(a>0)
{
b=a%10;
{if(b==0)
num0++;
if(b==1)
num1++;
if(b==2)
num2++;
if(b==3)
num3++;
if(b==4)
num4++;
if(b==5)
num5++;
if(b==6)
num6++;
if(b==7)
num7++;
if(b==8)
num8++;
if(b==9)
num9++;}

a=a/10;

cout<<"Frequency of 0="<<num0<<endl;
cout<<"Frequency of 1="<<num1<<endl;
cout<<"Frequency of 2="<<num2<<endl;
cout<<"Frequency of 3="<<num3<<endl;
cout<<"Frequency of 4="<<num4<<endl;
cout<<"Frequency of 5="<<num5<<endl;
cout<<"Frequency of 6="<<num6<<endl;
cout<<"Frequency of 7="<<num7<<endl;
cout<<"Frequency of 8="<<num8<<endl;
cout<<"Frequency of 9="<<num9<<endl;

system("pause");
return 0;

}
OUTPUT:
END

You might also like