logo
  • Home
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C++ Program to Implement Extended Eucledian Algorithm

Posted on January 1, 2014 by Manish
This C++ Program demonstrates the implementation of Extended Eucledian Algorithm. For the modular multiplicative inverse to exist, the number and modular must be coprime.

Here is source code of the C++ Program to implement Extended Eucledian Algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Extended Eucledian Algorithm
  3.  */
  4. #include <iostream>
  5. #include <utility> 
  6.  
  7. using namespace std;
  8. /* return the gcd of a and b followed by the pair x and y of 
  9.   equation ax + by = gcd(a,b)
  10. */
  11. pair<int, pair<int, int> > extendedEuclid(int a, int b) 
  12. {
  13.     int x = 1, y = 0;
  14.     int xLast = 0, yLast = 1;
  15.     int q, r, m, n;
  16.     while (a != 0) 
  17.     {
  18.         q = b / a;
  19.         r = b % a;
  20.         m = xLast - q * x;
  21.         n = yLast - q * y;
  22.         xLast = x; 
  23.         yLast = y;
  24.         x = m; 
  25.         y = n;
  26.         b = a; 
  27.         a = r;
  28.     }
  29.     return make_pair(b, make_pair(xLast, yLast));
  30. }
  31.  
  32. int modInverse(int a, int m) 
  33. {
  34.     return (extendedEuclid(a, m).second.first + m) % m;
  35. }
  36.  
  37. //Main
  38. int main()
  39. {
  40.     int a, m;
  41.     cout<<"Enter number to find modular multiplicative inverse: ";
  42.     cin>>a;
  43.     cout<<"Enter Modular Value: ";
  44.     cin>>m;
  45.     cout<<modInverse(a, m)<<endl;
  46. }

$ g++ eucledian.cpp
$ a.out
 
Enter number to find modular multiplicative inverse: 133
Enter Modular Value: 135
67
 
------------------
(program exited with code: 1)
Press return to continue

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

If you wish to look at all C++ Programming examples, go to C++ Programs.
« Prev Page - C++ Program to Implement Knuth–Morris–Pratt Algorithm (KMP)
» Next Page - C++ Program to Implement Multimap in STL
« C++ Program to Implement Knuth–Morris–Pratt Algorithm (KMP)
C++ Program to Implement Multimap in STL »

Deep Dive @ Sanfoundry:

  1. C Programming Examples on Computational Geometry Problems & Algorithms
  2. C Programming Examples on Numerical Problems & Algorithms
  3. Java Programming Examples on Collection API
  4. C Programming Examples on Data-Structures
  5. C Programming Examples on Graph Problems & Algorithms
  6. C++ Programming Examples on STL
  7. Java Programming Examples on Numerical Problems & Algorithms
  8. C++ Programming Examples on Set & String Problems & Algorithms
  9. C Programming Examples on Set & String Problems & Algorithms
  10. C++ Programming Examples on Numerical Problems & Algorithms
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry