Wednesday 28 August 2013

Arithmetic Operators | C Programming Language pdf



Arithmetic Operators: 

C provides all the basic arithmetic operators, they are +, -, *, /, %
Integer division truncates any fractional part. The modulo division produces the remainder of an integer division.
Eg:  a + b  a – b  a * b
      -a * b  a / b  a % b
Here 'a' and 'b' are variables and are known as operands. % cannot be used for floating point data. C does not have an operator for exponentiation.

Integer Arithmetic: When the operands in an expression are integers then the expression is an integer expression and the operation is called integer arithmetic. This always yields an integer value. For Eg. a = 14 and n = 4 then
a - b = 10               Note : During modulo division,the
a + b = 18            sign of the result is always the sign
a * b = 56            of the first operand (the dividend )
a / b = 3                    - 14 % 3 = -2
a % b = 2                    -14 % - 3 = 2
14 % -3 = 2

1. Write a program to illustrate the use of all Arithmetic operator
main ( )
{
int sum, prod , sub, div, mod, a, b ;
printf(“Enter values of a, b :”) ;
scanf(“ /.d %d”, & a, & b) ;
sum = a+b ;
printf(“sum = %d”, sum);
sub = a-b;
printf(“sub = %d”, sub);
prod = a * b ;
printf(“prod = %d”, a* b);
div = a/b;
printf(“ Div = %d”, div);
mod = a % b ;
printf(“ mod = %d”,a % b);
}

2. WAP to convert given no. of days into years, months days

3. WAP to use various relational operators and display their return values.
main ( )
{
printf(“ in condition : Return Values In”);
printf(“ In 10! = 10 : %5d”, 10! = 10);
printf(“ In 10 = 10 : %5d” , 10 == 10);
printf(“ In 10>=10 : %5d”, 10>=10);
printf(“ In 10<+100 : %5d”, 10<100);
printf(“ In 10! = 9 : %5d”, 10!=9);
}
Real Arithmetic / Floating Pont Arithmetic:
Floating Point Arithmetic involves only real operands of decimal or exponential
notation. If x, y & z are floats, then
x = 6.0/7.0 = 0.857143
y = -1.0/3.0 = 0.333333
z = 3.0/2.0 = 1.500000
% cannot be used with real operands

Mixed mode Arithmetic: When one of the operands is real and the other is integer the expression is a mixed mode arithmetic expression.
Eg: 
15/10.0 = 1.500000
15/10 = 1
10/15 = 0
-10.0/15 = -0.666667

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved