The National Institute of Technology, Mysuru: Department of Computer Science and Engineering
The National Institute of Technology, Mysuru: Department of Computer Science and Engineering
Ramya S
Shashank S
Tojo Mathew
1
Table of Contents
Course Outcomes & CO Mapping 4
Module 1. Programming Basics 5
1.1 Hello World in C 5
1.2 Size of all data types 5
1.3 Simple and Compound Interest 6
1.4 Celsius to Fahrenheit Converter 7
1.5 Area of Circle 7
1.6 Area of triangle 8
1.7 Sum of two numbers 9
Module 2. Conditional Statements 11
2.1 Roots of a quadratic equation 11
2.2 Simple Calculator 12
Module 3. Looping Constructs I 14
3.1 GCD and LCM 14
3.2 Palindrome 16
3.3 Factorial 17
3.4 Fibonacci Numbers 18
Module 4. Looping Constructs II 19
4.1 Prime 19
4.2 Prime Series 20
4.3 Star Pattern 21
4.4 Multiplication Table 22
Module 5. One-D Array 23
5.1 Bubble Sort 23
5.2 Binary Search 24
Module 6. Two-D Array 27
6.1 Matrix Addition 27
Module 7. Structures 31
7.1 Employee Details(Structure) 31
Module 8. Strings 33
8.1 ASTROLOGY(Without Library functions) 33
8.2 ASTROLOGY(Using library functions) 35
Module 9. Arrays & Functions 37
9.1 Linear Search 37
9.2 Sorting in Descending Order 39
2
9.3 Mean, Variance & Standard Deviation 42
9.4 Matrix Multiplication 44
Module 10. Functions and Pointers 46
10.1 Swapping two numbers 46
10.2 Sum of array elements 47
Module 11. File Handling 48
11.1 File IO 48
3
Course Outcomes & CO Mapping
On Successful completion of this course, the students will be able to
4
Module 1. Programming Basics
1.1 Hello World in C
Write a C program to print "Hello World!"
Sample Output:
Hello World!
Solution:
Input Format:
There is no input.
Output Format:
Output should display size of all data types(refer sample input and output).
Solution:
5
1.3 Simple and Compound Interest
Input format:
The first line of the input consists of 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 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.
Solution:
6
1.4 Celsius to Fahrenheit Converter
The relatioship between Celcius (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]
Solution:
Input Format:
Input consists of float value which corressponds to radius of circle.
Output Format:
Output consist of area of circle in mtsq.
Sample Input:
5.5
Sample Output:
94.98
7
Solution:
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
8
Solution:
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:
9
10
Module 2. Conditional Statements
2.1 Roots of a quadratic equation
Write a C program to find and output all the roots of a given quadratic equation, for non-zero
coefficients. (Using if…else statement)
Input Format:
Input consists of 3 integers corresponding to the coefficients of the Quadratic equation ax^2 + bx +
c
Output Format:
The output consists of the roots of the quadratic equation.
111
372
Solution:
11
2.2 Simple Calculator
Write a C program to simulate a simple calculator that performs arithmetic operations like addition,
subtraction, multiplication, and division only on integers. Error message should be reported, if any
attempt is made to divide by zero. (Using switch statement)
Input Format:
The first input consists of an integer which corresponds to the first operand, second input consists of
an operator(+,-,*,/,%) and the third input consists of an integer which corresponds to the second
operand.
Output Format:
Refer Sample Output
Sample Input 1:
10
+
5
Sample Output 1:
The sum is 15
Sample Input 2:
10
-
5
Sample Output 2:
The difference is 5
Sample Input 3:
10
*
5
Sample Output 3:
The product is 50
Sample Input 4:
10
/
5
Sample Output 4:
The quotient is 2
Sample Input 5:
10
/
0
Sample Output 5:
Divide by Zero error!!!
Sample Input 6:
10
%
5
Sample Output 6:
The remainder is 0
13
Sample Input 7:
10
=
5
Sample Output 7:
Invalid Input
Solution:
Input Format:
The first and the second input are two integers.
Output Format:
Output consists of GCD and LCM of the 2 input numbers.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]
Solution:
15
3.2 Palindrome
Write a C program to reverse a given integer number and check whether it is a palindrome or not. Output the
given number with suitable message. (Using While statement)
Input Format:
The first input consists of an integer.
Output Format:
Output consists of a statement in which we print whether the input is palindrome or not.
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 1:
Enter an integer :
15651
15651 is a palindrome.
Sample Input and Output 2:
Enter an integer :
100
100 is not a palindrome.
Solution:
16
3.3 Factorial
Write a program to find the factorial of a given number(Using While loop).
Input Format:
Input consists of a single integer, n.
Output Format:
The output consists of a single integer which corresponds to n!
Sample Input :
4
Sample Output :
24
120
Solution:
17
3.4 Fibonacci Numbers
Write a C program to generate and print first „N‟ Fibonacci numbers. (Using do-While statement)
Input Format:
The first input consists of an integer.
Output Format:
Output consists of series of n fibinacci numbers.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]
18
Module 4. Looping Constructs II
4.1 Prime
Write a program to find whether a given number is prime or not.
Input Format:
Input consists of a single integer.
Output Format:
Output should display whether the input is “Prime” or “Not prime”
19
4.2 Prime Series
Write a program to print the following series
2 3 5 7 11 13 17 19 23 29 31 37 41 ...
Sample Input:
5
Sample Output:
2 3 5 7 11
Solution:
20
4.3 Star Pattern
Write a program to help Patrick to print the pattern given pattern using while loop?
[All text in bold corresponds to input and the rest corresponds to output]
*
**
***
Solution:
21
4.4 Multiplication Table
Write a program to print Multiplication tables till 10 for given range "N" starting from 1 (Using
Nested for loop statement).
Input Format:
Input consists of an integer (0<N<20).
Output Format :
The output consists of tables from 1 to N as shown in sample input-output format.
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
Sample Output:
12345
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
6 12 18 24 30
7 14 21 28 35
8 16 24 32 40
9 18 27 36 45
10 20 30 40 50
Solution:
22
Module 5. One-D Array
5.1 Bubble Sort
Write a C program to input N integer numbers into a single dimension array. Sort them in ascending order using
bubble sort technique. Print both the given array and the sorted array with suitable headings.
Input Format:
The first input consists of an integer which corresponds to the number of elements present in the single dimension
array.
The next n inputs are the elements in the array.
Output Format:
First line output consists of array elements before sorting and the next line of the outputconsists of array elements
after sorting.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]
23
5.2 Binary Search
Write a C program to input N real numbers in ascending order into a single dimension array. Conduct a binary
search for a given key integer number and report success or failure in the form of a suitable message.
Input Format:
The first input consists of an integer which corresponds to the number of elements present in the single dimension
array.
The next n inputs are the elements in the array.
The next inputconsists of an integer which corresponds to the key element to be searched.
Output Format:
Output should display whether the key is present in the array or not.
24
Enter the number of elements :
5
Enter the elements :
6
3
1
7
1
Enter the key to be searched :
1
The key is present in the array
Atindex2
Solution:
25
26
Module 6. Two-D Array
6.1 Matrix Addition
Write a C program to read two matrices A(M x N) and B(P x Q) and perform addition. Output the input matrices
and the resultant matrix with suitable headings and format. (Using two dimension arrays where array size M, N,
P,Q ≤ 4)
Input Format:
The first input consists of an integer which corresponds to the number of elements present in the two dimension
array.
The next n inputs are the elements of the first matrix and then followed byelements of the second matrix.
Output Format:
The output consist of the first matrix and then the second matrix and then the resultant matrix which corresponds
to the addition of two matrix.
Print "Matrix addition is not possible" if the size(order) of two matrixis not same.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]
27
Additional Sample TestCases
Sample Input and Output 1 :
Enter the size of First matrix :
2
3
First matrix :
1
2
3
4
5
6
Enter the size of Second matrix :
3
2
Second matrix :
1
2
3
4
5
6
Matrix Addition not possible
Sample Input and Output 2 :
Enter the size of First matrix :
2
2
First matrix :
8
9
6
3
Enter the size of Second matrix :
2
2
1
Second matrix :
4
5
6
First input matrix :
89
63
Second input matrix :
14
56
Resultant matrix :
9 13
11 9
Solution:
28
29
30
Module 7. Structures
7.1 Employee Details(Structure)
Write a C program to create a structure called Employee with members Name, Job, Salary. Create a structure
variable. Accept the input values for the structure members at run time. Suitably display the same.
Input Format:
The first input is an integer which corresponds to the number of employees.
Secondinput is a charecter array which corresponds to the name of the employee.
Third input is a charecter array which corresponds to the job of the employee.
Fourth input is a float which corresponds to the salary of the employee.
Output Format
Output should display the employee details which is given in the sample input and output format.
31
Details of Employee :2
Name :Jenifer
Job :Teacher
Salary :10000.00
Details of Employee :3
Name :Kavin
Job :Police
Salary :25000.00
Solution:
32
Module 8. Strings
8.1 ASTROLOGY(Without Library functions)
Whole India is tweeting about the probable names for Aishwarya Rai's daughter. Some astrologers are suggesting
that she will grow up to be a more famous celebrity than Aishwarya if her name is a palindrome. As we all know,
a palindrome is a word that can be read the same way in either direction.
Write a C program to determine whether a given word is a palindrome or not. Do not use any string library
functions.
Input Format:
Input consists of a single string. Assume that the maximum length of the string is 50.
Output Format:
Refer sample input and output for formatting specifications.
Solution:
33
34
8.2 ASTROLOGY(Using library functions)
Whole India is tweeting about the probable names for Aishwarya Rai's daughter. Some astrologers are suggesting
that she will grow up to be a more famous celebrity than Aishwarya if her name is a palindrome. As we all know,
a palindrome is a word that can be read the same way in either direction.
Write a C program to determine whether a given word is a palindrome or not. Use String built-in function.
Input Format:
Input consists of a single string. Assume that the maximum length of the string is 50.
Output Format:
Refer sample input and output for formatting specifications.
Solution:
35
36
Module 9. Arrays & Functions
9.1 Linear Search
Write C program for user-defined functions:
1. To input N integer numbers into a single dimension array.
2. To conduct a linear search.
Using these functions, write a C program to accept the N integer numbers & given key integer number and
conduct a linear search. Report success or failure in the form of a suitable message.
Function Specification:
void search(int a[ ],int b,int n)
This function will search whether the element 'b' is present in anarray "a" of size n. Andprint "The key is present
in the array."if foundelseprint"The key is not present in the array.".
Input Format:
The first input consists of an integer which corresponds to the number of elements present in the single dimension
array.
The next n inputs are the elements in the array.
The next input consistsof an integer which corresponds to the key element to be searched.
Output Format:
The output should display whetherthe key is present in the array or not.
Solution:
38
9.2 Sorting in Descending Order
Write C program for user defined functions:
To input N integer numbers into a single dimension array.
To sort the integer numbers in descending order using selection sort technique.
To print the single dimension array elements.
Using these functions, write a C program to input N integer numbers into a single dimension array, sort them in
descending order, and print both the given array & the sorted array with suitable headings.
Input Format:
The first input consists of an integer which corresponds to the number of elements present in the single dimension
array.
The next n inputs are the elements in the array.
Output Format:
39
First line output consists of array elements before sorting and the next line of the output consists of array elements
after sorting the elements in descending order.
Solution:
40
41
9.3 Mean, Variance & Standard Deviation
Write C program for user defined functions:
To input N real numbers into a single dimension array.
Compute their mean.
Compute their variance
Compute their standard deviation.
Using these functions, write a C program to input N real numbers into a single dimension array, and
compute their mean, variance & standard deviation. Output the computed results with suitable
headings.
Input Format:
The first input consists of an integer which corresponds to the number of elements present in the
single dimension array.
The next n inputs are the elements in the array.
Output Format:
First line output consists of 3 float values which corresponds to the mean, variance & standard
deviation values.
Solution:
42
43
9.4 Matrix Multiplication
Write a „C‟ Program to find the product of two matrices.
Solution:
44
45
Module 10. Functions and Pointers
10.1 Swapping two numbers
Mrs. Anitha , our favourite Maths teacher wanted to teach her students to swap two elements.
Write a program to accept 2 integers and to swap them using functions and pointers.
Function Specification:
void swap(int *a,int *b)
This functions swaps 2 integers.
Input Format:
The input consists of 2 integer.
Output Format:
Refer to the sample input and output for formatting details.
[All text in bold corresponds to input and the rest corresponds to output.]
Function Definitions:
46
10.2 Sum of array elements
Write a program to find the sum of the elements in an array.
Function Specification:
int sum(int *arr,int n)
This functionadds all the elements in an array. And returns the added Sum.
Input Format:
Input consists of n+1 integers. The first integer corresponds to „n‟,the size of the array. The next „n‟
integers correspond to the elements in the array. Assume that the maximum value of n is 15.
Output Format:
Refer sampleoutput for better understanding.
Sample Input 1:
5
2
3
6
47
8
1
Sample Output 1:
The sum of the elements in the array is 20
Solution:
Solution:
49