CS13002 Programming and Data Structures |
(Autumn semester) |
Practice Session - 1
Your first C program
The following program prints Hello world! and terminates.#include <stdio.h> main () { printf("Hello world!\n"); }Complete your edit-save-compile-debug-run cycle on this code.
Program - 2
Reading two numbers x and y, computing their sum and printing the sum./* This line is a comment */ /* A comment can be split across multiple lines like this. You must use comments to create a program header as shown below. *//* Roll Number: 04CS3022 Name: Rajat Sethi Assignment No: a5 */#include <stdio.h> main () { int x, y, sum ;printf("Enter x: "); scanf("%d", &x); printf("Enter y: "); scanf("%d", &y); sum = x + y ;printf("The sum of x and y is: %d \n", sum) ; }Type in this program, save it, compile it and run it. If you make syntax errors while typing in the program, the compiler will point out syntax errors. The compiler shows the line number of the error. Sometimes the actual error may lie in the previous line, but may be detected in the next line by the compiler. Always look at the error line indicated by the compiler and the previous line as well.
Program - 3
Modify Program-2 to read two numbers x and y and print the larger of the two.
#include <stdio.h> main () { int x, y, big ;printf("Enter x: "); scanf("%d", &x); printf("Enter y: "); scanf("%d", &y); if (x > y) { big = x ; } else { big = y ; }printf("The larger among x and y is: %d \n", big) ; }
Program - 4
Modify Program-3 to read two characters a and b and print the larger of the two (in alphabetical order). You may face some problems in reading two characters one after the other. We will explain the cause and the solution during the class.
Program - 5
Modify Program-2 to print the average of the two numbers x and y. What should be the data type of the average?
Program - 6
Modify Program-3 to create a program that reads three floating point numbers, x, y and z, and does the following:
(a) It prints the smallest among the three
(b) It checks whether one of the numbers is the average of the other two and prints a message identifying the pair
(c) It prints the average of the three numbers up to 2 decimal places