Module 1

Programming Basics

Home > Courses > CP Lab > Basics

Programs with line-by-line explanations

Prepared by: Dr. Tojo Mathew

Warning! You are warned against blindly copying the code statements. Read the explanations given for the code statements and understand the implementation. Then, try to do it yourself. Blindly re-typing the code statements is a waste of time & effort.

Table of contents

Note: Descriptions in brown font enclosed within  /*  and  */  or starting with // are comments for human readers  to understand the program better.  No need to type in comments in the actual program. Compilers ignore comments while generating executable code of the program. 

1.1 Hello World

Write a C program to print "Hello World!"

Sample Output:

Hello World!

Solution:


General description of program: This program, on execution, prints the message ‘Hello World!’ on the screen.

Return to top

1.2 Size of All Data Types

Write a C program to print the size of various data types in C.


Input Format:

There is no input.

Output Format:

Output should display the size of all data types(refer sample input and output).


Refer sample input and output for formatting specifications.

[All text in bold corresponds to input and the rest corresponds to output]


Sample Input and Output :

Size of int is 4

Size of float is 4

Size of double is 8

Size of char is 1


Solution:

General description of program: Size of a data type is the number of bytes allocated for a variable of that data type in the computer memory (RAM). Size of a data type determines how big numbers can be stored in a variable of that data type. More number of bytes allocated means larger magnitude numbers can be stored. The operator ‘sizeof’ can be used to get the size of a data type or variable. The size value returned by the ‘sizeof’ operator is always an integer since it refers to the number of bytes. Hence format string %d is used to display size value.

The program displays sizes of the four basic data types on screen. Since sizes of different data types depend on the compiler/system architecture, on different systems it may show different values.

Return to top

1.3 Simple and Compound Interest


Write a c program to find simple and compound interest.


Input format:


The first line of the input consists of a float value which corresponds to the principal value.

The second line of the input consists of float value which corresponds to the rate value.

The third line of the input consists of integer value which corresponds to the period in the year.


Output format:


The first line of the output consists of float value which corresponds to the simple interest.

The second line of the output consists of float value which corresponds to the compound interest.

Print the output value with two decimal places as given in the sample input and output.


Refer sample input and output for formatting specifications.

[All text in bold corresponds to input and the rest corresponds to output]


Sample Input and Output:

1500

5

5

Simple interest is 375.00

Compound interest is 414.42


Solution:

General description of program: The program takes three input values from the user namely principle amount (float type), rate of interest in percentage (float), duration/time (int) and computes simple interest and compound interest using the respective formulae. 

SI = (p*r*n)/100   and CI = p* (1+ r/100)n - p

CI is computed in the program in two steps. First (1+ r/100) is computed and stored in an intermediate variable q. Then CI is computed in the second step with help of pow() function from header file math.h. as CI = p*pow(q,n) - p. The two steps can be replaced with a single step as CI = p*pow(1 + (r/100),n) - p. 

Return to top

1.4 Celsius to Fahrenheit Converter


The relationship between Celsius (C) and Fahrenheit (F) degrees for measuring temperature is linear. Find an equation relating C and F if 0 C corresponds to 32 F and 100 C corresponds to 212 F.

Write a C program to simulate Celcius to Fahrenheit Converter.


Input Format:

Input consists of a single integer which corresponds to a measure of temperature in Celcius.

Output Format:

Refer Sample Input and Output for exact formatting specifications.

[All floating point values are displayed correct to 1 decimal place]


Sample Input and Output:

[All text in bold corresponds to input and the rest corresponds to output]

12

53.6F

Solution: 

Return to top

1.5 Area of Circle

Write a program to find the area of a circle. (Assume pi=3.14)


Input Format:

Input consists of float value which corresponds to radius of circle.


Output Format:

Output consists of the area of the circle in mtsq.



Sample Input:

5.5

Sample Output:

94.98


Solution:

Return to top

1.6 Area of triangle

Write a program to find the area of a triangle when the three sides of the triangle are given.


Input Format:

Input consists of three integer variables corresponding to three sides of the triangle.


Output Format:

The output consists of the area of the triangle.

Refer sample input and output for formatting specifications.

[All text in bold corresponds to the input and the rest corresponds to output]

Sample Input:

5

6

7


Sample Output:

14.70


Solution:

Return to top

1.7 Sum of Two Numbers

Write a program to find the sum of two numbers.


Input Format:

Input consists of 2 integers.


Output Format:

The output consists of an Integer indicating the sum of inputs.

Sample Input:

4

7

Sample output:

11

Solution: