Question

In C# write a program for factorizing natural numbers and computing GCF & LCM of two...

In C# write a program for factorizing natural numbers and computing GCF & LCM of two natural numbers. Users must be promoted to enter two numbers. use class List<>to represent factorizations.

Sample Program:

Enter first num: 36   

Enter second num: 48

Factor of 36 are: 2 2 3 3

Factor of 48 are: 2 2 2 2 3

common factors of 36 & 48 are: 2 2 3

GCF of 36 and 48 is: 12

LCM of 36 and 48 is: 144

0 0
Add a comment Improve this question Transcribed image text
Answer #1

for the given question code in C# is attached below:

using System;
using System.IO;
using System.Collections.Generic;

class Driver{
  
int GCF(int num1,int num2){ //method to find GCF.
if(num2==0)
return num1;
else return GCF(num2,num1%num2);
}
  
void factorization(int num,List<int> factor){ //method to find factorization and storing in a List.
int i=2;
while(num!=1){ //exits the loop when num == 1.
if(num%i==0){
factor.Add(i); //add factor to list.
num/=i;
}
else
i++;
}
}
  
public static void Main() {
int num1,num2;
  
Console.Write("enter first num: ");
num1 = Convert.ToInt32(Console.ReadLine());// input 1st number by user.
Console.Write("enter second num: ");// input 2nd number by user.
num2 = Convert.ToInt32(Console.ReadLine());
  
//lists of both numbers factor.
List<int> factor1 = new List<int>();
List<int> factor2 = new List<int>();
  
Driver obj = new Driver(); // object of Driver class to call their methods.
  
//calling factorization method for both numbers.
obj.factorization(num1,factor1);
obj.factorization(num2,factor2);
  
Console.Write("Factor of "+num1+" "+"are: ");
foreach(var fac in factor1){//printing factors of num1 on console.
Console.Write(fac+" ");
}
  
Console.WriteLine();
Console.Write("Factor of "+ num2 +" are: ");
foreach(var fac in factor2){//printing factors of num2 on console.
Console.Write(fac+" ");
}
  
Console.WriteLine();
int gcf=obj.GCF(num1,num2);//calling method GCF and storing answer in variable gcf.
Console.Write("GCF of " + num1 + " and " + num2 + " is: " + gcf);
  
Console.WriteLine();
//since lcm(a,b) = (a*b)/gcf(a,b);
Console.Write("LCM of " + num1 + " and " + num2 + " is: " + (num1*num2)/gcf);
}
}

Refer to the screenshot attached below for better uderstanding the identation:

Below is the screenshot of Output attached:

if my answer helps you then please UPVOTE,

and if you have any queries comment below.

Thank you.

Add a comment
Know the answer?
Add Answer to:
In C# write a program for factorizing natural numbers and computing GCF & LCM of two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • FindGCF.py 1 #The Greatest Common Factor (GCF) of two numbers is the 2 #largest number that...

    FindGCF.py 1 #The Greatest Common Factor (GCF) of two numbers is the 2 #largest number that divides evenly into those two 3 #numbers. For example, the Greatest Common Factor of 48 4 #and 18 is 6. 6 is the largest number that divides evenly 5 #into 48 (48 / 6 = 8) and 18 (18 / 6 = 3). 6 # 7 #Write a function called find gcf. find gcf should have 8 #two parameters, both integers. find_gcf should return...

  • C++ Part1: Palindrome detector Write a program that will test if some string is a palindrome...

    C++ Part1: Palindrome detector Write a program that will test if some string is a palindrome Ask the user to enter a string Pass the string to a Boolean function that uses recursion to determine if something is a palindrome or not. The function should return true if the string is a palindrome and false if the string is not a palindrome. Main displays the result ( if the string is a palindrome or not Part 2: Compute the Greatest...

  • 1) Given two positive numbers, write a program using while loop to determine their lowest common...

    1) Given two positive numbers, write a program using while loop to determine their lowest common multiple. You cannot use any automatic LCM, GCD finder function or return command. Program Inputs • Enter the first number: – The user can enter any positive whole number, no error checking required • Enter the second number: – The user can enter any positive whole number, no error checking required Program Outputs • The LCM of X and Y is Z! – Replace...

  • Solve the following question using Matlab language only. Least common multiple (LCM) of two numbers is...

    Solve the following question using Matlab language only. Least common multiple (LCM) of two numbers is the smallest number that they both divide. For example, the LCM of 2 and 3 is 6, as both numbers can evenly divide the number 6. Find the LCM of two numbers using recursion Hint: You may assume that the first number is always smaller than the second number. Examplel First number for LCM:3 Second number for LCM 19 The LCM of 3 and...

  • Define a function that creates a list of all the numbers that are factors of the...

    Define a function that creates a list of all the numbers that are factors of the user's number. For example, if the function is called factor, factor(36) should return [1, 2, 3, 4, 6, 9, 12, 18, 36]. The numbers in your list should be sorted from least to greatest, and 1 and the original number should be included. Remember to consider negative numbers as well as 0. Bonus: Have the program print the factors of the users number in...

  • Assignment Develop a program to analyze one or more numbers entered by a user. The user...

    Assignment Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. Input should be limited to numbers from 1 through 1000. Determine if a number is a prime number or not. A prime number is one whose only exact divisors are 1 and the number itself (such as 2, 3, 5, 7, etc.). For non-prime numbers, output a list of all divisors of the number. Format your...

  • Write a program in c++ that generates a 100 random numbers between 1 and 1000 and...

    Write a program in c++ that generates a 100 random numbers between 1 and 1000 and writes them to a data file called "randNum.dat". Then re-open the file, read the 100 numbers, print them to the screen, and find and print the minimum and maximum values. Specifications: If your output or input file fails to open correctly, print an error message that either states: Output file failed to open Input file failed to open depending on whether the output or...

  • Write a C program that asks the user to enter three numbers (integres). A menu will...

    Write a C program that asks the user to enter three numbers (integres). A menu will be displayed to let the user choose one of the three options: print the two highest numbers, product of the three numbers or the division of the second by the by the third if the third is not zero. See the sample runs. Required: Your code must use -One function to get the numbers from the user One function for each of the three...

  • 1. Write a program in Assembly language using MIPS instruction set that reads two integer numbers...

    1. Write a program in Assembly language using MIPS instruction set that reads two integer numbers from the user named as start and end number and finds out all the prime numbers between start and end (including start and end). Your program should do the validation of both the numbers as follows: i. start number must be smaller or equal to the end number. ii. Both numbers must be positive. iii. The maximum value for the end number is 10000...

  • Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display...

    Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display the numbers in 3 formats. Then check, whether the larger of the 2 is evenly divisible by the smaller. Detail: Write a complete C program, more complex than the typical "hello world" program. Prompt the user to enter 2 integer numbers that are not negative. After either entry, display that number again. Print the smaller in hexadecimal, in decimal, and in octal format. Include...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT