Contact Learn C
Copy

Program 233:To Print Multiplicative Inverse of a Number

Program 233:
 
#include<stdio.h>
main()
{
 int i,num,MI;
 printf("Enter number to get multiplicative Inverse\n");
 scanf("%d",&num);
 for(i=1;i<=num;i++)
 {
  MI=((i*26)+1);
  if(MI%num==0)
  {
   break;
  }
 }
 MI=MI/num;
 printf("Multiplicative inverse of %d is %d\n",num,MI);
 
}
Explanation:

  1. This program starts with initializing :
    • num → To store input from user
    • i →Used as helping variable
    • MI→ To store output Multiplicative Inverse
  2. printf("Enter number to get multiplicative Inverse\n");
     scanf("%d",&num);
    Taking input from user(Let it be 7)
  3. Logic of this program is MI=((I*26)+1)/num . We obtain Multiplicative Inverse by substituting values in I so that the least integer when substituted in place of I (like 1,2,3,4...) the answer should be an integer.
  4. For example:
    let I is 1 then MI=27/7 which is not integer.
    I=2 then MI=((2*26)+1)/7=53/7 which is also not integer
    I=3 then MI=79/7 not an integer
    I=4 then MI=105/7=15(integer) so this is the Multiplicative inverse of number 7 
  5. To achieve this first we do calculation for MI=(i*26)+1 and by substituting from 1 till number(7 here) one by one and checking whether the equation is factor for given number(7) or not and if it goes with 7 giving remainder zero then finally  dividing MI with 7 we will get output.
  6. The main logic of code is:taking num as 7
    for(i=1;i<=num;i++)
     {
      MI=((i*26)+1);
      if(MI%num==0)
      {
       break;
      }
     }
    • Iteration 1:i=1 and 1<=7 which is true so the forloop is executed
      • MI=(1*26)+1=27
      • 27%7!=0 so if part is not executed
      • i is incemented by 1 so i=2
    • Iteration 2:i=2 and 2<=7 which is true so the forloop is executed
      • MI=(2*26)+1=53
      • 53%7!=0 so if part is not executed
      • i is incemented by 1 so i=3
    • Iteration 3:i=3 and 3<=7 which is true so the forloop is executed
      • MI=(3*26)+1=79
      • 79%7!=0 so if part is not executed
      • i is incemented by 1 so i=4
    • Iteration 4:i=4 and 4<=7 which is true so the forloop is executed
      • MI=(4*26)+1=105
      • 105%7==0 which is true so if part is executed
      • And will break out of forloop
      • Finally MI=105
  7. MI=MI/num;
     printf("Multiplicative inverse of %d is %d\n",num,MI);
    MI=105/7=15 
    So inverse of 3 is 15 

 Output:

Multiplicative Inverse

Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide