0% found this document useful (0 votes)
5 views82 pages

C Programming Lab

Uploaded by

saleemaafsar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views82 pages

C Programming Lab

Uploaded by

saleemaafsar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 82

16BECS212 COMPUTER PROGRAMMING LAB 0032

OBJECTIVES:

 To provide an awareness to Computing and C Programming 


 To know the correct and efficient ways of solving problems 
  To learn to develop algorithm for simple problem solving 
 To learn to program in C 

LIST OF EXERCISES
1. Write a C program to find Factorial of a given number using do while loop.
2. Write a C Program to print Fibonacci series using while loop.
3. Write a C Program to check a given number is Prime or Not.
4. Write a C Program to compute the sum of even numbers for a given n value.
5. Write a C Program to check the given string is Palindrome or Not.
6. Write a C Program to check the given number is Armstrong or Not using functions.
7. Write a C Program to count the number of vowels from the given string using switch case.
8. Write a C Program to read a line of text from keyboard and print the number of characters, words
and spaces.
9. Write a C Program to print the student‟s record using structure.
10. Write a C Program to find factorial of a number using recursion function.

4
Instructions to the students

The following instructions must be followed by the students in their laboratory classes.

1) Students are expected to be punctual to the lab classes. If they are late, they will be considered
absent for that particular session.

2) Students should strictly maintain the dress code.

3) Students must bring their Workbook, record note (completed with previous experiment) to
every lab class without fail.

4) Students are advised to come with full preparation for their lab sessions by

Reading the detailed procedure of the exercise from the laboratory manual.

Completion of Workbook (i.e.) Aim, Procedure, Algorithm, Program should be written


and Flowchart should be drawn in the observation note before entering into the
laboratory.

5) Students must use pen for writing and pencil to draw Flowchart in Work book.

6) Students must get attestations immediately for their output/execution.

7) Students are advised to enter their results evaluated in the Work book on the same day of that
exercise.

8) Assessment marks for each exercise is based only on their performance in the laboratory.

9) Record note has to be completed then and there and get corrected when the students are coming
for the next lab class.

10) Students must strictly maintain silence during lab classes.

11) If any of the students is absent for the lab class for genuine reasons, he/she will be permitted to
do the exercise during the repetition class only.

12) If any student is found causing damage to the lab equipments, he/she shall replace the same
with a new.

5
List of Experiments

1. Write a C program to find Factorial of a given number using do while loop.

2. Write a C Program to print Fibonacci series using while loop.

3. Write a C Program to check a given number is Prime or Not.

4. Write a C Program to compute the sum of even numbers for a given n value.

5. Write a C Program to check the given string is Palindrome or Not.

6. Write a C Program to check the given number is Armstrong or Not using functions.

7. Write a C Program to count the number of vowels from the given string using switch case.

8. Write a C Program to read a line of text from keyboard and print the number of characters,
words and spaces.

9. Write a C Program to print the student‟s record using structure.

10. Write a C Program to find factorial of a number using recursion function.

6
GENERAL PROCEDURE

C PROGRAMS:

1. Click “New” in toolbar.


2. Type the program according in C language.
3. Save the program with proper extension (Use .c for C programs)
4. Click “Build” in toolbar. It should say “Compilation finished successfully”, otherwise check
your code.
5. Click “Execute” in toolbar. It should popup a console window and you should see
“OUTPUT!” displayed.

7
Signatur
S.No Date Name of the Experiment Marks e

1 Write a C program to find Factorial of a given


number using do while loop

2 Write a C Program to print Fibonacci series


using while loop

3 Write a C Program to check a given number is


Prime or Not

4 Write a C Program to compute the sum of even


numbers for a given n value

5 Write a C Program to check the given string is


Palindrome or Not

6 Write a C Program to check the given number is


Armstrong or Not using functions

7 Write a C Program to count the number of


vowels from the given string using switch case

8 Write a C Program to read a line of text from


keyboard and print the number of characters,
words and spaces

9 Write a C Program to print the student‟s record


using structure

10 Write a C Program to find factorial of a number


using recursion function
Total

Average

8
Ex.No.1
To Find Factorial of a given number
Date:

Aim:

This task is to find factorial of n numbers. The factorial of a non-negative integer n, denoted by
n!, is the product of all positive integers less than or equal to n. For example, the value of 0! is 1,
according to the convention for an empty product.
Definition:

The factorial of a non-negative integer n, denoted by n!, is the product of all positive
integers less than or equal to n.
n!=nx(n-1)x(n-2)x(n-3)x…3x2x1

