Arithmetic Operators
Arithmetic Operators का उपयोग सामान्य अंकगणितीय गड़नाओ के लिए किया जाता है जैसे – addition(जोड़ना), subtraction(घटाना) multiplication(गुणा करना) औऱ division (भाग करना) । The Basic arithmetic operators are +, - , *, / and %.
विवरण निम्न प्रकार है।
Arithmetic Operator
|
Name
|
Operation
|
+
|
Addition Operator
|
Add operands
|
-
|
Subtraction Operator
|
Subtract second operand from first
|
*
|
Multiplication Operator
|
Multiply operands
|
/
|
Division Operator
|
Divide and gives the quotient as the result
|
%
|
Modulo Operator
|
Divide and gives remainder as the result
|
Addition, Subtraction और Multiplication ये सभी operations क्रमशः +, - और * द्वारा किये जाते है । प्रत्येक operator को दो operands की जरूरत होती है एक left में और दूसरा right में. यदि दोनों integer है तो परिणाम (result) integer आएगा, यदि दोनों real, तो परिणाम (result) real आएगा, यदि एक integer और दूसरा real है, तो result, real आएगा.
Division
There are two types of division performed C. The first type
of division is performed using /
operator, which gives the quotient
and the second type of division is performed using % operator, which gives the remainder.
The detailed description of these two operators is given here.
C में दो तरह के division(भाग) किये जाते है पहले प्रकार का division / operator का उपयोग करके किया जाता है, जो भाग्यफल (quotient) देता है और दूसरे प्रकार का division % operator का उपयोग करके किया जाता है जो शेषफल(remainder) देता है।
इन दोनों Operators का विवरण निम्न प्रकार है।
इन दोनों Operators का विवरण निम्न प्रकार है।
The Division Operator /
The / operator divides the first operand by second, and gives the quotient as result. If both operands are integer, an integer quotient is obtained. If both operands are real, a real quotient is obtained. If one operand is integer and the other is real, a real quotient is obtained.For example 20/3 is solved like
this –
And the result obtained is 6, which is the integer quotient of division
And the result obtained is 6, which is the integer quotient of division
The Modulo Operator %
The % operator gives the remainder of division. For this operator, both operators must be integer. You cannot apply it to real operands.For example 20 % 3 is solved like this –
And the result obtained is 2,
which is the remainder of division.
Unary + and –
Unary + and – are used to denote positive and negative values respectively. Although, a number without a sign is always positive, you can use + sign to explicitly specify a positive value. For example, the following statement show the use of unary +.
int i = +25;
The + which precedes the 25 in the declaration, is the unary +.
Similarly, in the statement –
int j = -45;
the – which precedes the 45 is the unary -.
If x = 25 then y = - x will put -25 into y. x will remain unchanged.
If x = -25 then y = -x will put 25 into y. x will remain unchanged.
Unary + and – are used to denote positive and negative values respectively. Although, a number without a sign is always positive, you can use + sign to explicitly specify a positive value. For example, the following statement show the use of unary +.
int i = +25;
The + which precedes the 25 in the declaration, is the unary +.
Similarly, in the statement –
int j = -45;
the – which precedes the 45 is the unary -.
If x = 25 then y = - x will put -25 into y. x will remain unchanged.
If x = -25 then y = -x will put 25 into y. x will remain unchanged.
*/ Use of Arithmetic operators*/
#include<stdio.h>
#include<stdio.h>
int main(void)
{
int a,b,add,sub,mul,div,mod;
clrscr();
printf("Enter value of a & b");
scanf("%d%d",&a,&b);
add=a+b;
printf("Addition of a & b= %d\n", add);
sub=a-b;
printf("Subtraction of a & b= %d\n", sub);
#include<stdio.h>
#include<stdio.h>
int main(void)
{
int a,b,add,sub,mul,div,mod;
clrscr();
printf("Enter value of a & b");
scanf("%d%d",&a,&b);
add=a+b;
printf("Addition of a & b= %d\n", add);
sub=a-b;
printf("Subtraction of a & b= %d\n", sub);
mul=a*b;
printf("Multiplication of a & b= %d\n", mul);
div=a/b;
printf("division of a & b= %d\n", div);
printf("Multiplication of a & b= %d\n", mul);
div=a/b;
printf("division of a & b= %d\n", div);
mod=a%b;
printf("Mod of a & b= %d\n", mod);
getch();
return 0;
}
printf("Mod of a & b= %d\n", mod);
getch();
return 0;
}
Enter value of a & b 10 5
Addition of a & b = 15
Subtraction of a & b = 5
Multiplication of a & b = 50
Division of a & b = 2
Mod of a & b = 0
Addition of a & b = 15
Subtraction of a & b = 5
Multiplication of a & b = 50
Division of a & b = 2
Mod of a & b = 0
*/ Program
to get the sum of last & first digit of integer four digit number*/
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
void
main()
{
int
number, first_digit, last_digit, sum;
clrscr();
printf("Enter
the four digit number");
scanf("%d",&
number);
first_digit
= number / 1000;
last_digit
= number % 10;
sum
= first_digit+ last_digit;
printf("sum
of last & first digit =%d", sum);
getch();
}
Enter
the four digit number 1234
sum
of last & first digit =5
मान लीजिए कोई चार अंक की संख्या 1234 है और इस संख्या के पहले
और अंतिम अंक का योग निकलना है तो
पहला statement
first_digit
= number/1000 यहाँ
number
की
जगह 1234
आएगा
अब
first_digit
= 1234/1000 इससे
भागफल 1
निकलेगा।
अब
first_digit
= 1 एवम
दूसरा statement
last_digit
= number%10 यहाँ
भी number
की
जगह 1234
आएगा
अब
last_digit
= 1234%10 इससे
शेषफल 4
निकलेगा।
अब
last_digit
= 4 एवम
तीसरा statement
sum=first_digit+
last_digit
यहाँ first_digit की जगह 1 और last_digit की जगह 4 आएगा और हमे sum ज्ञात हो जाएगा sum = 5
Arithmetic Operations on characters
When you perform any arithmetic operation on characters, it is actually performed on their ASCII values. For example, consider the two declarations –
char ch1 = ‘A’; and char ch2 = ‘B’;
As you know characters A and B are corresponds to ASCII values 65 and 66. Because of this, ch1 + ch2
would mean 65 + 66 i.e. 131. The same is true for other arithmetic operators. They will perform their operations on ASCII values if used with characters. Here is the program to show this.
Arithmetic Expressions
Arithmetic Expressions
An expression consists of arithmetic operators, along with their operands, is termed as arithmetic expression. The result of an arithmetic expression is always a numeric value. Arithmetic expressions may have parenthesis and function calls. If an arithmetic expression has, then parenthesis and functions calls are solved first then other arithmetic operators are solved (See precedence table). For example, the following arithmetic expression –
sqrt(25) + 5 * (2 + 3);
Have parenthesis around 2 + 3 and a call to function sqrt( ). The result of this expression will be 30.0 as described here.
sqrt(25) + 5 * (2 + 3); Given expression
5.0 + 5 * 5 Function call and parenthesis are solved first
5.0 + 25 * Solved
30.0 + solve, 30.0 is the result of expression
ग कोडर शुरुआत कोडर के लिए प्रोग्रामिंग नमूना कार्यक्रम
ReplyDeleteप्रोग्राम लेखकों के लिए बाइनरी सर्च ट्री नमूना कोड