Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct rational{
- int numerator;
- int denominator;
- }*n3 = NULL; //n3 is the final answer
- struct rational *insert(struct rational *n, int a, int b){
- n = (struct rational*) malloc(sizeof(struct rational)); //memory is allocated for inserting values
- n->numerator = a;
- n->denominator = b;
- return n;
- }
- void add(struct rational *n1, struct rational *n2){ //Formula for addition is given
- int a = (n1->numerator)*(n2->denominator) + (n1->denominator)*(n2->numerator);
- int b = n1->denominator * n2->denominator;
- n3 = insert(n3, a, b); //resulting number will have a numerator and denominator
- }
- void multiply(struct rational *n1, struct rational *n2){ //formula for multiplication is given
- int a = n1->numerator * n2->numerator;
- int b = n1->denominator * n2->denominator;
- n3 = insert(n3, a, b); //resulting number will have a numerator and denominator
- }
- int main(){
- int a, b, choice;
- struct rational *n1 = NULL, *n2 = NULL; //good practict to initialize pointers to NULL
- start: printf("Enter the numerator of first number: ");
- scanf("%d", &a);
- printf("Enter the denominator of first number: ");
- scanf("%d", &b);
- n1 = insert(n1, a, b); //n1 will contain a numerator and a denominator
- printf("Enter the numerator of second number: ");
- scanf("%d", &a);
- printf("Enter the denominator of second number: ");
- scanf("%d", &b);
- n2 = insert(n2, a, b); //n2 will also contain numerator and denominator
- while(1){
- printf("\nThe first number is %d/%d", n1->numerator, n1->denominator);
- printf("\nThe second number is %d/%d", n2->numerator, n2->denominator);
- printf("\n1.Addition 2.Multiplication 3.Different numbers 4.Exit: ");
- scanf("%d",&choice);
- switch(choice){
- case 1: add(n1, n2);
- printf("Addition is %d/%d", n3->numerator, n3->denominator);
- break;
- case 2: multiply(n1, n2);
- printf("Multiplication is %d/%d", n3->numerator, n3->denominator);
- break;
- case 3: goto start;
- case 4: return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement