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());
                if(num1%i==0 && num2%i==0)
            Console.Write("\nCommon Factor is : ");
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.

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases
Enter the First Number : 12
Enter the Second Number : 16
Common Factor is : 4

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.