For example, 5! = 5*4*3*2*1=120


TASK:
1. To find the factorial of n numbers.
2. Identifiy the number of variables to be used.
3. Identify the data type of each variable.

INPUT FORMAT:

Input consists of a single integer n, where 1<=n<=100.

OUTPUT FORMAT:

Print the factorial of n.

SPACE FOR PROGRAM:


9
SAMPLE INPUT: 3 4 5 6
SAMPLE OUTPUT: 6 24 120 720

10
Practice Programs

1. Write to program to print the pyramid pattern formed of stars

Sample Input: n = 6

Output:
*
**
***
****
*****
******
*****
****
***
**
*
The idea is to use two for loops for every part of the pyramid. The two parts may be classified

as upper part and lower part


11
2. Given an integer N, print an inverted isosceles triangle of stars such that the height of
the triangle is N.

Input:
The first line of the input contains an integer T denoting the number of test cases. Then T test
cases follow. Each test case consists of a single line containing an integer N denoting the height
of the inverted isosceles triangle.
Output:

Corresponding to each test case, print the inverted triangle of height N in a single line such that all
the lines/rows of the triangle are placed side by side taking into consideration the spaces.

Constraints:
1 <= T <= 100
1 <= N <= 100
Example:
Input:
2
4
3
Output:
******* ***** *** *
***** *** *
Explanation:
For the 1st test case where N = 4
*******
*****
***
*

The above is the proper inverted isosceles triangle for the test case, but when printed in a
single line it becomes as shown in the output. Please mind there are 3 spaces after the single *
in the last row which has to be printed in single line also.
3. Sum of all prime numbers between 1 and N.

Given a positive integer N, calculate the sum of all prime numbers between 1 and N.

Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N.

Output:
Print the sum of all prime numbers between 1 and N.

Constraints:
1 ≤ T ≤ 50
1 ≤ N ≤ 1000000

12
Example:
Input:
2
5
10

Output:
10
17

13
4. For a given number N. Print the pattern in the following form: 1 121 12321 1234321 for N=4.

Input:
First line of input contains the test cases T. Each line of test case contain the value of N for
which the pattern is to be displayed.

Output:
For each value of N, print the pattern in a single line.

Constraints:
1<= T <=10
1<= N <= 20

Example:
Input:
2
3
6
Output:
1 121 12321
1 121 12321 1234321 123454321 12345654321

Result:
14
Ex.No.2
To Find Fibonacci series of a given number
Date:
Aim:
To write a C program to generate the Fibonacci Series
The Fibonacci sequence begins with 0 and 1. These are the first and second terms, respectively.
After this, every element is the sum of the preceding elements:
Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)
Task
Start counting from Fibonacci (1) =0. This might differ from some other notations that
treats Fibonacci (0) =0.
The overall equation is:
= 0, n = 1
Fibonacci(n) = 1 , n = 2
Fibonacci(n-1) + Fibonacci(n-2) , n > 2
Input Format
One line of input, the integer N.
Constraints
0<N<=40
Output Format
th
Output one integer, the N Fibonacci number.
Function Prototype
The code for accepting the input and displaying the output is provided. You will be provided the
th
input parameter N, and you need to return the Fibonacci N term.
Sample Input and Output Values for the Fibonacci Series
fibonacci(3) = (0+1) = 1
fibonacci(4) = (1+1) = 2
fibonacci(5) = (1+2) = 3
Program Space:
15
Sample Input
3
4
Sample Output
The Fibonacci series for 3 is: 0,1,1
The Fibonacci series for 4 is: 0,1,1,2

16
Practice Programs:
1. Given a sample space S consisting of all perfect squares starting from 1, 4, 9 and so
on. You are given a number N, you have to print the number of integers less than N in
the sample space S.

Input :
The first line contains an integer T, denoting the number of test cases. Then T test cases follow.
The first line of each test case contains an integer N, denoting the number.
Output :
Print the answer of each test case in a new line.
Constraints :
1<=T<=100
1<=N<=10^18

Example
Input :
2
9
3

Output :
2
1
17
2. Given a number N. Your task is to print the no of divisors of number N which are perfect
squares.

Input :
The first line of input contains an integer T, denoting number of test cases.Then T test cases
follow. The first line of each test case contains an integer N.

Output :
For each test case in a new line print the answer .
Constraints :
1<=T<=10
1<=N<=10^12

Example:
Input :
2
36
60
Output :
4
2

18
3. Given a number N, Your task is to find the length of the longest consecutive 1's in its binary
representation.

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases
follow. Each test case contains an integer N.

Output:
For each test case in a new line print the length of the longest consecutive 1's in N's
binary representation.

Constraints:
1<=T<100
1<=N<=1000

Example:
Input
2
14
222
Output
3
4
19
4. Given a number N, print all its unique prime factors in increasing order.

The prime factors of a positive integer are the prime numbers that divide that integer exactly.
Input : N = 100
Output: 2x2x5x5
Input : N = 35
Output: 5x7
Input : N = 360
Output: 2x2x2x3x3x5

Result:

20
Ex.No.3
To Find given number is Prime or Not
Date:

Aim:
Write a C Program to Find a given number is prime or Not

Input Format

First line of input consists of T , number of test cases.


For each test case, input consists of a number n .

Constraints
T <= 1000
2 <= n <= 1000000

Output Format
For each test case print Yes if the number is prime or No if the number is not prime.

Program Space:
21
Sample Input
3
2
4
5
Sample Output
Yes
No
Yes

Practice Problems:

1. Given a positive integer N, print count of set bits in it.


For example, if the given number is 6, output should be 2 as there are two set bits in it.
a) If the given number is 6 the binary number is 110, so we have two set bits, the output will
be 2
b) If the given number is 11 the binary number is 1011, so we have three set bits, the output
will be 3
(Hint set bit means 1)
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases
follow. The next T lines will contain an integer N.

Output:
Corresponding to each test case, in a new line, print count of set bits in it.

Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 1000000

Example:
Input:
2
6
11

Output:
1
2
3
22
23
2. Given a value N, you need to find how many numbers less than or equal to N
have numbers of divisors exactly equal to 3.
Input :
The first line contains integer T, denoting number of test cases. Then T test cases follow.
The only line of each test case contains an integer N.

Output :
Print in a new line the answer of each test case .

Constraints :
1<=T<=10^5
1<=N<=10^9

Example:
Input :
3
6
10
30

Output :
1
2
3

Result:
24
Ex.No.4
To Find sum of even numbers
Date:

Aim:
To Write a C Program to Find the Sum of Even Numbers in a set of numbers in an Array
Given an array of N integers,
Input Format
The first line contains an integer, N, denoting the size of the array.
The second line contains N space-separated integers representing the array's elements.
Output Format
Print the sum of the array's even numbers as a single integer.
Explanation
Let A =[1,2,3,4,5,6,7,8,9,10]
We print the sum of the array's even number, which is: 2+4+6+8+10=30
Program Space:

Sample Input
6
2 4 6 8 10 12
Sample Output
42
25
Practice Problems:

1. Given a positive integer N, print sum of first and last digit of any number
For example, if the given number is 1234, output should be 5 ie., 1+4, 1 is the first digit and 4
is the last digit.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases
follow. The next T lines will contain an integer N.

Output:
Corresponding to each test case, in a new line, add set bits in it.

Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 1000000

Example:
Input:
135
672
871

Output:
6
8
9
26
2. Given a value N, you need to find product of its digits using loop of your choice..

Explanation:

Input number: 1234


Output product of digits: 1 * 2 * 3 * 4 = 24

Input :
The first line contains integer T, denoting number of test cases. Then T test cases follow.
The only line of each test case contains an integer N.

Output :
Print in a new line the answer of each test case .

Constraints :
1<=T<=10000
1<=N<=10000

Example:
Input :
1234
445
871

Output :
24
80
56

Result:
27
Ex.No.5
To Check given String is Palindrome or not
Date:

Aim:
This task is to check given string is palindrome or not.
Palindrome is a number, word, phrase, or sequence that reads the same backward as forward,
e.g., "madam" or "malayalam" or 1221 or 10301.
TASK:
1. To print the given string is palindrome or not palindrome.
2. Use various string handling functions.

INPUT FORMAT:
Input consists of a sequence of characters n, where 1<=n<=100.

OUTPUT FORMAT:
Print the string is palindrome or not palindrome

SPACE FOR PROGRAM:

SAMPLE INPUT: MADAM, MALAYALAM


SAMPLE OUTPUT: Palindrome , Not Palindrome
28
Practice Programs
1. Design and develop an algorithm to find the reverse of an integer number NUM and
check whether it is PALINDROME or NOT.

Output:
Enter a
number: 5642
The Number is not Palindrome

Enter a
number: 1221
The Number is Palindrome

