Showing posts with label Addition. Show all posts
Showing posts with label Addition. Show all posts

How to Write a C program to perform basic math operations on two input whole numbers. The basic operations are addition, subtraction, multiplication, integer division, real division, and modulus.

Write a C program to perform basic math operations on two input whole numbers. The basic operations are addition, subtraction, multiplication, integer division, real division, and modulus (%)



  1. //Write a C program to perform basic math operations on two input whole numbers. The basic operations are addition, subtraction, multiplication, integer division, real division, and modulus (%).
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x,y;
  7.     cin>>x>>y;
  8.     cout<<"Addition:"<<x+y<<endl;
  9.     cout<<"Subtraction:"<<x-y<<endl;
  10.     cout<<"Multiplication:"<<x*y<<endl;
  11.     cout<<"Integer division:"<<x/y<<endl;
  12.     cout<<"Real division:"<<(float)x/y<<endl;
  13.     cout<<"Modulus"<<x%y;
  14. }

C Program To Do Basic Arithmetic Calculation

How to write a C Program To Do Basic Arithmetic Calculation in C Programming Language ?


  1. Addition
  2. Subtraction
  3. Multiplication
  4. Division
  5. Factor

Solution For C Program :

/*C Program To Do Basic Arithmetic Calculation.*/


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a,b,c,ch,num,fact=1,n=0;
char choice='y';
clrscr();
while(choice=='y'||choice=='Y')
  {
  printf("\n:----MENU----:");
  printf("\n[1] :--ADDITION--");
  printf("\n[2] :--SUBTRACTION--");
  printf("\n[3] :--MULTIPLICATION--");
  printf("\n[4] :--DIVISION--");
  printf("\n[5] :--Factorial--");
  printf("\n[6] :--Exit--");
  printf("\nEnter your choice==>");
  scanf("%d",&ch);
  switch(ch)
    {
    case 1:    printf("Enter the 1st number==>");
        scanf("%d",&a);
        printf("Enter the 2nd number==>");
        scanf("%d",&b);
        c=a+b;
        printf("\nSUM of %d and %d==%d",a,b,c);
        break;
    case 2:    printf("Enter the 1st number==>");
        scanf("%d",&a);
        printf("Enter the 2nd number==>");
        scanf("%d",&b);
        c=a-b;
        printf("\nSubstraction of %d and %d==%d",a,b,c);
        break;
    case 3:    printf("Enter the 1st number==>");
        scanf("%d",&a);
        printf("Enter the 2nd number==>");
        scanf("%d",&b);
        c=a*b;
        printf("\nMultiplication of %d and %d==%d",a,b,c);
        break;
    case 4:    printf("Enter the 1st number==>");
        scanf("%d",&a);
        printf("Enter the 2nd number==>");
        scanf("%d",&b);
        c=a/b;
        printf("\nDivision of %d and %d==%d",a,b,c);
        break;
    case 5:    printf("\nEnter the number for factorial==>");
        scanf("%d",&num);
        n=num;
        while(n>0)
            {
          fact*=n;
          n=n-1 ;
          }
        printf("\nFactorial of %d is==%d",num,fact);
        break;
    case 6:    exit(0);
    default:    printf("\nWrong Choice");
    }
  printf("\n\nDo you want to continue==>");
  fflush(stdin);
  scanf("%c",&choice);
  }
getch();
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

Adding two polynomial functions C Program Using Structure

How to write a C Program to Adding Two Polynomial Functions in C Programming Language ?


Solution:
/*Adding two polynomial functions*/

#include <stdio.h>
#include<stdlib.h>

struct poly* link(struct poly*);
void display(struct poly*);
struct poly* addlink(struct poly*,struct poly*, struct poly*);

struct poly
{
int coeff, pow;
struct poly *ptr;
};

