You will complete 3 tasks. Task 1: Determine if the input number is prime or not You will ask the user for an integer You will determine if the integer is prime or not and print out that information for the user Task 2: Determine the greatest common factor of 2 numbers You will ask the user for 2 integers You will determine what the greatest common factor of the two numbers is and print that information for the user Task 3:Determine the least common multiple of 2 numbers You will ask the user for 2 integers You will determine the least common multiple of the two numbers and print that information out for the user Ex: If the input is:

Respuesta :

Answer:

Program is written in C++

#include<iostream>

using namespace std;

int main()

{

//1. Prime Number

int num;

cout<<"Input Number: ";

cin>>num;

int chk = 0;

for(int i =2; i <num;i++)

{

 if(num%i==0)

 {

  chk = 1;

  break;

 }

}

if(chk == 0)

{

 cout<<num<<" is prime"<<endl;

}

else

{

 cout<<num<<" is not prime"<<endl;

}

//2. Greatest Common Factor

int num1, num2, x, y, temp, gcf;  

cout<<"Enter two numbers: ";

cin>>num1;

cin>>num2;

x = num1;

y = num2;

while (y != 0) {

   temp = y;

   y = x % y;

   x = temp;

 }

 gcf = x;

 cout<<"Greatest Common Factor: "<<gcf<<endl;

 

// 3. LCM  

cout<<"Enter two numbers: ";

cin>>num1;

cin>>num2;

x = num1;

y = num2;

while (y != 0) {

   temp = y;

   y = x % y;

   x = temp;

 }

 gcf = x;

 int lcm =(num1 * num2)/gcf;

cout<<"Least Common Multiple: "<<lcm<<endl;

return 0;

}

Explanation:

I've added the full source code as an attachment where I make use of comments to explain some lines

Ver imagen MrRoyal