This program uses loop to find the least significant digit, separate least significant digit
and to reverse the given number. Once the number is reversed the, reversed number is compared
with original number to check it is palindrome or not.

29
2. Write a C Program to Check if a String is a Palindrome without using the Built-in
Function.
Output:
Enter a String:
Ankit
The Number is not Palindrome

Enter a
number: wow
The Number is Palindrome

Copy the entered string into a new string, and then we reverse the new string and then
compare it with original string. If both of them have same sequence of characters i.e. they are
identical then the entered string is a palindrome otherwise not.

Result:

30
Ex.No.6
To check the given number is Armstrong or Not using functions
Date:

Aim:
Write a C Program to check the given number is Armstrong or not using functions.
Definition :
Those numbers which sum of its digits to power of number of its digits is equal to that
number are known as armstrong numbers.
Example 1: 153 Total
digits in 153 is 3
And 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

Input Format

First line of input consists of T , number of test cases.


For each test case, input consists of a number n .

Constraints
T <= 1000
1 <= n <= 1000000

Output Format
For each test case print Yes if the number is Armstrong or No if the number is not armstrong.

Program Space:
31
Sample Input
0
1
122
370
371
Sample Output
Yes
Yes
No
Yes
Yes
32
Practice Problems:

1. Write a C program to print Armstrong numbers from 1 to 500.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases
follow. The next T lines will contain an integer N.

Output:
Corresponding to each test case, in a new line, print all the Armstrong numbers.

Constraints:
1 ≤ T ≤ 500
1 ≤ N ≤ 1000000

Example:
Input:
2
6
11

Output:
2
3
33
2. Given a value N, you need to you need to check given number is strong number or not.
Input :
The first line contains integer T, denoting number of test cases. Then T test cases follow.
The only line of each test case contains an integer N.

Output :
YES or NO

Definition:
A number is called strong number if sum of the factorial of its digit is equal to number itself.

Constraints :
1<=T<=10000
100<=N<=100000

Example:
Input :
145
6
40585
30

Output :
YES
NO
YES
NO
34
Result:

35
Ex.No.7
To Count the number of Vowels from the given String
Date:

Aim:
Write a C Program to count the number of vowels from the given string using switch
case.
Definition:
A letter representing a vowel sound, such as a, e, i, o, u.
Task :
Traverse entire string character by character and increment the vowel counter if the
current character is vowel.

Example 1: Karpagam University


The vowels in the input is a,a,a,U,i,e,i.
Total Vowels = 7

Input Format

First line of input consists of T , number of test cases.


For each test case, input consists of a characters n .

Constraints
T <= 1000
1 <= n <= 1000000

Output Format
For each test case print the total number of vowels.

Program Space:
36
Sample Input
Computer Science
C Programming
Welcome to Coimbatore

Sample Output
Total Vowels = 6
Total Vowels = 3
Total Vowels = 9

37
Practice Problems:

1. Write a C program to count the number of digits in a Integer.

Input:
Enter any Positive integer N greater than 0.

Output:
Print the number of digits in the given number using the Count variable as output.

Constraints:
100
1 ≤ N ≤ 10

Example:
Input:
256
123456789
951486

Output:
3
9
6
38
2. Given a String N, you need to find the frequency of characters in a String.
Input :
The first line contains string N. Then enter the character ch to which the frequency is to
be found.

Output :
Frequency of the character ch in N to be printed.

Constraints :
1<=N<=10000
ch in N

Example:
Input :
Enter a String : The Website is awesome.
Enter a character to find the frequency: e

Output :
Frequency of e = 4

Result:
39
Ex.No.8 To read a line of text from keyboard and print the number of characters, words
Date: and spaces

Aim:
Write a C Program to read a line of text from keyboard and print the number of
characters, words and spaces.

Task :
The program takes string input from the user and stores in variable line.

Initially, the variables characters, words and spaces are initialized to 0.

When the character is found, character variable is incremented by 1.


Similarly, words and spaces are incremented when these characters are found.

Finally, the count is displayed on the screen.

Example 1: Karpagam University


Characters = 18
Words = 2
Spaces= 2

Input Format

For each test case, input consists of a characters n .

Constraints
a <= n <= z

Output Format
For each test case print the total number of characters, words and spaces.

Program Space:
40
Sample Input
Computer Science
C Programming
Welcome to Coimbatore

Sample Output
Characters = 15 Words =2 White Spaces = 2
Characters = 12 Words =2 White Spaces = 2
Characters = 19 Words =3 White Spaces = 3

