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:
- This program starts with initializing :
- num → To store input from user
- i →Used as helping variable
- MI→ To store output Multiplicative Inverse
printf("Enter number to get multiplicative Inverse\n"); scanf("%d",&num);
Taking input from user(Let it be 7)- 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.
- 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
- 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.
- 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
- Iteration 1:i=1 and 1<=7 which is true so the forloop is executed
MI=MI/num; printf("Multiplicative inverse of %d is %d\n",num,MI);
MI=105/7=15
So inverse of 3 is 15
Output: