2/24/2016

Mathematical operations and arithmetic operators

Mathematical operations use arithmetic operators.

Arithmetic Operators

   multiplication
/    division
 remainder after division (modulo arithmetic)
+   addition
-    subtraction and unary minus

1.Summation of two numbers using C program.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Welcome");
printf("Enter first number:");
/* here no use of \n */
scanf("%d",&a);
printf("Enter second number:\n");
scanf("%d",&b);
printf("Sum=%d",a+b);
getch();
}
Output:
WelcomeEnter first number:6
Enter second number:
4
Sum=10

2.A c program which accept two integer number and gives the values of  their summation,subtraction,multiplication,division and remainder.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,e,f,g;
printf("Enter both the numbers:\n");
scanf("%d%d",&a,&b);
c=a+b;
d=a-b;
e=a*b;
f=a/b;
g=a%b;
printf("Sum=%d",c);
printf("\nSubtraction=%d\n",d);
printf("Multiplication=%d\nDivision= %d",e,f);
printf("\nRemainder=%d",g);
getch();
}
Output:
Enter both the numbers:
7 3
Sum=10
Subtraction=4
Multiplication=21
Division=2
Remainder=1

3.A C program to find the hcf and lcm of two integer number.

#include<stdio.h>
#include<conio.h>
int main() {
 int x, y, hcf, lcm;
int temp,i,mul;
  printf("Enter two integers(first enter greater number)\n");
  scanf("%d%d",&x,&y);

  mul=x*y;

  while(y != 0)
{
temp=x%y;
x=y;
y=temp;
i++;

}

hcf=x;
  lcm = mul/hcf;

  printf("Greatest common divisor  = %d\n", hcf);
  printf("Least common multiple  = %d\n",lcm);

  return 0;
}

Output:
Enter two integers(first enter greater number)
12
5
Greatest common divisor  = 1
Least common multiple  = 60


4.A C program for the conversion of temperature from fahrenheit to degree celcius.


#include <stdio.h>
void main()
{

  double fahrenheit;
double celsius;

printf("Enter the temperature in degrees fahrenheit:");
scanf("%lf", &fahrenheit);
celsius = (5.0/9.0) * (fahrenheit-32);
printf ("The converted temperature is %lf\n", celsius);

getch();

}


Output:
Enter the temperature in degrees fahrenheit:100
The converted tempetature is 37.777778

5.A program to print the prime numbers till a given number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
int temp=0;
int i,j;
printf("Enter the value of n:");
scanf("%d",&n);
printf("List of prime numbers:\n1\n2\n");
for(i=3;i<=n;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
{
temp==1;
}
}
if(temp==0)
{
printf("%d\n",i);
}
temp=0;
}
getch();
}

Output:
Enter the value of n:8
List of prime numbers:
1
2
3
5
7


6.C program for the simple calculator function of two numbers

first method:-(by if else statement)

#include<stdio.h>
#include<conio.h>
void main()
{
float a,b;
int c;
printf("Enter two numbers:");
scanf("%f%f",&a,&b);
printf("Enter according to the following instruction:\n");
printf("Summation->1\nSubtraction->2\nMultiplication->3\nDivision->4\n");
scanf("%d",&c);
if(c==1)
{
printf("Sum=%f",a+b);
}
else if(c==2)
{
printf("Subtraction=%f",a-b);
}
else if(c==3)
{
printf("Multiplication=%f",a*b);
}
else
{
printf("Division=%f",a/b);
}
getch();
}

Output:
Enter two numbers:12 6
Enter according to the following instruction:
Summation->1
Subtraction->2
Multiplication->3
Division->4
3
Multiplication=72.000000

Second method:-(by switch statement)

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float a,b;
char operator;
printf("Enter two numbers:");
scanf("%f%f",&a,&b);
printf("Enter operator:");
scanf("%c",&operator);
switch(operator)
{
case '+':
printf("Sum=%f",a+b);
break;

case '-':
printf("Subtraction=%f",a-b);
break;

case '*':
printf("Multiplication=%f",a*b);
break;

case '/':
printf("Division=%f",a/b);
break;

default:
printf("Something wrong");
break;

}
return 0;
}

Output:
Enter two numbers:12 4
Enter operator:+
Sum=16.000000

7.A C program to find the simple interest.
#include<stdio.h> 
#include<conio.h>
void main()
{
int amount, rate, time, ans;

printf("\nEnter Principal Amount : ");
scanf("%d", &amount);

printf("\nEnter Rate of Interest : ");
scanf("%d", &rate);

printf("\nEnter Period of Time : ");
scanf("%d", &time);

ans = (amount * rate * time)/100;
/*Simple interest formula*/
printf("Simple Interest :  %d",ans);
getch();
}

Output:
Enter Principal Amount: 100
Enter Rate of Interest: 5
Enter Pariod of Time: 4
Simple interest : 20

8.A C program to find nPr and nCr.

#include<stdio.h>
#include<conio.h>

void main()
{
  int n,r,ncr,npr,i;
int fact1=1,fact2=1,fact3=1;
  printf("Enter the value of n and r\n");
  scanf("%d%d",&n,&r);
for(i=1;i<=n;i++)
{
fact1=fact1*i;
}

for(i=1;i<=r;i++)
{
fact2=fact2*i;
}

for(i=1;i<=n-r;i++)
{
fact3=fact3*i;
}

  npr = fact1/fact3;
ncr = fact1/(fact2*fact3);
  printf("%dC%d = %d\n", n, r, ncr);
  printf("%dP%d = %d\n", n, r, npr);
 getch();
}

Output:
Enter the value of n and r
4 3
4C3 = 4
4P3 = 24









No comments:

Post a Comment