Sunday 8 January 2012

C PROGRAMMING EXAMPLES


C program to perform addition, subtraction, multiplication and division

C program to perform basic arithmetic operations i.e. add , subtract, multiply and divide two numbers. Numbers are assumed to be integers and will be entered by the user.

C programming code


#include<stdio.h>
 
main()
{
   int first, second, add, subtract, multiply;
   float divide;
 
   printf("Enter two integers\n");
   scanf("%d%d",&first,&second);
 
   add = first + second;
   subtract = first - second;
   multiply = first * second;
   divide = first / (float)second;   //typecasting
 
   printf("Sum = %d\n",add);
   printf("Difference = %d\n",subtract);
   printf("Multiplication = %d\n",multiply);
   printf("Division = %.2f\n",divide);
 
   return 0;
}

Arithmetic operations.
Output:
arithmetic operations program

Comments

#1 Guest : superb

i like this post, was searching this on net from last 1 hours

#2 Guest : but what if the user enters integers in the order 5 and 10?

the difference in that case would come out to be negative which is incorrect because difference is always positive..its the greater - smaller.

#3 adminPs : Use long data type

For large numbers you may use long int data type.
Declare variable as
long var;
and use %ld as conversion specifier.

#4 Guest : hi

very nice

#5 Guest : multiplication

how to multiply two numbers without using multiplication sign ?

#6 adminPs : c program to multiply numbers without using multiplication sign


#include<stdio.h>
 
main()
{
      int x,  y, c, temp, temp1, result = 0;
 
      scanf("%d%d",&x,&y);
      temp = x;
      temp1 = y;
 
      if ( temp < 0 )
         temp = -temp;       /* If x is negative*/
 
      for ( c = 1 ; c <= temp ; c++)
         result = result + temp1;
 
      if( x < 0 && y < 0 )   /* to print proper sign */
         result = -result;
 
      printf("%d\n",result);
      return 0;
 
}

No comments:

Post a Comment