Lab 5
Lab 5
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>
int main() {
int n, m, d, c, i;
cin>>n;
cin>>m;
if(n>=m){
c=n;
else{
c=m;
d=i;
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.
#include <iostream>
int main() {
int n, m, d, c, lcm;
cin>>n;
cin>>m;
if(n>m){
c=n;
else{
c=m;
d=i;
lcm=(n*m)/d;
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>
int main() {
int n;
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>
int main() {
int n, r;
cin>>n;
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>
int main() {
int n, r, count=0;
cout<<"Enter your number"<<endl;
cin>>n;
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>
int main() {
int n, sum=0;
cin>>n;
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>
int main(){
int n, m, d, larger;
cin>>n;
cin>>m;
if(n>m){
larger=n;
else{
larger=m;
if(n%i==0){
cout<<i<<",";
cout<<endl;
if(m%i==0){
cout<<i<<",";
cout<<endl;
d=i;
cout<<i<<",";
cout<<endl;
Question no 12:
Given a number, count the number of set bits (bits that are 1) in its binary representation.
#include<iostream>
int main(){
int n, r, count=0;
cin>>n;
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>
int main() {
cin>>n;
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>
int main() {
cin>>n;
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>
cin>>n;
cin>>m;
if(n>m){
g=n;
l=m;
else{
g=m;
l=n;
gg=g;
ll=l;
g=gg*i;
l=ll*j;
if(g==l){
break;
l=l-l;
if(g==l){
break;
g=g-g;
}
return 0;