C Program to Find LCM of two Numbers

Examples on different ways to calculate the LCM (Lowest Common Multiple) of two integers using loops and decision making statements.
Lowest Common Multiple
To understand this example, you should have the knowledge of following C programming topics:
The LCM of two integers n1 and n2is the smallest positive integer that is perfectly divisible by both n1 and n2(without a remainder). For example: the LCM of 72 and 120 is 360.

Example #1: LCM using while Loop and if Statement

  1. #include
  2. int main()
  3. {
  4. int n1, n2, minMultiple;
  5. printf("Enter two positive integers: ");
  6. scanf("%d %d", &n1, &n2);
  7. // maximum number between n1 and n2 is stored in minMultiple
  8. minMultiple = (n1>n2) ? n1 : n2;
  9. // Always true
  10. while(1)
  11. {
  12. if( minMultiple%n1==0 && minMultiple%n2==0 )
  13. {
  14. printf("The LCM of %d and %d is %d.", n1, n2,minMultiple);
  15. break;
  16. }
  17. ++minMultiple;
  18. }
  19. return 0;
  20. }
Output
Enter two positive integers: 72
120
The LCM of 72 and 120 is 360.
In this program, the integers entered by the user are stored in variable n1and n2 respectively.
The largest number among n1 and n2 is stored in minMultiple. The LCM of two numbers cannot be less than minMultiple.
The LCM of two numbers can also be found using the formula:
LCM = (num1*num2)/GCD
Learn more on, how to find the GCD of two numbers in C programming.

Example #2: LCM Calculation by Finding GCD

  1. #include
  2. int main()
  3. {
  4. int n1, n2, i, gcd, lcm;
  5. printf("Enter two positive integers: ");
  6. scanf("%d %d",&n1,&n2);
  7. for(i=1; i <= n1 && i <= n2; ++i)
  8. {
  9. // Checks if i is factor of both integers
  10. if(n1%i==0 && n2%i==0)
  11. gcd = i;
  12. }
  13. lcm = (n1*n2)/gcd;
  14. printf("The LCM of two numbers %d and %d is %d.", n1, n2, lcm);
  15. return 0;
  16. }

C Program to Calculate the Sum of Natural Numbers

To compute the sum of natural numbers from 1 to n (entered by the user), loops can be used. You will learn how to use for loop and while loop to solve this problem.
To understand this example, you should have the knowledge of following C programming topics:
The positive numbers 1, 2, 3... are known as natural numbers. The programs below takes a positive integer (let say n) as an input from the user and calculates the sum up to n.

Example #1: Sum of Natural Numbers Using for Loop

  1. #include
  2. int main()
  3. {
  4. int n, i, sum = 0;
  5. printf("Enter a positive integer: ");
  6. scanf("%d",&n);
  7. for(i=1; i <= n; ++i)
  8. {
  9. sum += i; // sum = sum+i;
  10. }
  11. printf("Sum = %d",sum);
  12. return 0;
  13. }
  14.  
The above program takes the input from the user and stores in variable n. Then, for loop is used to calculate the sum upto the given number.

Example #2: Sum of Natural Numbers Using while Loop

  1. #include
  2. int main()
  3. {
  4. int n, i, sum = 0;
  5. printf("Enter a positive integer: ");
  6. scanf("%d",&n);
  7. i = 1;
  8. while ( i <=n )
  9. {
  10. sum += i;
  11. ++i;
  12. }
  13. printf("Sum = %d",sum);
  14. return 0;
  15. }
Output
Enter a positive integer: 100
Sum = 5050
In both programs, the loop is iterated n number of times. And, in each iteration, the value of i is added to sum and i is incremented by 1.
The above programs doesn't work properly if the user enters a negative integer. Here's a little modification of the above program to take input from the user until positive integer is entered.

Example #3: Program to Read Input Until User Enters a Positive Integer

  1. #include
  2. int main()
  3. {
  4. int n, i, sum = 0;
  5. do {
  6. printf("Enter a positive integer: ");
  7. scanf("%d",&n);
  8. }
  9. while (n <= 0);
  10. for(i=1; i <= n; ++i)
  11. {
  12. sum += i; // sum = sum+i;
  13. }
  14. printf("Sum = %d",sum);
  15. return 0;