Assignments on Variables, Operators, Expressions and Assignment statements
1. Define an integer (int) varialbe. Read a number from the user into this
variable. Finally print the value in the variable. Also print the
variable as hexadecimal(hex) number and as octal number.
2. Define a short integer (short) varialbe. Read a number from the user into
this variable. Finally print the value in the variable. Also print the
variable as hex number and octal number.
3. Define a character (char) varialbe. Read a character from the user into
this variable. Finally print the character in the variable.
Also print the char variable as decimal number and also as hex number.
4. Define a small integer (char) varialbe. Read a small number from the
user into this variable. Finally print the variable.
Also print the variable as hex number and octal number.
5. Define a 64 bit integer (long long) varialbe. Ask user to enter his
mobile number. Read and store the mobile number in the 64 bit integer.
Print the mobile phone number.
6. Define two integer variables. Read two numbers from the user into these
variables. Next calculate and print the sum, difference, product,
quotient and remainder.
7. Read two integers from the user (that is first define two integer variables
and then read). Perform all possible conditional operations on these
two integers. Print the results of all these conditional operations.
Following are the possible conditinal operators:
Equal to(==) Less than(<) Greater than(>)
Less than or Equal to (<=) Greater than or equal to(>=)
Not equal to(!=)
8. Read two small integers from the user in hex format. You should ask user
to enter only hexadecimal numbers with maximum two digits.
Perform all possible bit wise operations on these two numbers. Print the
results of all these bit wise operations.
Following are the possible bit wise operations:
AND(&) OR() EXOR(^) Complement(~)
Shift left(<<) Shift right (>>)
9. Read two integers from the user. Perform all possible logical
operations on these two integers. Print the results of all these
logical operations. Following are the possible logical operators:
AND(&&) OR() NOT(!)
10. Read three integers a, b, and c from the user.
Write the condition to check if a, b and c are equal.
Write the condition to check a is bigger than b and c.
Write the condition to check a is smaller than b and c.
Print the condition result in each case.
Note: Use logical operators to combine the conditional operations on these
variables.
11. Define two float variables, read two float numbers from the user.
Print their sum, difference, product and quotient.
12. Define two double variables, read two float numbers from the user.
Print their sum, difference, product and quotient.
13. Define two double variables with names as 'degrees' and 'radians'.
From the user read degrees. Convert the degrees to radians and store in the
'radians' variable. Finally print the degrees and radians.
14. Define a double variable with name as 'angle'. Read degrees from the user
into this 'angle'. Convert 'angle' into radians.
15. Read angle in degrees from the user into a double variable. Convert the
degrees to radians. Use math library function sin() and calculate the
sine of given angle. Print the degrees given, radians and its sign value.
16. Extend the above program to calculate the cos and tan values for the
given degrees by using math library functions cos() and tan().
17. Extend the above program to convert sin, cos and tan values back to
radians by using inverse functions of sin, cos and tan.
18. Read a double value from the user, calculate its square root by using
math library function sqrt(). Print the given number and its square root.
19. Write a program to use the math function pow(). Read base and power
values fron the user as double variables. Use pow() to calculate the
base raised by the power. Finally print the base, power and base raised
by power.
20. Read angle in degrees as double variable. Calculate sine and cos of
given angle. Compute square root of , sum of sin square and cos square.
For any angle this value should be 1.
21. Read the radius of a sphere from the user. Calculate the volume of the
sphere and print it. Use all variables of type 'double' only.
Formula for volume of sphere is : (4.0/3.0) * PI * r * r * r
22. Read the radius of a sphere from the user. Calculate the surface are
of the sphere and print it. Use all variables of type 'double' only.
Formula for surface area of sphere is : 4.0 * PI * r * r
23. Read the radius of sphere from the user. Ask user to enter the radius in
the units of centi meters only. Print the values of densities to the
user as given below:
Following are the densities of some metals:
Aluminum 2.712 grams/cm3 (grams per centimeter cube)
Copper 8.940
Gold 19.320
Iron 7.850
Silver 10.490
Next ask the user to enter the density of the metal of the sphere.
Finally print the weight of the sphere.
Formula for weight : volume * Density.
24. Following is the formula for the compound interest
A = P * ( (1 + (r/n)) ** (n*t) )
Note: ** means 'Power of'
P = Principal amount (the initial amount you borrow or deposit)
r = Annual rate of interest
t = Number of years the amount is deposited or borrowed for
A = Amount of money accumulated after n years, including interest
n = Number of times the interest is compounded per year
Write a program which reads P,r,n and t. Use all variables of type
double only. Compute the value of 'A' and print it.
Hint: Use 'power of' function pow()
25. Read a character into char variable and print this as small integer in
decimal and hex.
26. Read a small int into char variable and print this small int as character.
27. Read a character into char variable and print the previous character
and the following character.
28. Ask user to enter a capital letter. Read the capital letter into a char
variable. Set the 5th bit in this char variable to 1. Now print this char
as character.
29. Ask user to enter a small letter. Read the small letter into a char
variable. Clear the 5th bit in this char variable to 0. Now print this char
variable as character.
30. Read a short variable from the user as hex number. Print the value of
bit number 0 and bit number 9. Note that value of bit will be either zero
or one.
31. Read a short variable from the user as hex number. Clear the 5th bit to 0
and set the 12th bit to 1. After clearing and setting the bits, print the
value as hex number. Verify that the 5th bit is cleared to zero and 12th
bit is set to 1.
32. Read a short variable from the user as hex number. Also read the bit
position to set, from the user. Now set the specified bit in the hex number.
Finally print the number in hex, after setting the given bit.
33. Read a short variable from the user as hex number. Also read the bit
position to clear, from the user. Now clear the specified bit in the hex
number. Finally print the number in hex, after clearing the given bit.
34. Read a short variable from the user as hex number. Also read the
bit position, from the user. Print the value present in the 4 bits,
starting from the given bit position.
35. An electric pole is present on one side of the road. From the opposite
side of the road, the tip of the pole is making certain angle. Ask the
user to enter the angle in degrees and width of the road in meters.
Calculate the height of the pole and print it.
Formula: TAN theta = pole_height / road_width
Use math function tan().
36. Read the time from the user as hours, minutes and seconds. Convert
the time to total number of seconds.
37. Read the total number of seconds from the user. And display it as time
like hh:mm:ss.

