0% found this document useful (0 votes)
11 views11 pages

Lab 5

The document contains solutions to algorithmic problems in C++ including finding the greatest common divisor, least common multiple, reversing digits of a number, checking if a number is perfect, counting set bits, and converting between binary and decimal numbers.

Uploaded by

shazil.xia094
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

Lab 5

The document contains solutions to algorithmic problems in C++ including finding the greatest common divisor, least common multiple, reversing digits of a number, checking if a number is perfect, counting set bits, and converting between binary and decimal numbers.

Uploaded by

shazil.xia094
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Question no 1:

The greatest common divisor of two positive integers, n and m, is the largest number, d, which divides
evenly into both n and m. There are several algorithms that can be used to solve this problem, including:
Initialize d to the smaller of m and n. Keep decreasing d, until you find a number that divides both of the
numbers.

#include <iostream>

using namespace std;

int main() {

int n, m, d, c, i;

cout<<"Enter first number"<<endl;

cin>>n;

cout<<"Enter second number"<<endl;

cin>>m;

if(n>=m){

c=n;

else{

c=m;

for(int i=1; i<=c; i++){

if(n%i==0 && m%i==0){

d=i;

cout<<"The greatest common divisor of "<<n<<" and "<<m<<" is "<<d;

return 0;

}
Question no 4:
Find LCM by using algorithm: to get the least common multiple of two numbers is to multiply them and
divide the result by their greatest common divisor.

Least Common Factor

#include <iostream>

using namespace std;

int main() {

int n, m, d, c, lcm;

cout<<"Enter first number"<<endl;

cin>>n;

cout<<"Enter second number"<<endl;

cin>>m;

if(n>m){

c=n;

else{

c=m;

for(int i=1; i<=c; i++){

if(n%i==0 && m%i==0){

d=i;

lcm=(n*m)/d;

cout<<"The least common factor of "<<n<<" and "<<m<<" is "<<lcm;


return 0;

Queston no 6:
Given a number 'n', repeatedly apply the following: If 'n' is even, divide it by 2. If 'n' is odd, multiply it by
3 and add 1. The conjecture states that no matter which positive integer you start with, you will always
eventually reach 1. Print the series: e.g., 6 = 6 , 3, 10, 5, 16, 8, 4,2,1

#include <iostream>

using namespace std;

int main() {

int n;

cout<<"Enter your number"<<endl;

cin>>n;

for(;;){

if(n==1){

break;

if(n%2==0){

n=n/2;

cout<<n<<endl;

else if(n%2==1){

n=(n*3)+1;

cout<<n<<endl;
}

return 0;

Question no 5:
Reverse the digits of an integer without converting it to a string.

#include <iostream>

using namespace std;

int main() {

int n, r;

cout<<"Enter your number"<<endl;

cin>>n;

for(int i=n; i>0; i=i/10){

r=i%10;

cout<<r;

return 0;

Question no 7:
Count the number of digits in a given number without converting it to a string.

#include <iostream>

using namespace std;

int main() {

int n, r, count=0;
cout<<"Enter your number"<<endl;

cin>>n;

for(int i=n; i>0; i=i/10){

r=i%10;

count++;

cout<<count;

return 0;

Question no 8:
Check if a number is a perfect number or not. A perfect number is a positive integer that is equal to the
sum of its proper divisors, excluding itself.

#include <iostream>

using namespace std;

int main() {

int n, sum=0;

cout<<"Enter your number"<<endl;

cin>>n;

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

if(n%i==0){

sum=sum+i;

if(sum==n){

cout<<"Perfect Number";

else if(n==1){
cout<<"Perfect NUmber";

else {

cout<<"Not Perfect";

return 0;

Question no 2:
Find GCD from the conventional method.

#include<iostream>

#include<conio.h>

using namespace std;

int main(){

int n, m, d, larger;

cout<<"Enter first number"<<endl;

cin>>n;

cout<<"Enter the second number"<<endl;

cin>>m;

if(n>m){

larger=n;

else{

larger=m;

cout<<"Factors of "<<n<<" are ";


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

if(n%i==0){

cout<<i<<",";

cout<<endl;

cout<<"Factors of "<<m<<" are ";

for(int i=1; i<=m; i++){

if(m%i==0){

cout<<i<<",";

cout<<endl;

cout<<"Common factors of "<<n<<" and"<<m<<" are ";

for(int i=1; i<=m; i++){

if(n%i==0 && m%i==0){

d=i;

cout<<i<<",";

cout<<endl;

cout<<"The greatest common divisor of "<<n<<" and "<<m<<" is "<<d;

Question no 12:
Given a number, count the number of set bits (bits that are 1) in its binary representation.
#include<iostream>

using namespace std;

int main(){

int n, r, count=0;

cout<<"Enter your number"<<endl;

cin>>n;

for(int i=n; i>0; i=i/2){

r=i%2;

if(r==1){

count++;

cout<<count;

Question no 11:
Calculate the sum of the series: 1 - 1/2 + 1/3 - 1/4 + 1/5 - ... up to 'n' terms.

#include <iostream>

using namespace std;

int main() {

float n, expression=0, count=1;

cin>>n;

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

expression=expression+(1/count);

count++;

expression=expression-(1/count);

count++;
}

cout<<expression;

return 0;

Queston no 10:
Convert a binary number to its decimal representation.

#include <iostream>

#include <cmath>

using namespace std;

int main() {

int n,r, decimal=0, count=0;

cout<<"Enter your binary number"<<endl;

cin>>n;

for(int i=n; i>0; i=i/10 ){

r=i%10;

decimal=decimal+((pow(2,count))*r);

count++;

cout<<decimal;

return 0;

Question no 3:
Find LCM of two numbers using conventional method.

#include <iostream>

using namespace std;


int main() {

int n, m, g, l, gg, ll;

cin>>n;

cin>>m;

if(n>m){

g=n;

l=m;

else{

g=m;

l=n;

gg=g;

ll=l;

for(int i=1; ; i++){

g=gg*i;

for(int j=1; j<=9; j++){

l=ll*j;

if(g==l){

cout<<"The LCM of "<<n<<" and "<<m<<" is "<<g<<endl;

break;

l=l-l;

if(g==l){

break;

g=g-g;

}
return 0;

You might also like