0% found this document useful (0 votes)
10 views10 pages

Part2 Documents

The document provides 10 programming exercises including: 1) finding the sum and average of 5 numbers, 2) finding the square and cube of a number, 3) swapping two numbers with and without a third variable, 4) converting distances between units, 5) finding the area of shapes, 6) calculating simple interest, 7) calculating employee salary from basic pay, allowances, and deductions, 8) finding the sum of digits in a 4 digit number, 9) reversing a 4 digit number, and 10) converting between Fahrenheit and Celsius temperatures. It also discusses conditional operators, the sizeof operator, operator precedence, symbolic constants, and the comma operator.

Uploaded by

hajayanth2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Part2 Documents

The document provides 10 programming exercises including: 1) finding the sum and average of 5 numbers, 2) finding the square and cube of a number, 3) swapping two numbers with and without a third variable, 4) converting distances between units, 5) finding the area of shapes, 6) calculating simple interest, 7) calculating employee salary from basic pay, allowances, and deductions, 8) finding the sum of digits in a 4 digit number, 9) reversing a 4 digit number, and 10) converting between Fahrenheit and Celsius temperatures. It also discusses conditional operators, the sizeof operator, operator precedence, symbolic constants, and the comma operator.

Uploaded by

hajayanth2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Programming Exercises

1. Write a program to find the sum and average of five numbers


2. Find the square and cube of a given number
3. Swap two numbers with and without using the third variable
4. Covert the given distance in kms into m, cm, feet and inches
5. Find the area of a circle and triangle
6. Calculate the simple interest
7. Find the total salary of the employee if his basic pay is 12500, HRA is 40%
of basic and DA is 25 % of basic.
8. Write a program to find the sum of the digits of a given 4 digit number
9. Reverse a given 4 digit number
10. Write a program to convert a temp in Fahrenheit into centigrade
(cen = 5/9(fah-32)
Conditional operator ? :
• Syntax :
exp1?exp2:exp3

main() OUTPUT:
{
Enter the Values of A and B
int A, B; 10 20
printf(“Enter the Values of A and B”) Y=20

scanf(“%d%d”, &A, &B); // 100 15 Enter the Values of A and B


15 5
Y = A > B ? A : B;
Y=15
printf(“\nY=%d”, Y);
}
Examples
• Check if a number is positive
(num>=0)? printf(“no is positive”) : printf(“no is negative”);

• Check if a number is Odd or Even 7 % 2 = 1


• (num%2)? printf(“no is odd”) : printf(“no is Even”);

• Compare two numbers


(A>B)? printf(“ A is greater”) : printf(“ B is greater”);

• Compare two numbers


(A==B)? printf(“ Numbers are EQUAL”) : printf(“Numbers are NOT EQUAL”);

• Given number is palindrome : 1234 4321


(num == rev_num)? printf(“No is Palindrome”) : printf(“No is not a palindrome”);
Important point

• The second expression in conditional operator can be omitted

• Example:
int x = a > b? : b;

X will be assigned b if the exp1 is false else the result of exp1 is


assigned
sizeof() operator
main()
{
int a =10;
float b = 3.142;
char c = ‘b’;

printf(“Sizeof a = %d”, sizeof(a)); // 4


printf(“Sizeof b = %d”, sizeof(b)); //4
printf(“Sizeof c = %d”, sizeof(c)); // 1

printf(“Sizeof int = %d”, sizeof(int)); // 4


printf(“Sizeof float = %d”, sizeof(float)); // 4
printf(“Sizeof char = %d”, sizeof(char)); //1
}
Operator Precedence
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative */% Left to right


Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left

Comma , Left to right


Operator Precedence Examples
Y = 3+2*4+10/2 Y = 3 + 2 < 4 + 10/2
= 3+8+10/2 = 3+2 < 4 + 5
= 3+8+5 = 5 < 4+5
=11+5 =5<9
16 Y=1

Y = 3+2/4+10*2
= 3+ 0 + 10 * 2
= 3+0+20
= 3+20
= 23
Predict the output
main()
{
int _ = 10;
int _ _ = 20;
int _ _ _;
_ _ _ = _ + _ _; // 10 + 20
printf(“%d”, _ _ _);
}
Output: 30
Symbolic Constants

#define PI 3.142
main()
{
int rad;
float area;
area = PI * rad * rad;
printf(“Area of a circle = %f”, area);
}
Comma operator
main() main()
{ {
int a = 10,20,30; int a;
printf(“%d”, a); a=(10,20,30);
} printf("%d", a);
}
main()
{ The evaluation will happen from left to right
int a ; and the right most value is valid assignment
a= 10,20,30; main()
printf(“%d”, a); {
} int a, b=3;
a=(b++, b=b+3, b++);
printf(“a=%d”, a);
}

You might also like