Write C# program to find HCF of any two numbers


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.

techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public class csharpExercise
  7. {
  8. static void Main(string[] args)
  9. {
  10. int i, num1, num2, min, HCF = 1;
  11.  
  12. // Reading 2 numbers from user
  13. Console.WriteLine("Enter any number to print in words: ");
  14. num1 = Convert.ToInt32(Console.ReadLine());
  15. num2 = Convert.ToInt32(Console.ReadLine());
  16.  
  17.  
  18. // Find min number between two numbers
  19. min = (num1 < num2) ? num1 : num2;
  20.  
  21. for (i = 1; i <= min; i++)
  22. {
  23. if (num1 % i == 0 && num2 % i == 0)
  24. {
  25. HCF = i;
  26. }
  27. }
  28.  
  29. Console.WriteLine("HCF of " + num1 + " and " + num2 + " is: " + HCF);
  30.  
  31. Console.ReadLine();
  32. }
  33. }

Result

C# language techstudy

     





Related Topics




Popular Posts