0% found this document useful (0 votes)
103 views49 pages

The National Institute of Technology, Mysuru: Department of Computer Science and Engineering

Here are 3 sample test cases for finding the factorial of a number: Sample Input 1: Enter a number: 5 Sample Output 1: The factorial of 5 is 120 Sample Input 2: Enter a number: 3 Sample Output 2: The factorial of 3 is 6 Sample Input 3: Enter a number: 0 Sample Output 3: The factorial of 0 is 1 Solution: #include <stdio.h> int main() { int number, factorial = 1; printf("Enter a number: "); scanf("%d", &number); if(number < 0) printf("Factorial does not

Uploaded by

CC Hosamath
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)
103 views49 pages

The National Institute of Technology, Mysuru: Department of Computer Science and Engineering

Here are 3 sample test cases for finding the factorial of a number: Sample Input 1: Enter a number: 5 Sample Output 1: The factorial of 5 is 120 Sample Input 2: Enter a number: 3 Sample Output 2: The factorial of 3 is 6 Sample Input 3: Enter a number: 0 Sample Output 3: The factorial of 0 is 1 Solution: #include <stdio.h> int main() { int number, factorial = 1; printf("Enter a number: "); scanf("%d", &number); if(number < 0) printf("Factorial does not

Uploaded by

CC Hosamath
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/ 49

The National Institute of Technology, Mysuru

Department of Computer Science and Engineering

C Programming Lab (E-Box) Students’ Manual V1.1


2017-18

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

1. Demonstrate the ability to solve simple problems using C.


2. Understand the problem and apply appropriate branching statement.
3. Illustrate the usage of different decision making and looping statements.
4. Illustrate basic operations that can be performed on arrays, structures and strings.
5. Demonstrate the different categories of functions and use of pointers.

COs Contributing Modules


CO1 Programming Basics
CO2 Conditional Statements
CO3 Looping Construct I & II
CO4 1-D Array, 2-D Array, Structures, Strings
CO5 Arrays & Functions, Functions & Pointers

4
Module 1. Programming Basics
1.1 Hello World in C
Write a C program to print "Hello World!"
Sample Output:

Hello World!

Solution:

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 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:

5
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 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.

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:

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]

Sample Input and Output:


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

Solution:

1.5 Area of Circle

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

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:

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

Additional Sample TestCases


Sample Input and Output 1 :
3
4
5
6.00

8
Solution:

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

Additional Sample TestCases


Sample Input and Output 1 :
2
3
Sum: 5

Sample Input and Output 2 :


22
108
Sum: 130

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.

Refer sample input and output for formatting specifications.


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

SAMPLE INPUT OUTPUT 1 :


Enter the coefficients of a, b and c :
111
-0.50+0.87i and -0.50-0.87i

SAMPLE INPUT OUTPUT 2 :


Enter the coefficients of a, b and c :
372
-0.33 and -2.00

Additional Sample TestCases


Sample Input and Output 1 :
Enter the coefficients of a, b and c :

111

-0.50+0.87i and -0.50-0.87i

Sample Input and Output 2 :


Enter the coefficients of a, b and c :

372

-0.33 and -2.00

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

Refer sample input and output for formatting specifications.


12
[All text in bold corresponds to input and the rest corresponds to 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:

Module 3. Looping Constructs I


3.1 GCD and LCM
14
Write a C program to find the GCD and LCM of two integers and output the results along with the given integers.
Use Euclid‟s algorithm. (Using looping constructs)

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]

Sample Input and Output:


Enter two numbers
50
25
GCD = 25 and LCM = 50 of 50 and 25

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

Additional Sample TestCases


Sample Input and Output 1 :
5

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]

Sample Input and Output 1:


Enter an integer :
7
0112358
Sample Input and Output 2:
Enter an integer :
5
01123
Solution:

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”

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:
13
Prime
Sample Input and Output 2:
33
Not prime
Solution:

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 ...

Input and Output Format:

Input consists of a single integer that corresponds to n.


The outputconsists of n integers in the series, separated by a space. There is a trailing space.

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?

Input and Output Format:


Input consists of single integer, n which corresponds to number of rows.

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

Sample Input and Output1:


5
*
**
***
****
*****

Sample Input and Output 2:


3

*
**
***

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]

Sample Input and Output:


Enter the number elements in the array :
5
Enter the elements of the array :
9
6
7
4
5
Before sorting the array :
96745
After sorting the array :
45679
Solution:

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.

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:

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

Sample Input and Output 2:


Enter the number of elements :
5
Enter the elements :
6
2
3
7
1
Enter the key to be searched :
2
The key is not present in the array

Additional Sample TestCases


Sample Input and Output 1 :
Sample Input and Output 2 :

Enter the number of elements :


6
Enter the elements :
2
4
5
6
8
9
Enter the key to be searched :
1
The key is not present in the array

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]

Sample Input and Output:


Enter the size of first matrix :
2
2
First matrix :
8
9
6
3
Enter the size of Second matrix :
2
2
Second matrix :
1
4
5
6
First input matrix :
89
63
Second input matrix :
14
56
Resultant matrix :
9 13
11 9

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.

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:


Enter the number of Employees :
3
Enter the details of Employee :1
Enter the Name :
Bala
Enter the Job :
SoftwareEngineer
Enter the Salary :
30000
Enter the details of Employee :2
Enter the Name :
Jenifer
Enter the Job :
Teacher
Enter the Salary :
10000
Enter the details of Employee :3
Enter the Name :
Kavin
Enter the Job :
Police
Enter the Salary :
25000
Details of Employee :1
Name :Bala
Job :SoftwareEngineer
Salary :30000.00

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.

Sample Input and Output 1:


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

Enter the word


hannah
hannah is a palindrome

Sample Input and Output 2:


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

Enter the word


bina
bina is not a palindrome

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.

Sample Input and Output 1:


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

Enter the word


hannah
hannah is a palindrome

Sample Input and Output 2:


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

Enter the word


bina
bina is not a palindrome

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.

Refer sample input and output for formatting specifications.


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

Sample Input and Output 1:


Enter the number of elements :
5
Enter the elements :
6
3
1
7
1
Enter the key to be searched :
3
The key is present in the array.
At index 1

Sample Input and Output 2:


Enter the number of elements :
5
Enter the elements :
6
3
1
7
1
Enter the key to be searched :
2
37
The key is not present in the array.

Additional Sample TestCases


Sample Input and Output 1 :
Enter the number of elements :
7
Enter the elements :
3
6
2
4
12
25
64
Enter the key to be searched :
25
The key is present in the array.
At index 5
Sample Input and Output 2 :
Enter the number of elements :
5
Enter the elements :
2
4
6
8
10
Enter the key to be searched :
9
The key is not present in the array.

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.

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:


Enter the number elements in the array :
5
Enter the elements of the array :
9
6
5
8
3
Before sorting the array :
96583
After sorting the array :
98653

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.

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:


Enter the number of elements in the array
5
Enter the elements in the array
6
8
9
1
4
The Mean = 5.60, Variance = 8.24 and Standard deviation = 2.87

Solution:

42
43
9.4 Matrix Multiplication
Write a „C‟ Program to find the product of two matrices.

Input and Output Format :


Refer Sample Input and Output for further details.

Sample Input and Output 1 :

[ All text of bold corresponds to Input and the rest output]

Enter the size of the First matrix :


2
2
Enter the size of the Second matrix :
2
2
Enter the elements of First matrix
1
2
3
4
Enter the elements of Second matrix
5
6
7
8
The Matrix after Multiplication is
19 22
43 50

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.]

Sample Input and Output:


Enter the value of a
5
Enter the value of b
3
Before swapping
a=5b=3
After swapping
a=3b=5

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:

Module 11. File Handling


11.1 File IO
Write a C program to record the student details into the file. Get the student detailsNAME, USN,
BRANCH,SEMESTERfrom the user and write those information in a file called student.rec
Below is the format of the output file.
Name : aarshad
USN : 12345
Branch : IT
Semester : 54321
Input and Output Format:
Get the student detailsNAME, USN, BRANCH,SEMESTERfrom the user
[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output :


Enter name:
aarshad
48
Enter USN:
12345
Enter branch:
IT
Enter sementer:
54321

Solution:

49

You might also like