41
Practice Problems:
1. Write a C program to find substring from the given string using appropriate string
function.

Input:
Enter any String N and greater than 0.

Output:

The printf statement will print the sub string present in the given string as output.

Constraints:
1 ≤ N ≤ 10000

Example:
Input:

TechCrashCourse

Output:
left index = 2
Length of substring = 7
Then, Substring = chCrash
42
2. Given a File, read a line from a file and display it.

Input :

A file Contains a set of string.

Output :

A Line from the File.

Example:

Input :
program.txt file contains:

C Programming is awesome.
I like C programming.
How are you doing?

Output :
Data from the file: C programming is awesome.

Result:
43
Ex.No.9
To print student record using structure
Date:

Aim:
Write a C Program to print student record using structure.

Task :

a structure, student need to be created.

This structure has three members: name (string), roll (integer) and marks(float).

Then, we created a structure array of size 10 to store information of 10 students.

Using for loop, the program takes the information of 10 students from the user and displays it on
the screen.

Input Format

For each test case T, input consists student name, roll no and marks.

Constraints
name: a-z
1>=Roll no<=10000
1>=marks<=10000

Output Format
For each test case print the student name, roll no and marks.

Program Space:
44
Sample Input
Enter information:
Enter Name : Arun
Enter Rollno: 001
Enter Marks : 34.5

Sample Output
Displaying Information:
Name : Arun
Rollno: 001
Marks : 34.5
45
Practice Problems:
1. Write a C program to print the Book details in Department Library.

Input:
Enter the book details such as book_title, book_author, publisher and cost.

Output:

The printf statement will print the book detail as output.

Example:

Input:

Enter the Name of Book : ABC


Enter the Number of Pages : 100
Enter the Price of Book : 200
Enter the Name of Book : EFG
Enter the Number of Pages : 200
Enter the Price of Book : 300
Enter the Name of Book : HIJ
Enter the Number of Pages : 300
Enter the Price of Book : 500

Output:

--------- Book Details ------------

Name of Book : ABC


Number of Pages : 100
Price of Book : 200
Name of Book : EFG
Number of Pages : 200
Price of Book : 300
Name of Book : HIJ
Number of Pages : 300
Price of Book : 500
46
47
2. Write a C Program to print employee details.

Input :

The first line of input contains an integer T denoting the number of employees. Then T test cases
follow. Each test case contains an Emp.No, Name, Bpay, Allowance, Deducation and Netpay.

Output:

For each employee, given in input, print the Emp.No, Name, Bpay, Allowance, Deducation and
Netpay.

Example:

Input :

Enter the number of employees : 2


Enter the employee number : 101
Enter the name : Arun
Enter the basic pay, allowances & deductions : 5000 1000 250
Enter the employee number : 102
Enter the name : Babu
Enter the basic pay, allowances & deductions : 7000 1500 750

Output :

Emp.No. Name Bpay Allow Ded Npay


101 Arun 5000 1000 250 5750
102 Babu 7000 1500 750 7750
48
Result:

49
Ex.No.10
To find factorial of a number using recursion function
Date:

Aim:
To Write a C Program to find factorial of a number using recursion function.
Given an array of N integers,
Input Format
The first line contains an integer, N.
Output Format
Print the factorial of the numbers using recursive function.
Explanation
Print the factorial of integers, which is: 5!=5*4*3*2*1=120
Program Space:

Sample Input
5
6
Sample Output
120
720
50
Practice Problems:

1. Given two positive integers M,N, print G.C.D using recursion.


For example, if the given numbers are 366,60, output should be 6.
Input:

The first line of input contains two positive integers M and N.

Output:
Corresponding to each test case, in a new line, print the G.C.D.

What is GCD?
The Greatest Common Divisor (GCD) of two or more integers, when at least one of them
is not zero, is the largest positive integer that divides the numbers without a remainder. For
example, the GCD of 8 and 12 is 4.

Constraints:
1 ≤ M ≤ 10000
1 ≤ N ≤ 10000

Example:
Input:
81 153
100 70
20 12

Output:
9
10
4
51
52
2. Given a value N, you need to find sum of natural numbers using recursion.
Input :
The first line contains integer T, denoting number of test cases. Then T test cases follow.
The only line of each test case contains an integer N.

Output :
Print sum of natural numbers in all case.

Constraints :

1<=T<=10000

Example:
Input :
20
100
10

Output :
210
5050
55
53
Result:

54

You might also like