Assignments for practicing C on a Linux System
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.

Assignments on If and Switch statements
1. Read an integer number from the user and print whether the number is
even number or odd number. Check the first bit (0th bit), if first bit
is one, the number is odd, else number is even
2. Read an integer number from the user and print whether the number is
even number or odd number. Find the remainder value when the given
number is divided by 2. If remainder is zero, the number is even.
Else number is odd.
3. Read an integer number from the user and print whether the number is
negative or positive. Find the number as negative or positive by checking
the most significant bit. That is bit 31. If bit 31 is one, it is
negative number. Else positive number.
4. Read a character from the user, and print whether the character is
Capital letter / Small letter / Numeric digit / Other.
5. Read a character from the user. If character is capital letter, convert
it to small letter and print it. If character is small letter, convert it
to capital letter. If character is nither capital nor small letter, print
as it is.
6. Write a program to perform divisibility test. Read two numbers from the
user. Test if first number is divisible by second number without any
remainder. Based on the resule, print "Divisible Test Passed" or
"Divisible test failed".
7. Read two integer numbers from the user. Print the bigger number. If both
numbers are equal, print "Both numbers are equal\n".
8. Read three integer numbers from the user. If these three numbers are
in asending order, print "Asending". If the numbers are in decending order
print "Decending". If all three numbers are equal print "Equal". If none
of the above are true, print "Random".
9. Read the radius of a sphere in centimeters from the user. Ask the user
to enter the metal type of sphere as below:
printf("1:Aluminum 2:Copper 3:Gold 4:Iron 5:Silver\n");
Read the metal type as number 1 to 5. If user enters invalid metal number
print error and exit from the program.
Calculate and print the weight of sphere by using following densities of
metals:
Aluminum 2.712 grams/cm3 (grams per centimeter cube)
Copper 8.940
Gold 19.320
Iron 7.850
Silver 10.490
10. Read two integer numbers from the user. Ask the user to enter any
arithmetic operator character. User can enter any one of the following
arithmetic operator character:
+  * / %
Based on the character do the corresponding operation on the numbers and
print the result. Use IfelseIfelseIf statement.
11. Redo the above program by using switch() statement.
12. Read month from the user as integer. User should enter 1 for the month
January, 2 for February and so on. The program should print the number
days in the given month. Use switch() statement. Assume that year is
not a leaf year.
13. Redo the above program. Now read both month and year from the user.
Print the number of days in the given month. Assume that year is leaf
year if remainder is zero when devided by 4.

Assignments on Arrays and Loops
1. Define an array of 5 int variables. Ask user to enter 5 numbers.
Read five numbers into this array of five. Finally print these 5 numbers.
2. Read five numbers from the user into an array of 5. Calculate the smallest
number, largest number, total and average. Finally print these calculated
values.
3. Define an array of 6 char variables. Ask the user to enter the marks of
6 subjects. Read these marks into the array. As each mark is from 0 to 100,
it can be stored in 8 bit integer, that is char variable.
Print whether the student is passed or failed. Take pass mark as 35.
4. Read a a line of string from the user. Find the length of the string and
print.
5. Read a line from the user and print the number of capital letters present
in the string.
6. Read a line from the user and print the number of spaces present in the
line.
7. Read a line and print the number of words present in the line string.
Note that one or more spaces could be present between the wokds.
8. Read a line from the user, and convert each capital letter to small letter
and small letter to capital. All the other characters should remain as
it is. Finally print the modified line string.
9. Define two strings, each of size 80 chars. Read a line string into
first string. Copy this first string into the second one. Print the
second string.
10. Define a string of size 80 chars. Also define 5 strings of size 20 chars
each. Ask the user to enter a line containg 5 words. Copy each word
into individual strings. Finally print all the inidividual string.

Assignments on defining and using structure variables
1. Define a structure variable of type 'struct Time' as given below:
struct Time
{
char hour;
char min;
char sec;
};
Note that the structure fields hour, min and sec are char (small) integers.
Small integers are sufficient to store the time values.
Ask the user to enter hours, minutes and seconds. Read those values into
the structure. Finally print the time structure with the format 'HH:MM:SS'.
2. Define a structure variable of type 'struct Date' as given below:
struct Date
{
char date;
char month;
short year;
};
Ask the user to enter date and read into the structure variable.
Finally print the values in the structure variable in the format
'DD/MM/YYYY'. Again print the date in the format 'DD MMM YYY', where
MMM is the name of the month in three characters like 'Jan' .. 'Dec'.
3. Define a structure variable of type 'struct Student' as given below:
struct Student
{
char name[40];
long long mobile;
struct Date dob; //Date of birth, struct Date as given in assignment 2.
};
Ask the user to enter the student information, and fill the defined
structure variable. Finally print the studnent informaiton in the
following format:
Name : Bharat Kumar B
Mobile : 9393916923
DOB : 18/10/1994
Note that, name may have multiple words.
4. Define an array of 5 structure variables of type 'struct Student'
Read from the user, information of 5 students in to this array.
Finally print the array in the following format:
S.No Name Mobile Date of Birth
   
1 Bharat Kumar B 9393916923 18/10/1994
   
5 xxxxx xxxxxx x xxxxxxxxxx xx/xx/xxxx

Assignments on defining simple functions and calling them from main()
1. Write the following function:
double getSphereVolume(double radius);
This function takes the radius of sphere as parameter and returns the
volume of sphere. Write a main() function to test this function. To test
this function, in main() function read radius from the user and call this
function. Finally print the value returned by this function.
Note that, inside the getSphereVolume() function, you should not print the
volume. This function should calculate and return the volume. Printing of
volume should be done only from the main() function.
2. Write the following function:
double getSphereSurfaceArea(double radius);
Write the main() function to call and test the above function.
3. Write the following function:
double getSphereWeight(double radius, double density);
Write the main() function to call and test the above function.
4. Write the following function. This function sets the bit at the given
position to one.
unsigned int setBit(unsigned int val , int bitPos);
Write the main() function to call and test the above function.
In the main() function read an hexadecimal number from the user and also
read bit position from the user. Call the above function. Print the value
returned by that function in hexadecimal.
Note that reading hex number and printing hex number allows us to check if
the bit is set correctly or not.
5. Write the following function. This function clears the bit at the given
position to zero.
unsigned int clearBit(unsigned int val , int bitPos);
Write the main() function to call and test the above function.
In the main() function read an hexadecimal number from the user and also
read bit position from the user. Call the above function. Print the value
returned by that function in hex.
Note that reading hex number and printing hex number allows us to check if
the bit is cleared correctly or not.
6. Write the following function. This function returns total number of seconds
elapsed at the given time
int getTotalSecondsElapsed(short hour, short min, short sec);
Write the main() function to call and test the above function.
7. Write the following function. This function is same as function written in
the above assignment. But takes structure Time as parameter.
Struct Time is specified in the assignments on structures.
int getTotalSecondsElapsed(struct Time t);
Write the main() function to call and test the above function.
8. Write the following function. This function is reverse of previous
function. It takes total seconds as input and returns struct time.
struct Time getTotalSecondsElapsed(int totalSeconds);
Write the main() function to call and test the above function.