int main()
{
struct poly *head1,*head2,*head3;
int num;
head1=NULL;
head2=NULL;
head3=NULL;

while(1)
{
printf("\nEnter\n1->To create first polynomial function\n2->To create second polynomial function\n3->To display first polynomial function\n4->To display second polynomial function\n5->To add both the polynomial functions\n6->To display the new polynomial created after addition\n");
scanf("%d",&num);

switch(num)
{

case 1: head1=link(head1);
break;

case 2: head2=link(head2);
break;

case 3: display(head1);
break;

case 4: display(head2);
break;

case 5: head3=addlink(head1,head2,head3);
break;

case 6: display(head3);
break;

default: printf("\nBoss ! Enter the correct value\n");

}
}
}

struct poly* link(struct poly *head1)
{
struct poly *newnode,*temp;
int value,degree;
newnode=temp=NULL;
printf("\nEnter the coefficient of the polynomial\n");
scanf("%d",&value);
printf("\nEnter the degree for variable x\n");
scanf("%d",&degree);

newnode=(struct poly*) malloc (sizeof (struct poly));

newnode->pow=degree;
newnode->coeff=value;
temp=head1;

if(temp==NULL)
{
printf("\nThe first node is created\n");
newnode->ptr=NULL;
return newnode;
}
else
{
printf("\nThe next node is being linked\n");
temp->ptr=newnode;
temp=temp->ptr;
return head1;
}

}

void display(struct poly* head)
{
if(head == NULL)
{
printf("\nThe polynomial function is not created.\nPlease create a new polynomial function.\n");
}

while(head != NULL && head->ptr != NULL)
{
printf("The polynomial function has been detected\n");
printf("%dx^%d+",head->coeff,head->pow);
head=head->ptr;
}

if(head->ptr == NULL)
{
// printf("\nEntering the last node\n");
printf("%dx^%d",head->coeff,head->pow);
head=head->ptr;
}
}


struct poly* addlink(struct poly* head,struct poly* tail,struct poly *head3 )
{
struct poly *temp1,*temp,*newnode;
temp=newnode=temp1=head3=NULL;

newnode=( struct poly*) malloc(sizeof(struct poly));

if( head == NULL || tail == NULL)
{
printf("\nYou have no node to get added\n");
}
if(head !=NULL && tail == NULL)
{
printf("\nThe existing first polynomial is the final polynomial\n");
return head;
}
if(head == NULL && tail !=NULL)
{
printf("\nThe existing second polynomial is the final polynomial\n");
return tail;
}

head3=newnode;

if(head3==NULL)
{
printf("\nEnter a node first\n");
}

if(head3 != NULL)
{
while(head !=NULL && tail !=NULL)
{
if(head->pow > tail->pow)
{
temp=tail;
temp1=head;
while((head->pow) > (temp->pow))
{
printf("\nThe element's degree in first poly. is greater than the second\n");

if(temp->ptr == NULL)
{
newnode->coeff=temp->coeff;
newnode->pow=temp->pow;
newnode->ptr=NULL;
head3=newnode;
return head3;
temp1=temp1->ptr;

}

temp=temp->ptr;
}
printf("\nYou have come to the end of the tail pointer\n");
}

if(tail->pow > head->pow)
{
temp=head;
temp1=tail;
while((tail->pow) > (temp->pow))
{
printf("\nThe element's degree in second poly. is greater than the first\n");

if(temp->ptr ==NULL)
{
newnode->coeff=temp->coeff;
newnode->pow=temp->pow;
newnode->ptr=NULL;
head3=newnode;
return head3;
temp1=temp1->ptr;
}

temp=temp->ptr;
}

printf("\nYou have come to the end of the tail pointer\n");
}

if(head->pow == tail->pow)
{
temp=head;
temp1=tail;

while((temp->pow == temp1->pow))
{
newnode->coeff = (temp->coeff + tail->coeff);
newnode->pow=temp->pow;
head3=newnode;
return head3;
temp=temp->ptr;
temp1=temp1->ptr;
}
}
}
}

}