ES101 CP Lab Manual
ES101 CP Lab Manual
ES101 CP Lab Manual
for
Computer Programming (ES101)
Bachelor of Technology
2024 – 2025 (1st Semester)
(Common to All Branches)
Laboratory Evaluation
Component Duration Weightage Date(s) Syllabus Remarks
Mid Lab Exam (Lab 1 Hour 10% 30th Sept. - 04th Lab Week 1 – 10 Marks
Test-1) Oct. 2024* Week 5
End Lab Exam (Lab 1 Hour 10% 25th – 29nd Nov. Lab Week 6 – 10 Marks
Test-2) 2024* Week 11
*Tentative (subject to change as per requirements)
Every Week has two laboratory classes i.e lab-1 and lab-2. The experiments are
numbered with 1.1, 1.2 and so on etc where the first digit is the week number and second
digit is the assignment number.
In the 1st lab (Lab-1) of the week, viva voce will be taken. There will be maximum 4
questions related to the sessions conducted in the both theory and lab classes. The student
will execute at least two experiments in this lab. In the 2nd lab (Lab-2) the students will
complete execution of all remaining experiments. The maximum marks for viva voce and
program completion for each lab session is 01 (One).
There will be two Lab Evaluation Components each of 10 (3+5+2) marks with the
following divisions:
1. Viva voce (Continuous during any 3 lab sessions before Mid Lab and End Lab
Exam separately) – 3 Marks
2. Programming Test in Mid Lab and End Lab Exam separately – 5 Marks
3. Observation Copy - 2 Marks
known as a long format that displays detailed information about files and
-l directories.
-a Represent all files Include hidden files and directories in the listing.
Sort files and directories by their last modification time, displaying the most
-t recently modified ones first.
-S Sort files and directories by their sizes, listing the largest ones first.
1.3 Nano editor, Creating a file, Saving the file, Exit the Editor, Creating your resume file and
modifying it by adding some new achievements, Creating a C-Program File, Compiling using
gcc & Getting Output using ./a.out
1.3 Nano editor, Creating a file, Saving the file, Exit the Editor, Creating your
resume file and modifying it by adding some new achievements, Creating a C-
Program File, Compiling using gcc & Getting Output using ./a.out
Solution:
Nano Editor:
Nano is a command-line text editor that comes pre-installed with most Linux
distributions.
Nano provides essential editing features, making it ideal for quick edits, creating
configuration files, or writing scripts directly in the terminal.
Create and Open a New File in Nano Editor:
To Create a new file
Syntax:
nano newFileName
Example:
nano myresume.txt
Note: After creating a file, type your resume in the editor then save the file
To save the file (type the following command)
ctrl+o
Then press enter key
To exit from the editor
ctrl+x
o If there are unsaved changes, Nano will ask if you want to save before exiting.
Algorithm
Step-1 : Display "Welcome to IFHE"
Step-2 : Stop
Flowchart
Output:
Flowchart:
Output:
Name : ICFAI
Address: Donthanapally
City : Hyderabad
2.3 Write a C program to find the area of a circle. (Hints: Area= PI * r2)
2.4 Write a C program to enter marks of five subjects and calculate total, average and percentage
of marks.
2.5 Write a C program to convert days into years, weeks and days.
2.3 Write a C program to find the area of a circle. (Hints : Area= PI * r2)
Solution:
Algorithm
Start-1 : Declare variables: radius and area
Step-2 : Initialize PI=3.14159
Step-3 : Read radius
Step-4 : Compute area = PI * radius * radius
Step-5 : Display area
Step-6 : Stop
return 0;
}
Output:
Enter the radius of the circle: 4
The area of the circle with radius 4.00 is 50.27
Flowchart:
Start
Compute
total = sub1 + sub2 + sub3 + sub4 + sub5;
average = total / 5.0;
percentage = (total / 500.0) * 100;
Stop
Output:
Enter marks of five subjects:
78
95
63
42
86
Total marks = 364.00
Average marks = 72.80
Percentage = 72.80
2.5. Write a C program to convert days into years, weeks and days.
Solution:
Algorithm
Step-1 : Read days
Step-2 : Compute
years = days/365; // Calculate years
weeks = (days % 365)/7; // Calculate weeks
days = days - ((years*365) + (weeks*7)); //remaining days
Step-3 : Display years, weeks, days
Step-4 : Stop
return 0;
}
Output-1: Output-2:
Enter total number of days : 1329 Enter total number of days : 500
1329 days equivalent to : 500 days equivalent to :
Years: 3 Years: 1
Weeks: 33 Weeks: 19
Days: 3 Days: 2
1. What is variable?
Ans:
A variable is a quantity whose value is not fixed. The value of the variable can be changed
throughout the program. It is the memory location where we store value in the computer.
Now a=5 later a can be 10 and so on.
2. What is int data type?
Ans:
An integer type variable can store zero, positive, and negative values without any decimal. In
C language, the integer data type is represented by the int keyword, and it can be both signed
or unsigned. A variable can be declared as int using int a=5.
3. What are operators in C?
Ans:
Operators in C are symbols that perform operations on variables and values. They include
arithmetic operators (e.g., +, -, *, /), relational operators (e.g., ==, !=, >, <), logical operators
(e.g., &&, ||, !), bitwise operators (e.g., &, |, ^), assignment operators (e.g., =, +=, -=), and
others.
4. What is the difference between = and == in C?
Ans:
In C, = is an assignment operator used to assign a value to a variable, while == is a relational
operator used to compare two values for equality. For example, x = 5 assigns the value 5 to
Assignments:
the variable x, whereas x == 5 checks if x is equal to 5 and returns a boolean result (true or
false).
Assignments:
3.1 Write a C program to convert the temperature in degree Centigrade into Fahrenheit and vice
versa. (Formula: C= (F-32)/1.8)
3.2 You are given total time in seconds. Convert it into Hour: Min: Second format.
Algorithm:
Step-1 : Read temperature in celsius
Step-2 : Compute fahrenheit = (celsius * 9 / 5) + 32
Step-3 : Display Fahrenheit
Step-4 : Stop
Flowchart:
#include <stdio.h>
int main()
{
float celsius, fahrenheit;
return 0;
}
Output:
Enter temperature in Celsius: 42
42.00 Celsius = 107.60 Fahrenheit
3.2 You are given total time in seconds. Convert it into Hour: Min: Second
format.
Solution:
Flowchart Start
Read sec
Compute
hr = ( sec / 3600 )
min = ( sec - ( 3600 * h ) ) / 60
second = ( sec - ( 3600 * h ) - ( m * 60 ) )
#include <stdio.h>
int main() {
int sec, hr, min; // Declare variables for seconds, hours, minutes, and seconds
Output:
Input seconds: 10584
Hours:Minutes:Seconds - 2:56:24
Assignments:
3.3 Write a simple program that prints the results of all the operators available in C (including
pre/post increment, bitwise and/or/not, etc.). Read required operand values from standard input.
3.4 Write a C program to find out whether the character pressed through the Keyboard is a digit
or not (Use conditional operator only).
3.5 Write a C program to swap variable values of two variables using following techniques.
a) Using a third variable b) Without using third variable
3.3 Write a simple program that prints the results of all the operators available
in C (including pre/post increment, bitwise and/or/not, etc.). Read required
operand values from standard input.
Solution:
Algorithm:
Step-1 : Read two numbers to variables a and b.
Step-2 : Compute result using all relational, logical, increment, decrement, Bitwise operations
Step-3 : Display the result during calculation
Step-4 : Stop
Flowchart
Start
Read a and b
Stop
#include <stdio.h>
int main() {
int a, b;
// Read two integers from standard input
printf("Enter two integers (a and b): ");
scanf("%d %d", &a, &b);
// Arithmetic Operators
printf("Arithmetic Operators:\n");
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b); // Use %% to print the % character
// Relational Operators
printf("\nRelational Operators:\n");
printf("a == b = %d\n", a == b);
printf("a != b = %d\n", a != b);
printf("a > b = %d\n", a > b);
printf("a < b = %d\n", a < b);
printf("a >= b = %d\n", a >= b);
printf("a <= b = %d\n", a <= b);
// Logical Operators
printf("\nLogical Operators:\n");
printf("a && b = %d\n", a && b);
printf("a || b = %d\n", a || b);
printf("!a = %d\n", !a);
printf("!b = %d\n", !b);
// Bitwise Operators
printf("\nBitwise Operators:\n");
printf("a & b = %d\n", a & b);
printf("a | b = %d\n", a | b);
printf("a ^ b = %d\n", a ^ b);
printf("~a = %d\n", ~a);
printf("~b = %d\n", ~b);
// Shift Operators
printf("\nShift Operators:\n");
printf("a << 1 = %d\n", a << 1); // Left shift by 1
printf("a >> 1 = %d\n", a >> 1); // Right shift by 1
return 0;
}
Output:
Enter two integers (a and b): 15
6
Arithmetic Operators:
a + b = 21
a-b=9
a * b = 90
a/b=2
a%b=3
Relational Operators:
a == b = 0
a != b = 1
a>b=1
a<b=0
a >= b = 1
a <= b = 0
Logical Operators:
a && b = 1
a || b = 1
!a = 0
!b = 0
Bitwise Operators:
a&b=6
a | b = 15
a^b=9
~a = -16
~b = -7
Increment/Decrement Operators:
++a = 16
a++ = 16
--a = 16
a-- = 16
3.4 Write a C program to find out whether the character pressed through the
Keyboard is a digit or not (Use conditional operator only).
Solution:
Algorithm:
Step 1 : Read a Character to variable c
Step 2 : Compute isDigit = ( ( c > = ' 0 ' ) & & ( c < = ' 9 ' ) ) ? 1 : 0
Step 3 : If isDigit = 1 then
Display “Digit”
Else
Display “Not a Digit”
End of if block
Step 4: Stop
Read a character to c
Compute isDigit = ( ( c > = ' 0 ' ) & & ( c < = ' 9 ' ) ) ? 1 : 0
N
isDigit==1 ?
Stop
#include <stdio.h>
int main() {
char ch;
// Read a character from the user
printf("Enter a character: ");
scanf(" %c", &ch);
// Check if the character is a digit
(ch >= '0' && ch <= '9') ? printf("The character '%c' is a digit.\n", ch)
: printf("The character '%c' is not a digit.\n", ch);
return 0;
}
Algorithm:
Step 1: Read First Number to a
Step 2: Read Second Number to b
Step 3: Display “Before Swap” a and b
Step 4: Set temp =a
Set a = b
Set b = temp
Step 5: Display “After Swap” a and b
Step 6: Stop
#include <stdio.h>
int main()
{
int a,b,temp;
printf("Enter First Number : ");
scanf("%d",&a);
printf("Enter Second Number : ");
scanf("%d",&b);
printf("Before Swap a = %d and b = %d \n",a,b);
temp = a;
a = b;
b = temp;
printf("After Swap a = %d b = %d",a,b);
return 0;
}
Output
Algorithm:
Step 1 : Read First Number to a
Step 2 : Read Second Number to b
Step 3 : Display “Before Swap” a and b
Step 4 : Compute
a=a+b
b=a-b
a=a-b
Step 5 : Display “After Swap” a and b
Step 6 : Stop
Output:
Enter First Number : 5
Enter Second Number : 10
Before Swap a = 5 and b = 10
After Swap a = 10 b = 5
Assignments:
4.1 Write a C program to determine whether input number is Odd or Even. Display appropriate
message. (use if else statement)
4.2 Write a C program to find the maximum of given three numbers. (using if statement and
nested if statement separately)
Algorithm:
Step 1: Read num
Step 2: If (num mod 2 = 0) then
Display “Even”
Else
Display “Odd”
Endif
Step 3: Stop
Flowchart:
Start
Read num
N
is num module 2 = 0?
Stop
#include <stdio.h>
int main() {
int num;
//Input the Number
printf("Enter an integer: ");
scanf("%d", &num);
//Check the number is Even or Odd
if (num % 2 == 0) {
printf("%d is Even.\n", num);
} else {
printf("%d is Odd.\n", num);
}
return 0;
}
Output-1:
Enter an integer: 25
25 is Odd.
Output-2:
Enter an integer: 8
8 is Even.
4.2 Write a C program to find the maximum of given three numbers. (using if
statement and nested if statement separately)
Solution:
i) Using if statement
Algorithm:
Step-1 : Read a, b and c
Step-2 : If (a > = b and a > = c) then
Display a
Endif
Step-3 : If (b >= a and b >= c)
Display b
Endif
Step-4 : If (c >= a and c >= b)
Display c
Endif
Step-5 : Stop
Read a, b and c
Y
is (a>b and a>c) ?
N Display a
Y
is (b>a and b>c) ?
Display b N
Y
is (c>a and c>b) ?
N Display c
Stop
//Using simple if
#include <stdio.h>
int main() {
int a, b, c;
return 0;
}
Output-1:
Enter three different numbers: 94
45
67
94 is the largest number
Output-2:
Enter three different numbers: 12
58
32
58 is the largest number
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d%d%d", &a, &b, &c);
// outer if statement
if (a >= b) { // inner if...else
if (a >= c)
printf("%d is the largest number.", a);
else
printf("%d is the largest number.", c);
}
// outer else statement
else { // inner if...else
if (b >= c)
printf("%d is the largest number.", b);
else
printf("%d is the largest number.", c);
}
return 0;
}
Output-1:
Enter three numbers: 23
54
69
69 is the largest number.
Output-2:
Enter three numbers: 100
50
200
200 is the largest number.
4.3 Write a program to read marks from keyboard and your program should display
equivalent grade according to following table (if else ladder)
Marks Grade
100 – 80 Distinction
79 – 60 First Class
59 – 40 Second Class
< 40 Fail
4.4 Write a C program to input month number and print total number of days in month
using switch...case
4.5 Write a C program for designing a calculator using menu driven Program.
4.3 Write a program to read marks from keyboard and your program should
display equivalent grade according to following table (if else ladder)
Marks Grade
100 – 80 Distinction
79 – 60 First Class
59 – 40 Second Class
< 40 Fail
Solution:
Algorithm:
Step-1 : Read marks
Step-2 : If (marks > 100 OR marks < 0) then
Display “Your Input is out of Range"
Step-3 : Else if (marks >= 80) then
Display “You got Distinction"
Step-4 : Else if (marks >= 60) then
Display “You got First Class"
Step-5 : Else if (marks >= 40) then
Display “You got Second Class"
Else
Display “You got Fail"
Endif
Step-5 : Stop
Read marks
Is N
(marks >=80)
?
Is N
(marks >= 60)
?
Display “Distinction”
Y
Is
(marks >= 40)
? N
Display “Fail”
Stop
#include<stdio.h>
int main() {
int marks;
printf("\n Enter Marks between 0-100 :");
scanf("%d", &marks);
if (marks > 100 || marks < 0) {
printf("\n Your Input is out of Range");
}
else if (marks >= 80) {
printf("\n You got Distinction");
}
else if (marks >= 60) {
printf("\n You got First Class");
}
else if (marks >= 40) {
printf("\n You got Second Class");
}
else {
printf("\n You got Fail");
}
return 0;
}
Output-1:
Enter Marks between 0-100 :85
You got Distinction
Output-2:
Enter Marks between 0-100 :12
You got Fail
Output-3:
Enter Marks between 0-100 :56
You got Second Class
Output-4:
Enter Marks between 0-100 :105
Your Input is out of Range
Read month
Is month = 4 or Y
Set days = 30
6 or 9 or 11 ?
Is month = 1
or 3 or 5 or 7
Set days = 31
or 8 or 10 or Y
12?
?
Is Y Set days = 28
month = 2 ?
Set days = 0
Y
Is days = 0 ? Display “Invalid Month”
Display days
Stop
#include <stdio.h>
int main()
{
int month;
int days;
switch(month)
{
case 4:
case 6:
case 9:
case 11:
days=30;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 2:
days=28;
break;
default:
days=0;
break;
}
if(days)
printf("Number of days in %d month is: %d\n",month,days);
else
printf("You have entered an invalid month!!!\n");
return 0;
Second run:
Enter month: 2
Number of days in 2 month is: 28
Third run:
Enter month: 11
Number of days in 11 month is: 30
Fourth run:
Enter month: 13
You have entered an invalid month!!!
4.5 Write a C program for designing a calculator using menu driven Program.
Solution:
Algorithm:
Step 1: Declare local variables num1, num2, opt. For example, where n1 and n2 take two
numeric values, res will store results and opt variable define the operator symbols.
Step 2: Print the Choice (1. Addition, 2. Subtraction, 3. Multiplication, 4. Division, 5. Modulo
Division, 6. Exit)
Step 3: Enter the Choice
Step 4: Read two numbers, num1 and num2
Step 5: Switch case jump to an operator selected by the user
Step 6: Display the operation result
Step 8: Stop
Y
Is opt = 1 ? Display num1 + num2
N Y
Y
Is opt = 4 ? Display num1 / num2
Y
Is opt = 5 ? Display num1 mod num2
Display”Invalid Option”
Stop
case 2:
printf("Substraction of %d and %d is: %d\n",num1,num2,num1-num2);
break;
case 3:
printf("Multiplication of %d and %d is: %d\n",num1,num2,num1*num2);
break;
case 4:
if(num2==0) {
printf("The second integer is zero. Divide by zero.\n");
}
else {
printf("The Division of %d and %d is :
%d\n",num1,num2,num1/num2);
}
break;
case 5:
if(num2==0) {
printf("The second integer is zero. Devide by zero.\n");
}
else {
printf("The modulo Division of %d and %d is :
%d\n",num1,num2,num1%num2);
}
break;
case 6:
break;
default:
printf("Input correct option\n");
Output:
Enter the first Integer :15
Enter the second Integer :6
Answer:
Three types of loops are there in C. These are the while loop, the do-while loop, and the for loop.
2. Differentiate between for loop and a while loop? What are it uses?
Answer:
For executing a set of statements fixed number of times we use for loop whereas when the number
of iterations to be performed is not known in advance we use while loop.
Answer:
We use the do-while loop in C when we want the loop to execute at least once. If the test condition is
false, the for loop and while loop do not run even once.
Answer:
We use the “break” statement to exit from the loop as soon as the flow of control goes to this
statement. The “continue” statement is used to omit the current iteration and jump to the next
iteration of the loop.
Assignments:
5.1 Write a C program to find the sum of first 100 natural numbers.
5.2 Write a C program to find factorial of a given number
5.3 Write a C program to find the sum of odd numbers and even numbers between 1 to N (N is
a given input number).
Output:
The sum of the first 100 natural numbers is: 5050
#include <stdio.h>
int main() {
int n, i;
unsigned long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
// shows error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; i++) {
fact *= i;
}
printf("Factorial of %d = %lu", n, fact);
}
return 0;
}
Output-1:
Enter an integer: 5
Factorial of 5 = 120
Output-1:
Enter an integer: 12
Factorial of 12 = 479001600
Algorithm:
Step-1 : Initialize i=1, even_sum = 0 and odd_sum = 0
Step-2 : READ n
Step-3 : Repeat Step-4 and Step-7 until i<=n
Step-4 : If (i mod 2 = 0) then
Goto Step-5
Else
Got Step-6
Endif
Step-5 : Compute even_sum = even_sum + i and goto Step-7
Step-6 : Compute odd_sum = odd_sum + i
Step-7 : Compute i = i + 1
Step-8 : Display even_sum and odd_sum
Step-9 : Stop
Assignments:
Algorithm:
Step-1 : Initilize ctr=0, p_ctr=0, num=2, i=2
Step-2 : Repeat Step-3 to Step-10 until p_ctr <= 100
Step-3 : Set i = 2
Step-4 : Set ctr = 0
Step-5 : Repeat Step-6 to Step-7 until (i <=(num/2))
Step-6 : If (num % i = 0) then
Compute ctr = ctr +1
Step-7 : Compute i = i + 1
Step-8 : If (ctr = 0) then
Display num
Step-9 : Compute p_ctr= p_ctr + 1
Step-10 : Compute num = num +1
Step-11 : Stop
Output:
First 100 Prime Numbers are:
2
3
5
7
11
13
17
19
23
29
31
.
.
.
521
523
541
Solution :
a) 1
12
123
1234
12345
Algorithm:
Step-1 : Initialize i=1
Step-2 : Repeat Step-3 to Step-8 until i<=5
Step-3 : Initialize j=1
Step-4 : Repeat Step-5 and Step-6 until j<=i
Step-5 : Display j
Step-6 : Compute j=j+1
Step-7 : Display a new line
Step-8 : Compute i=i+1
Step-9 : Stop
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
return 0;
}
Output:
1
12
123
1234
12345
Output:
*
**
***
****
*****
Step-1: Divide the given decimal number by 2 and note down the remainder.
Step-2: Now, divide the obtained quotient by 2, and note the remainder again.
Step-3: Repeat the above steps until you get 0 as the quotient.
Step-4: Now, write the remainders in such a way that the last remainder is written first,
followed by the rest i.e in the reverse order.
Step-5 Stop
Step-1: Divide the given decimal number by 16 and note down the remainder (replace 10, 11,
12, 13, 14, 15 by A, B, C, D, E, F respectively i.e If the remainder is is less than 10, insert
(48 + remainder) in a character array otherwise if the remainder is greater than or
equal to 10, insert (55 + remainder) in the character array
Step-2: Now, divide the obtained quotient by 16, and note the remainder again as above.
Step-3: Repeat the above steps until you get 0 as the quotient.
Step-4: Now, write the remainders in such a way that the last remainder is written first,
followed by the rest i.e in the reverse order.
Step-5 Stop
#include<stdio.h>
int main()
{
int n, num, bin, rem = 0, place;
long int decn,rmd,q,dn=0,m,l;
int i=1,j,tmp,opt ;
char s;
Assignments:
6.1 Write a C program to count the frequency of digits in a given number. (Frequency means
number of occurrences of the digit in the given number)
6.2 Write a C program to find the sum of digits of the accepted number.
Algorithm:
Step-5 : Extract rem as the last digit of num by rem = num %10
Step-6 : Compare rem and digit and increment ctr if same i.e if rem=digit then ctr = ctr + 1
Step-9 : Stop
#include <stdio.h>
int main()
{
long num;
int digit,rem,count=0;
printf("Enter the Number: ");
scanf("%ld",&num);
printf("Enter the digit to be counted:");
scanf("%d",&digit);
while(num!=0)
{
rem=num%10;
if(rem==digit)
count++;
num=num/10;
}
printf("The digit %d present %d times ",digit,count);
}
OUTPUT
Enter the Number: 56755542311
Enter the digit to be counted:5
The digit 5 present 4 times
Algorithm:
Step-1 : Read num
Step-2 : Set sum = 0
Step-3 : Repeat Step-4 to Step-6 until (num > 0)
Step-4 : Extract rem as the last digit of num by rem = num mod 10
Step-5 : Compute sum = sum + rem
Step-6 : Remove the last digit of num by num = num / 10;
Step-7 : Display sum
Step-8 : Stop
#include <stdio.h>
int main()
{
int num, temp_num, sum = 0, rem;
printf("Enter an integer\n");
scanf("%d", &num); //Take any integer
temp_num=num; //Store the num in a temporary variable temp_num
while (num!=0) //Continue loop until num > 0
{
rem=num%10; //Find out remainder of division of num by 10
sum=sum+rem; //Find sum with rem
num=num/10; //Reduce the number num dividing by 10
}
num=temp_num; //Get the original number back from temp_num
printf("Sum of digits of %d = %d\n",num,sum);
return 0;
}
Output:
Enter an integer
24589
Sum of digits of 24589 = 28
Assignments:
6.3 Write a C program to check whether the given number is palindrome or not.
6.4 Write a C program to input a number and find sum of first and last digit of the number
using for loop (e.g Input 5642, Output=7)
6.5 Write a C program to print the given number in word form of digits.
(Ex input : 123, output is ONE TWO THREE )
Algorithm:
Step-1 : Initialize rev = 0
Step-2 : Read num
Step-3 : Set temp_num = num
Step-4 : Repeat Step-5 to Step-7 until num != 0
Step-5 : Compute rem = num mod 10
Step-6 : Compute rev = rev *10 + rem
Step-7 : Compute num = num / 10
Step-8 : If temp_num = rev then
Display Pallindrome
Else
Display Not Pallindrome
Endif
Step-9 : Stop
#include <stdio.h>
int main()
{
int num, temp_num, rev = 0, rem;
printf("Enter an integer\n");
scanf("%d", &num); //Take any integer
temp_num=num; //Store the num in a temporary variable temp_num
while (num!=0) //Continue loop until num > 0
{
rem=num%10; //Extract the last digit
rev=rev*10+rem; //Append the last digit to rev
num=num/10; //Remove the last digit
}
num=temp_num; //Get the original number back from temp_num
if(num==rev)
printf("\n%d is Pallindrome number",num);
else
printf("\n%d is Not a Pallindrome number",num);
return 0;
}
Output-1:
Enter an integer
4562
Output-1:
Enter an integer
8668
Algorithm:
Step-1 : Initialize sum = 0
Step-2 : Read num
Step-3 : Set lastdigit = num mod 10
Step-4 : Repeat Step-5 until num >= 10
Step-5 : Compute num = num / 10
Step-6 : Set firstdigit = num
Step-7 : Compute sum = firstdigit + lastdigit
Step-8 : Display sum
Step-9 : Stop
/**
* C program to find sum of first and last digit of a number using loop
*/
#include <stdio.h>
int main()
{
int num, sum=0, firstDigit, lastDigit;
/* Find the first digit by dividing num by 10 until first digit is left */
while(num >= 10)
{
num = num / 10;
}
firstDigit = num;
return 0;
}
Output:
Enter any number to find sum of first and last digit: 5642
Sum of first and last digit = 7
6.5 Write a C program to print the given number in word form of digits.
(Ex input : 123, output is ONE TWO THREE )
Solution:
Algorithm:
Step-1 : Read n
Step-2 : Take num as reverse of n
#include<stdio.h>
int main()
{
int rev=0,digit,num,mod;
printf("Enter a positive Integer\n");
scanf("%d", &num);
/* reverse the input number */
while (num > 0)
{
rev=(rev*10)+num%10;
num/=10;
}
Assignments:
7.1 Write a C program to find sum of two numbers using function without argument and
with return value
7.2 Write a program to find square of a number using function with argument and with
return value
7.3 Write a C program to find the Fibonacci numbers using a recursive function
#include<stdio.h>
int add(); //Function Prototype
int main()
{
int sum;
sum=add(); //Calling Function
printf("\nSum = %d",sum);
return(0);
}
Output:
Enter two numbers : 5
7
Sum = 12
Square of 9 = 81
int main(){
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n-2);//n-2 because 2 numbers are already printed
return 0;
}
Output:
Enter the number of elements: 7
Fibonacci Series: 0 1 1 2 3 5 8
Enter the number of elements: 15
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
7.4 Write a C program to find sum and average of n numbers in the array.
Solution
Program File : Prg74.c
#include<stdio.h>
int main()
{
int i,n,a[100],sum=0; //Array a[100] declared with size 100
float avg;
printf("Enter Size :");
scanf("%d",&n);
printf("Enter %d Numbers into the Array\n",n);
printf("\n----------------------------------\n");
//Display
for(i=0;i<n;i++)
printf("\n%d",a[i]);
printf("\nSum = %d and Average = %.2f",sum,avg);
return(0);
}
7.5 Write a C program to find both the largest and smallest number in a list of
integers.
Solution:
Program File : Prg75.c
#include<stdio.h>
int main()
{
int i,n,a[100],large,small;
printf("\nEnter the number of elements : ");
scanf("%d",&n);
printf("\nInput the array elements : ");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
Output:
Enter the number of elements : 5
Input the array elements : 10
25
68
14
23
The smallest element is 10
The largest element is 68
#include<stdio.h>
int main()
{
int mat1[3][3], mat2[3][3], i, j, mat3[3][3]; //Declaration of 3X3 matrices
printf("Enter 3*3 matrix 1 elements :");
for(i=0; i<3; i++) //Input to Mat1
{
for(j=0; j<3; j++)
scanf("%d",&mat1[i][j]);
}
printf("Enter 3*3 matrix 2 elements :");
for(i=0; i<3; i++) //Input to Mat2
{
for(j=0; j<3; j++)
scanf("%d",&mat2[i][j]);
}
Output:
Enter 3*3 matrix 1 elements :
121
365
421
Enter 3*3 matrix 2 elements :
961
543
276
1. What is an array?
Ans:
It is a collection of elements of same data type stored at contiguous memory locations.
2. What is the beginning and end index of an array?
Ans:
The beginning index of an array is always 0 (zero) and end index is its size-1.
3. Can we store and access beyond the size of the array size?
Ans:
No, it will show undesired result.
4. Mention some advantages and disadvantages of Arrays.
Ans:
Multiple elements of Array can be sorted at the same time.
Using the index, we can access any element in O(1) time.
Assignments:
Output:
Enter first 3*3 matrix element:
618
257
147
Enter second 3*3 matrix element:
963
852
741
8.2 Write a C program to input elements in an array and find the sum using
pointer as well as display array using pointer.
Solution:
Program File : Prg82.c
#include <stdio.h>
int main() {
int n, i,a[100],sum;
int *ptr;
sum=0;
for(i = 0; i < n; i++)
sum=sum+*(ptr+i);
printf("Array elements:");
for (i = 0; i < n; i++) {
printf("\n%d", *(ptr + i)); // Access and print elements using pointer dereferencing
}
printf("\nSum=%d",sum);
printf("\n");
return 0;
}
Output:
Enter the number of elements: 4
Enter the elements:
1
12
3
5
Array elements:
1
8.4 Write a C program to allocate a block of memory using Dynamic Memory Allocation
functions and perform the following.
i) accept and store the numbers
ii) find the minimum and maximum of the numbers
iii) find the average of the numbers
swap(&a, &b);
return 0;
}
Output:
Enter two numbers: 5 10
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, min, max, sum = 0;
int *ptr,*start=NULL;
float average;
printf("Enter the number of elements: ");
scanf("%d", &n);
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
start=ptr;
//Accept and store elements
printf("Enter the elements:\n");
for (i = 1; i <= n; i++) {
scanf("%d", ptr++);
}
ptr=start;
// Find minimum and maximum
min = max = *ptr;
for (i = 1; i <= n; i++) {
if (*ptr < min) {
min = *ptr;
}
if (*ptr > max) {
max = *ptr;
}
sum += *ptr;
ptr++;
return 0;
}
Output:
Enter the number of elements: 5
Enter the elements:
10
5
63
45
21
Minimum value: 5
Maximum value: 63
Average value: 28.80
1. What is pointer in C?
Ans:
2. What is the difference between array and structure?
Ans:
A pointer can be used to store the memory address of other variables, functions, or even
other pointers. The use of pointers allows low-level memory access, dynamic memory
allocation, and many other functionality in C.
3. What are the types of function with respect to function definition?
Ans:
Function can be called either with or without arguments and might return values. They may or
might not return values to the calling functions. So the types of functions are
1. Function with no arguments and no return value
2. Function with no arguments and with return value
3. Function with argument and with no return value
4. Function with arguments and with return value
Ans:
Call by Value involves passing the value as a copy to the function in which modifications
made within the function do not affect the original data but the Call by Reference passes the
memory address (reference) of the original data, allowing functions to directly modify the
original data.
Assignments:
9.1 Write a C program to store and print the roll no., name, age, address and marks of n
students using structure.
9.2 Write a C Program to enter the marks of 5 students in 3 courses Python, DAA, and JAVA
(each out of 100) using a structure named Marks having elements roll no., name,
Python_marks, DAA_marks and JAVA_marks and then display the percentage of each
student.
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
struct Student {
int roll_no;
char name[50];
int age;
char address[100];
float marks;
};
int main() {
struct Student students[MAX_STUDENTS];
int i,n;
printf("Enter Number of Students (<100) :");
scanf("%d",&n);
Output:
* * * * * * * * Student Information: * * * * * * * *
------------------------------------------------------------------
RollNo Name Age Address Marks
101 Rajesh 21 Hyderabad 87.00
102 Ayan 20 Delhi 91.00
103 Shivam 21 Odisha 90.00
104 Mahesh 21 Chennai 85.00
105 Ganesh 19 AP 93.00
#include <stdio.h>
#define SIZE 5
struct Marks {
int python;
int daa;
int java;
};
struct Students {
int roll_no;
char name[50];
struct Marks mark;
};
int main() {
struct Students students[SIZE];
int i;
float percentage;
Output:
Enter details for student 1:
Roll No.: 111
Name: Amiya
Python Marks: 89
DAA Marks: 88
Java Marks: 96
Enter details for student 2:
Roll No.: 222
Name: Rameya
Python Marks: 78
DAA Marks: 98
Java Marks: 85
Enter details for student 3:
Roll No.: 333
Name: Rupa
Python Marks: 75
DAA Marks: 58
Java Marks: 69
Enter details for student 4:
Roll No.: 444
Name: Narendra
Python Marks: 45
DAA Marks: 59
Java Marks: 68
Enter details for student 5:
Roll No.: 555
Name: Vamshi
Python Marks: 88
DAA Marks: 77
Java Marks: 66
---------------------------------
Roll No.: 222
Name: Rameya
Python Marks: 78
DAA Marks: 98
Java Marks: 85
Percentage: 87.00%
---------------------------------
Roll No.: 333
Name: Rupa
Python Marks: 75
DAA Marks: 58
Java Marks: 69
Percentage: 67.33%
---------------------------------
Roll No.: 444
Name: Narendra
Python Marks: 45
DAA Marks: 59
Java Marks: 68
Percentage: 57.33%
---------------------------------
Roll No.: 555
Name: Vamshi
Python Marks: 88
DAA Marks: 77
Java Marks: 66
Percentage: 77.00%
---------------------------------
9.3 Write a C program that uses functions and structures to perform the followings.
i) Reading a complex number
ii) Displaying the complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
9.4 Write a program to add two distances in inch-feet using structure. The values of the
distances are to be taken from the user. Display the sum.
9.3 Write a C program that uses functions and structures to perform the
followings.
i) Reading a complex number
ii) Displaying the complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
Solution:
#include <stdio.h>
struct Complex {
float real;
float imag;
};
int main() {
struct Complex c1, c2, sum, product;
printf("Sum: ");
dispComplex(sum);
printf("Product: ");
dispComplex(product);
return 0;
}
Output:
Enter first complex number:
Enter real part: 2
Enter imaginary part: 3
Enter second complex number:
Enter real part: 4
Enter imaginary part: 5
Sum: 6.00 + 8.00i
Product: -7.00 + 22.00i
#include <stdio.h>
struct Distance {
int feet;
float inches;
};
int main() {
struct Distance d1, d2, sum;
return 0;
}
Output-2:
Enter feet and inches for the first distance: 5
4
Enter feet and inches for the second distance: 3
11
Sum of distances: 9 feet 3.00 inches
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
union Student {
int roll_no;
char name[50];
int age;
char address[100];
float marks;
};
int main() {
union Student students[MAX_STUDENTS];
int i,n;
printf("Enter Number of Students (<100) :");
scanf("%d",&n);
Output:
* * * * * * * * Student Information: * * * * * * * *
------------------------------------------------------------------
RollNo Name Age Address Marks
1117782016 1117782016 80.00
1117650944 1117650944 79.00
1119617024 1119617024 94.00
Ans:
The main difference between a structure and a union is that a structure stores multiple data
types of different sizes, while union stores multiple data types of the same size in the same
location.
When the variables are declared in a structure, the compiler allocates memory to each
variables member. The size of a structure is equal or greater to the sum of the sizes of each
data member.
When the variable is declared in the union, the compiler allocates memory to the largest size
variable member. The size of a union is equal to the size of its largest data member size.
2. What is command line argument in C?
Ans:
Instead of invoking the input statement from inside the program, it is possible to pass data
from the command line to the main() function when the program is executed. These values
are called command line arguments.
Command-line arguments are the values given after the name of the program in the
command-line shell of Operating Systems. Command-line arguments are handled by the
main() function of a C program.
3. What is difference between character array and string in C?
Ans:
Character array is the array of characters but a String is an array of character ending with a
NULL character (“\0”).
4. What is macro in C and how to define a macro?
Ans:
A macro is a piece of code in a program that is replaced by the value of the macro. Macro is
defined by #define directive. Whenever a macro name is encountered by the compiler, it
replaces the name with the definition of the macro. Macro definitions need not be
terminated by a semi-colon(;).
10.1 Write a C program to perform the following operations without using string handling
functions.
i) length of a given string ii) string copy
10.2 Write a C program to perform the following operations without using string handling
functions
i) compare two strings ii) concatenate two strings
Algorithm:
Step 1: SET length := 0
Step 2: PRINT Enter a string :
Step 3: READ str
Step 4: Repeat step 4.1 to 4.2 while str [ i ] ! = ' \ 0 '
Step 4.1: SET length := length + 1
Step 4.2: SET i := i + 1
End of while block
Step 5: PRINT length
Step 6: Stop
int main() {
char str[100];
int length = 0, i;
return 0;
}
Output:
Enter a string: IcfaiTech
Length of the string: 9
Algorithm:
Step 1: PRINT Enter a string :
Step 2: READ str1
Step 3: Repeat Step 3.1 to 3.2 while str1 [ i ] ! = ' \ 0 ' ;
Step 3.1: SET str2 [ i ] := str1 [ i ]
Step 3.2: SET i := i + 1
End of for block
Step 4: SET str2 [ i ] := ' \ 0 '
Step 5: PRINT str2
Step 6: Stop
// string copy
#include <stdio.h>
int main() {
char str1[100], str2[100];
int i;
return 0;
}
Output:
Enter a string: IcfaiTech
Copied string: IcfaiTech
Algorithm:
Step-1 : Read two strings as str1 and str2
Step-2 : Compare the two strings str1 & str2 by checking whether corresponding
characters of both str1 and str2 are equal, or first string is greater than the
second or the first string is less than the second string
Step-3 : Display equal if str1 and str2 same otherwise display not equal
Step-4 : Stop
int main() {
char str1[100], str2[100];
int i;
for (i = 0; str1[i] == str2[i] && str1[i] != '\0' && str2[i] != '\0'; i++);
if (str1[i] == str2[i]) {
printf("The strings are equal.\n");
}
else {
printf("The strings are different.\n");
}
return 0;
}
Output-1:
Enter the first string: IcfaiTech
Enter the second string: IcfaiTech
The strings are equal.
Output:2
Enter the first string: IcfaiTech
Enter the second string: IcfaiTch
The strings are different.
Algorithm:
Step 1: PRINT Enter the first string :
Step 2: READ str1
Step 3: PRINT Enter the second string :
Step 4: READ str2
Step 5: Repeat step 5.1 to 5.2 while str1[ i ] ! = ' \ 0 ' ;
Step 5.1: SET result[ i ] := str1[ i ]
Step 5.2: SET i := i + 1
End of for block
Step 6: Repeat step 6.1 to 6.2 while str2[ j ] ! = ' \ 0 ' ;
Step 6.1: SET result[ i + j ] := str2[ j ]
Step 6.2: SET j := j + 1
End of for block
Step 7: SET result[ i + j ] := ' \ 0 '
Step 8: PRINT result
Step 9: Stop
int main() {
char str1[100], str2[100], result[200];
int i, j;
result[i + j] = '\0';
printf("Concatenated string: %s\n", result);
return 0;
}
Output:
Enter the first string: Icfai
Enter the second string: Tech
Concatenated string: IcfaiTech
10.4 Write a C program to count the number of lines, characters and words in a given text of a
given string without using string handling functions.
10.5 Write a C program to convert a given string from lowercase to uppercase without using
string handling functions.
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], str3[200];
int length;
return 0;
}
Output:
Enter the first string: Icfai
Enter the second string: Tech
Length of the first string: 5
Copied string: Icfai
The two strings are not equal.
Concatenated string: IcfaiTech
10.4 Write a C program to count the number of lines, characters and words in
a given text of a given string without using string handling functions.
Solution:
Program File : Prg104.c
#include <stdio.h>
int main() {
int ctr_char, ctr_word, ctr_line; // Variables to count characters, words, and lines
int c; // Variable to hold input characters
int flag; // Flag to track word boundaries
Output:
ICFAI Foundation for Higher Education
Donthanpally, Shankarapally Road
Hyderabad, India
501203
Number of Characters = 91
Number of words = 11
Number of lines = 4
return 0;
}
Output:
Enter a string: icfaiTech
Uppercase string: ICFAITECH
Assignments:
11.1 Write a C program to read input characters from user and write it to file. After that read
the content of the same file and display on screen.
11.2 Write a C program to implement copy command with the use of Command line
arguments. Like “mycopy source.txt destination.txt” should copy content of source file to
destination file.
11.1 Write a C program to read input characters from user and write it to file.
After that read the content of the same file and display on screen.
Solution:
Program File : Prg111.c
#include <stdio.h>
int main() {
FILE *fptr;
char filename[50];
char ch;
fclose(fptr);
return 0;
}
Output:
11.2 Write a C program to implement copy command with the use of Command
line arguments. Like “mycopy source.txt destination.txt” should copy content
of source file to destination file.
Solution:
Program File : Prg112.c
#include <stdio.h>
int main(int argc, char *argv[]) {
FILE *source_file, *destination_file;
char ch;
if (argc != 3) {
printf("Usage: %s source_file destination_file\n", argv[0]);
return 1;
}
if (source_file == NULL) {
fclose(source_file);
fclose(destination_file);
return 0;
}
Output:
prompt#./a.out Address.txt Location.txt
File copied successfully!
Prompt#cat Location.txt
Department of CSE & AIDS
Faculty of Science and Technology
ICFAI Foundation for Higher Education
Hyderabad, 501203
Assignments:
11.3 Write a C program to write integer numbers to a file. Read them back and find the
average of all numbers.
11.4 Write a program to copy one file to another. While doing so replace all lowercase
characters with their equivalent uppercase characters.
11.3 Write a C program to write integer numbers to a file. Read them back and
find the average of all numbers.
Solution:
Program File : Prg113.c
#include <stdio.h>
int main() {
FILE *fptr;
int n, i, num, sum = 0;
float average;
if (fptr == NULL) {
printf("Error opening file!\n");
return 1;
}
return 0;
}
Output:
Enter the number of integers: 5
Enter 5 integers:
10
2
8
16
5
Average of numbers: 8.20
11.4 Write a program to copy one file to another. While doing so replace all
lowercase characters with their equivalent uppercase characters.
Solution:
Program File : Prg114.c
#include <stdio.h>
#include<ctype.h>
if (argc != 3) {
printf("Usage: %s source_file destination_file\n", argv[0]);
return 1;
}
if (destination_file == NULL) {
printf("Error creating destination file!\n");
fclose(source_file);
return 1;
}
fclose(source_file);
fclose(destination_file);
return 0;
}
OUTPUT:
Prompt#./a.out Address.txt UpperAddress.txt
File copied and converted to uppercase successfully!
if (argc != 4) {
printf("Usage: %s file1 file2 merged_file\n", argv[0]);
return 1;
}
if (file1 == NULL) {
printf("Error opening file1!\n");
return 1;
}
if (file2 == NULL) {
printf("Error opening file2!\n");
fclose(file1);
return 1;
}
if (mergedFile == NULL) {
printf("Error creating merged file!\n");
fclose(file1);
fclose(file2);
return 1;
}
OUTPUT:
Prompt# cat > file1
This is my first file.
This will be merged with second file.
Press ^ + d to Save
and So on…