Users Online

· Guests Online: 47

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Program to Find HCF of Two Numbers

C# Program to Find HCF of Two Numbers

 

 

This is a C# Program to find and display the hcf of a given number.

Problem Description

This C# Program Finds and Display the H.C.F of a Given Number.

Problem Solution

In other words the H.C.F is the largest of all the common factors.

Program/Source Code

Here is source code of the C# Program to Find and Display the H.C.F of a Given Number. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Find and Display the H.C.F of a Given Number 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Program
{
    class Program
    {
        public static void Main(string[] args)
        {
            int num1,num2,i;
            int hcf =0;
            Console.Write("\nEnter the First Number : ");
            num1 = int.Parse(Console.ReadLine());
            Console.Write("\nEnter the Second Number : ");
            num2 = int.Parse(Console.ReadLine());
            for(i=1;i<=num1||i<=num2;++i)
            {
                if(num1%i==0 && num2%i==0)
                {
                    hcf=i;
                }
            }    
            Console.Write("\nCommon Factor is : ");
            Console.WriteLine(hcf);
            Console.Read();
         }
    }
}
Program Explanation

In this C# is program we are reading the first and second numbers using ‘num1’ and ‘num2’ variables respectively. If condition statement is used to check the condition that the modulus of the value of ‘num1’ variable by the value of ‘i’ variable is equal to 0 and the modulus of the value of ‘num2’ variable by the value of ‘i’ variable is equal to 0. If the condition is true, then execute the statement and assign the number to hcf variable. Print the HCF of a given number.

 
Runtime Test Cases
 
Enter the First Number : 12
Enter the Second Number : 16
Common Factor is : 4

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.74 seconds
10,846,567 unique visits