0% found this document useful (0 votes)
18 views5 pages

HO2 Day2 Worksheet

Uploaded by

haydaycommonid02
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)
18 views5 pages

HO2 Day2 Worksheet

Uploaded by

haydaycommonid02
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/ 5

Spacing, escape characters and comments

1. Create a C program to print the following sentence.

This is my first exercise.

2. Modify the program to print the following.

This is my first
This is a new line.

3. Modify the program again to print the following (use a tab).

This is my first
This is an indented line.
This is a new line.

4. Create a C program to print your name, age, and address in the following format.
Name: <Your name>
Age: <Your age>
Addr.: <Your address (line 1),
(line 2),
(line 3),
(line 4)>

5. Create a C program to print the following sentence. Modify the program to make an alert
sound when displaying the message.
This is a message with ‘single quotes’ , “double quotes” and question mark (?).

6. Copy the file path of the folder in which you are doing these exercises and create a C program
to print it as follows.
My file path is : <Your file path>

1
The following code segments are erroneous. Please correct them and run the
programme.

7.

include <stdio.h>

int main() {
int 1_one = 25;
printf("%d" , 1_one);
return 0;
}

8.

include <stdio.h>

int main() {
int a = 25;
float a = 25;
printf("%d" , a);
return 0;
}

2
Variables

9. In C programming, declare a variable to store the radius of a circle as a float. Assign a value
to this variable and calculate the area of the circle. Display the radius and the area of the circle
on the screen.

10. Declare a variable in C to store the temperature in Celsius. Assign a value to this variable and
convert the temperature to Fahrenheit. Display both the Celsius and Fahrenheit temperatures
on the screen.

11. In C, declare two float variables 'a' and 'b' with initial values 11.2 and 2.2, respectively. Write
a program to display the results of adding, subtracting, multiplying, and dividing 'a' by 'b'.

12. In C, declare three variables to represent the dimensions of a rectangle: 'length', 'width', and
'area'. Write a program to calculate and display the area of the rectangle using the formula:
area = length * width. Ensure to output a clear message with the calculated area.

3
Declaring variables and Format specifiers

13. Write a C program to declare, initialize and display following variables.

number = 10
letter = a
pi = 3.14
value = 1.2345678900

14. Write a C program to declare, initialize and display the results of following operations where,
a = 10 and b = 15.

i) a + b
ii) a - b
iii) a * b
iv) a / b

15. Write a C program to increment value a = 10 by 1.

16. Write a C program to decrement value a = 10 by 5.

17. Write a C program to swap the values of two variables. (a = 10, b = 20)

18. The provided rectangular dimensions are a length of 7.342 cm and a width of 5.345 cm.

7.342 cm

5.345 cm

i) Write a C program to calculate the area of the rectangle. Display the output in the
format “The area of the rectangle is ……… cm^2”
(you should use appropriate Declaring variables and Format specifiers)

ii) Write a C program to calculate the perimeter of the rectangle. Display the output
in the format “The perimeter of the rectangle is ……… cm”
(you should use appropriate Declaring variables and Format specifiers)

iii) Modify the codes from (i) and (ii) above in another script to display their answers
with three decimal places.

4
Operators

19. For a given five-digit integer, print the sum of its digits. Eg- if the integer is 85314, the output
should be the sum of individual digits 8+5+3+1+4 = 21

20. What is the output of the following code? Provide a step-by-step evaluation of the code.

include <stdio.h>

int main() {
int n = 10;
n- = (n--) – (--n);
printf("%d" , n);
return 0;
}
21. what is the output of the following code? Provide a step-by-step evaluation of the code.
include <stdio.h>

int main() {
int a = 3,b,c;
b = 2*(a++);
c = 2*(++a);
printf("%d %d", b,c);
return 0;
}

Getting inputs with “scanf”

22. Write a program to get radius as a user input and calculate the area of the circle. Display the
radius and the area of the circle on the screen.

23. Write a program to get temperature in Celsius as a user input convert the temperature to
Fahrenheit. Display both the Celsius and Fahrenheit temperatures on the screen.

24. In C, declare three variables to represent the dimensions of a rectangle: 'length', 'width', and
'area'. Write a program to get values as user inputs and calculate and display the area of the
rectangle using the formula: area = length * width. Ensure to output a clear message with the
calculated area.

You might also like