[go: up one dir, main page]

0% found this document useful (0 votes)
6 views60 pages

PPS LAB Manual Revised

hoihoikhil

Uploaded by

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

PPS LAB Manual Revised

hoihoikhil

Uploaded by

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

Maharana Pratap Institute of Technology

Lachhipur, Sonauli Road, Gorakhnath, Gorakhpur, UP-273015


Department of Applied Science

(Affiliated to Dr. A.P.J. Abdul Kalam Technical University, Lucknow)


Academic Session: 2024-25 (ODD-SEMESTER)

Programming for Problem Solving


LAB MANUAL

1st Year, Course Code: BCS151/ BCS251

Scheme: 2024-25

w.e.f. 1st September, 2024

Proposed by :
Mr. Sachidanand Chaturvedi

1
Table of Contents

Sl. No Particulars Page No.


1. Vision, Mission 4
2. POs 5
3. Course Details 6-9
 Course Objectives
 Course Outcomes
 Syllabus
 List of experiments
 CO-PO Mapping
4. Continuous Internal Lab Evaluation Process 10
5. Lab Rubrics 11-12
6. Lab Evaluation Sheet 13
7. LAB 1 WAP that accepts the marks of 5 subjects and finds 14-19
the sum and percentage marks obtained by the student.
WAP that calculates the Simple Interest and
Compound Interest. The Principal, Amount, Rate of
Interest and Time are entered through the keyboard.
WAP to calculate the area and circumference of a
circle.
WAP that accepts the temperature in Centigrade and
converts into Fahrenheit using the formula C/5=(F-
32)/9.

8 LAB 2 WAP that swaps values of two variables using a third 20-24
variable.
WAP that checks whether the two numbers entered by
the user are equal or not.
WAP to find the greatest of three numbers.
WAP that finds whether a given number is even or
odd.

9 LAB 3 WAP that tells whether a given year is a leap year or 25-29
not.
WAP that accepts marks of five subjects and finds
percentage and prints grades according to the
following criteria:
Between 90-100%-----Print ‘A’
80-90%-----------------Print ‘B’
60-80%-----------------Print ‘C’
2
Below 60%-------------Print ‘D’

WAP that takes two operands and one operator from


the user, perform the operation, and prints the result
by using switch statement.

10 LAB 4 WAP to print the sum of all numbers up to a given 30-35


number.
WAP to find the factorial of a given number.
WAP to print sum of even and odd numbers from 1 to
N numbers.
WAP to print the Fibonacci series.
11 LAB 5 WAP to check whether the entered number is prime or 36-41
not.
WAP to find the sum of digits of the entered number.
WAP to find the reverse of a number using function.
WAP to print Armstrong numbers from 1 to 100.
WAP to convert binary number into decimal number
and vice versa.

12 LAB 6 WAP that simply takes elements of the array from the 42-49
user and finds the sum of these elements.
WAP that inputs two arrays and saves sum of
corresponding elements of these arrays in a third array
and prints them.
WAP to find the minimum and maximum element of
the array.
WAP to search an element in a array using Linear
Search.
WAP to add and multiply two matrices of order nxn.
WAP that finds the sum of diagonal elements of a mxn
matrix.

13 LAB 7 WAP to swap two elements using the concept of 50-52


pointers.
WAP to sort the elements of the array in ascending
order using Bubble Sort technique and pointer.

14 LAB 8 WAP to compare the contents of two files and 53-56


determine whether they are same or not.
WAP to check whether a given word exists in a file or
not. If yes then find the number of times it occurs.
15 Viva Viva Questions 57-60

3
Vision of the Institute
To become Centre of Excellence in technical education and occupy prominent place
amongst top technical institutes of the nation.

Mission of the Institute

[M1]: To build a culture of excellence in teaching and learning with required


performance and obligated from all support activities.
[M2]: To cultivate a culture to flourish new ideas, research and innovation which leads
in emergence of creators, innovators and entrepreneurs..
[M3]: To encourage contribution to society and nation by inculcating professional
competency & ethics, humanity, values and leadership.

4
Program Outcomes (POs)
Engineering Graduates will be able to:
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering
problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis
of the information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant
to the professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and
need for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
9. Individual and teamwork: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, and give
and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one‘s own work, as a member
and leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.

5
Course Details

Course Name: Programming for Problem Solving Lab


Course Code: BCS151 / BCS251
Course prerequisite: Basic knowledge of mathematical formulas and algorithm

Course Objectives

This course will enable the students to


 Become familiar with various operators and their precedence.
 Solve problem using array and structure.
 Solve real world problem using modular approach.
 Solve problem based on file handling.

Course Outcomes
CO After completion of the course students will be able to:
BCS 151.1 / Write program for arithmetic and logical problems in C language.
251.1
BCS 151.1 / Write program using modular concepts, arrays, structure and looping in C
251.2 language.

BCS 151.1 / Implement the pointer and file handling concepts using C programs.
251.3

Text books: (as per AKTU syllabus)


 Schaum's Outline of Programming with C by Byron Gottfried , McGraw-Hill.
 Computer Concepts and Programming in C, E Balaguruswami, McGraw Hill .

6
Syllabus

Lab Code BCS251 Internal Assessment Marks 50


No. of lab hours / 02 External Exam Marks 50
week
Total No. of lab hours 24 Exam hours 02

List of Experiments

S.N. Program CO
I WAP that accepts the marks of 5 subjects and finds the sum and percentage marks
obtained by the student.
II WAP that calculates the Simple Interest and Compound Interest. The Principal,
Amount, Rate of Interest and Time are entered through the keyboard.
LAB 1 CO1
III WAP to calculate the area and circumference of a circle.
IV WAP that accepts the temperature in Centigrade and converts into Fahrenheit
using the formula C/5=(F-32)/9.

I WAP that swaps values of two variables using a third variable.

II WAP that checks whether the two numbers entered by the user are equal or not.
LAB 2 CO1
III WAP to find the greatest of three numbers.
IV WAP that finds whether a given number is even or odd.

I WAP that tells whether a given year is a leap year or not.

II WAP that accepts marks of five subjects and finds percentage and prints grades
according to the following criteria:
Between 90-100%-----Print ‘A’
80-90%-----------------Print ‘B’
LAB 3 60-80%-----------------Print ‘C’ CO1
Below 60%-------------Print ‘D’

III WAP that takes two operands and one operator from the user, perform the
operation, and prints the result by using switch statement.

I WAP to print the sum of all numbers up to a given number.


7
II WAP to find the factorial of a given number. CO2

LAB 4
III WAP to print sum of even and odd numbers from 1 to N numbers.
IV WAP to print the Fibonacci series.
I WAP to check whether the entered number is prime or not.

II WAP to find the sum of digits of the entered number.


LAB 5 CO2
III WAP to find the reverse of a number using function.
IV WAP to print Armstrong numbers from 1 to 100.
V WAP to convert binary number into decimal number and vice versa.
I WAP that simply takes elements of the array from the user and finds the sum of
these elements.
II WAP that inputs two arrays and saves sum of corresponding elements of these
arrays in a third array and prints them.
LAB 6 WAP to find the minimum and maximum element of the array. CO2
III
IV WAP to search an element in a array using Linear Search.
V WAP to add and multiply two matrices of order nxn.
VI WAP that finds the sum of diagonal elements of a mxn matrix.
I WAP to swap two elements using the concept of pointers.

LAB 7 CO3
II WAP to sort the elements of the array in ascending order using Bubble Sort
technique and pointer.
I WAP to compare the contents of two files and determine whether they are same or
not.
LAB 8 CO3
II WAP to check whether a given word exists in a file or not. If yes then find the
number of times it occurs

8
CO-PO Mapping

PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1 3 2 - - 2 - - - - - - 3
CO2 3 2 3 - 2 - - - - - - 3
CO3 3 2 3 - 2 - - - - - - 3

AVERAGE 3 2 3 0 2 0 0.00 0.00 0 0 0 3

9
Continuous Internal Lab Evaluation Process
 CIE Marks in each practical/lab shall be the sum of marks prescribed for Lab tests
and continuous evaluation of experiments. Marks prescribed for lab tests shall be 20
and that for continuous evaluation of experiments 8.
 The CIE marks awarded for lab tests in the practical Course/s shall be based on two
lab tests generally conducted during the semester. Both lab tests shall be conducted
for a maximum of 20 marks and average / best one is considered as final marks. This
final mark is proportionality scale to 30 marks.
 The remaining 20 marks shall be awarded on the basis average marks obtained
for continuous evaluation during conduction of experiments.
 Tentative schedule for each class test:

Lab Test 1- Preferably after 1st class test.


Lab Test 2- After 2nd class test and before PUT.

10
Lab Rubrics:

Rubrics for the lab assessment

Weightage
Rubric Parameters
(Assessment Marks)

R1 Arrangement/Algorithm of 4
Program and Attendance of
student
R2 Observation/Execution of Experiment 2

R3 Catechism / Viva-Voce 2

R4 Result/Output and Record Evaluation 2

Total 10

Rubric #R1: Arrangement/Algorithm of Program and Attendance of student


Level of Achievement
Excellent (90-100%) Very Good (80-89%) Good (60-79%) Average (40-59%) Poor (0-39%)
All the algorithms Most of algorithms Some of Few of algorithms Algorithms or
or procedures are or procedures are algorithms or or procedures are procedures are
written clearly. written clearly. procedures are written clearly. written but with
written several errors.
undoubtedly.

Rubric #R2: Observation/Execution of Experiment


Level of Achievement
Excellent (90- Very Good (80-89%) Good (60-79%) Average (40-59%) Poor (0-39%)
100%)
Completeness of Completeness of Completeness of Code/setup with Code/setup with
Code/setup with Code/setup with no Code/setup with some error, not some error, not
no error, error, consistent no error, variable properly properly
consistent variable naming and naming/setup is written/planned written/planned
variable naming well not proper . All code/setup . All code/setup
and well Formatted/proper . All code/setup runs properly after runs properly
formatted/ proper planning. All runs properly but rectifying errors after rectifying

11
planning code/setup runs not provide all but not provide all errors but not
properly but not results. results. provide all
. All code/setup provide all results. results.
runs properly and
provide all results.
Rubric #R3: Viva-Voce
Level of Achievement
Excellent (90- Very Good (80- Good (60-79%) Average (40-59%) Poor (0-39%)
100%) 89%)
Demonstrates Demonstrates Has a fair idea of Has some idea of Confused in
good knowledge good knowledge both theory and theory but do not both theory
of both theory of either theory experiments know and
and experimental or experimental but confused in experimental experimental
Procedure. Procedure. experimental Procedure. procedure
procedure

Rubric #R4: Result/Output and Record Evaluation


Level of Achievement
Excellent (90- Very Good (80- Good (60-79%) Average (40- Poor (0-39%)
100%) 89%) 59%)
Results are Results are Superficial and Results are Conclusions are
explicitly explicitly immediate interpreted but not logical
interpreted interpreted and conclusions are interpretation is and/or do not
and compared compared with recorded not explicit. agree with data
with literature literature data Results are Conclusions are presented.
data and/or and/or concepts interpreted but written Conclusions are
concepts discussed in interpretation is coherently but written in non-
discussed in lecture. Conclusion not explicit. not logical and coherent
lecture. is written in Conclusions are contain some manner with
Conclusion is coherent manner written coherently spelling or many spelling
written in but some errors in but contain some grammatical and
coherent English syntax spelling or errors. grammatical
manner with grammatical errors
proper English errors.
syntax.

12
Lab Evaluation Sheet

Laboratory Performance Evaluation Sheet


Name of student…………………………. Roll No. ……………………….. Department…………………

Lab Code…………….. Lab Name……………….. Session…………

CIE-I
S.N. Date Name of Experiments Marks Obtained Remark and
A(4) O(2) C(2) R(2) Total(10) Signature of
faculty

CIE-II

LT Date Name of Experiment Marks Obtained Remark and


Signature of
A(5) O(5) C(5) R(5) Total(20) faculty

LT-1

LT-2

Marks Obtained
CIE-I (Scaled to 20) CIE-II(Scaled to 30) Total Marks(50) Faculty Signature HOD signature

A- Arrangement/Algorithm of Program and Attendance of student


O- Observation/Execution of Experiment
C- Catechism / Viva-Voce
R- Result/Output and Record Evaluation

13
Program #1

Objective:
I WAP that accepts the marks of 5 subjects and finds the sum and percentage marks
obtained by the student.
II WAP that calculates the Simple Interest and Compound Interest. The Principal,
Amount, Rate of Interest and Time are entered through the keyboard.

III WAP to calculate the area and circumference of a circle.


IV WAP that accepts the temperature in Centigrade and converts into Fahrenheit
using the formula C/5=(F-32)/9

Software required: Dev C++ /Tubo C / GCC

I. WAP that accepts the marks of 5 subjects and finds the sum and percentage marks
obtained by the student.

Algorithm:
 start
 Read five subject marks and store them into 5 different variables.
 Calculate the sum of all subjects and store in total = eng + phy + chem + math + comp.
 Divide the sum of all subjects by the total number of subjects to find the average i.e.
average = total / 5.
 Calculate the percentage using percentage = (total / 500) * 100.
 Finally, print resultant values total, average, and percentage.
 stop

Flow Chart:

14
Source Code:
#include <stdio.h>
int main()
{
float eng, phy, chem, math, comp;
float total, average, percentage;
printf("Enter marks of five subjects: :- ");
scanf("%f%f%f%f%f", &eng, &phy, &chem, &math, &comp);
total = eng + phy + chem + math + comp;
average = total / 5.0;
percentage = (total / 500.0) * 100;
printf("Total marks = %.2f\n", total);
printf("Average marks = %.2f\n", average);
printf("Percentage = %.2f", percentage);

15
return 0;
}

Input:
Enter marks of five subjects: :-
90
90
90
90
90
Output:
Total marks = 450.00
Average marks = 90.00
Percentage = 90.00

II.WAP that calculates the Simple Interest and Compound Interest. The Principal, Amount,
Rate of Interest and Time are entered through the keyboard.
Algorithm:
1. Begin.
2. Input principal amount (P), rate of interest (R), and time period (T) from the keyboard.
3. Calculate simple interest using the formula: Simple Interest (SI) = (P * R * T) / 100
4. Print the calculated simple interest.
5. Calculate amount (A) using the formula: Amount = P + SI
6. Input the number of times interest is compounded per time period (n) from the keyboard.
7. Calculate compound interest using the formula: Compound Interest (CI) = P * (1 + (R / (100
* n)))^(n * T) - P
8. Print the calculated compound interest.
9. End.

Flow Chart:

16
Source Code:

#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
float p, t, r, si, ci;
clrscr();
printf("Enter principal amount (p): ");
scanf("%f", &p);
printf("Enter time in year (t): ");
scanf("%f", &t);
printf("Enter rate in percent (r): ");
scanf("%f", &r);

/* Calculating simple interest */


si = (p * t * r)/100.0;

/* Calculating compound interest */


ci = p * (pow(1+r/100, t) - 1);

printf("Simple Interest = %0.3f\n", si);


printf("Compound Interest = %0.3f", ci);
getch();
return(0);
}

17
Enter principal amount (p): 5000 ↲
Input:

Enter time in year (t): 2 ↲


Enter rate in percent (r): 18 ↲

Output:
Simple Interest = 1800.000
Compound Interest = 1962.000

III. WAP to calculate the area and circumference of a circle.

Algorithm:
Step 1: Start
Step 2: Read radius
Step 3: Calculate area, area = 3.14*radius*radius and cir = 3.14*radius*radius
Step 4: Print area , cir
Step 5: Stop

Flow Chart:

Source Code:
#include <stdio.h>
#define PI 3.14159
int main() {
double radius, area, circumference;
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);
area = PI * radius * radius;

18
circumference = 2 * PI * radius;
printf("Area of the circle: %.2lf\n", area);
printf("Circumference of the circle: %.2lf\n", circumference);
return 0;
}

Input:
Enter the radius of the circle: 34

Output:
Area of the circle: 3631.68
Circumference of the circle: 213.63

IV. WAP that accepts the temperature in Centigrade and converts into Fahrenheit using the
formula C/5=(F-32)/9.

Algorithm:
1. start
2. Define temperature in Fahrenheit unit.
3. Apply in the formula C/5=(F-32)/9.
4. Print the temperature in Celsius.
5. stop

Flow Chart:

19
Source Code:
#include<stdio.h>
int main()
{
float Fahrenheit, Celsius;
Fahrenheit = 64;
Celsius = ((Fahrenheit-32)*5)/9;
printf("\n\n Temperature in Celsius is : %f",Celsius);
return (0);
}

Input:
Fahrenheit = 64;
Output:
Temperature in Celsius is: 17.7777777778

Program #2

Objective:
I WAP that swaps values of two variables using a third variable.

II WAP that checks whether the two numbers entered by the user are equal or not.

III WAP to find the greatest of three numbers.


IV WAP that finds whether a given number is even or odd.

Software required: Dev C++ /Tubo C / GCC

I. WAP that swaps values of two variables using a third variable.

Algorithm:
Step-1 Start
Step-2 Input Two Numbers Say NUM1, NUM2
Step-3 Display Before Swap Values NUM1, NUM2
Step-4 TEMP = NUM1
Step-5 NUM1 = NUM2
Step-6 NUM2 = NUM1
Step-7 Display After Swap Values NUM1, NUM 2
Step-8 Stop Algorithm

Flow Chart:

20
Source Code:
include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);

// value of first is assigned to temp


temp = first;

// value of second is assigned to first


first = second;

// value of temp (initial value of first) is assigned to second


second = temp;

// %.2lf displays number up to 2 decimal points


printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf", second);
return 0;
}

Input:
21
Enter first number: 1.20
Enter second number: 2.45

Output:
After swapping, first number = 2.45
After swapping, second number = 1.20

II. WAP that checks whether the two numbers entered by the user are equal or not.
Algorithm:
1. Start
2. Input the first number (num1)
3. Input the second number (num2)
4. If num1 is greater than num2, then set max_num to num1
5. Else, set max_num to num2
6. Output max_num as the larger number
7. End

Flow Chart:

Source Code:
#include <stdio.h> // Include the standard input/output header file.

void main()
{
int int1, int2; // Declare two integer variables 'int1' and 'int2'.

printf("Input the values for Number1 and Number2 : ");


scanf("%d %d", &int1, &int2);
if (int1 == int2
printf("Number1 and Number2 are equal\n");
else
printf("Number1 and Number2 are not equal\n}
Input:
Input the values for Number1 and Number2 : 15 15
22
Output:
Number1 and Number2 are equal

III. WAP to find the greatest of three numbers.

Algorithm:
 Start
 Initialize the three numbers let say a,b,c
 Check if a is greater than b.
 If true, then check if a is greater than c.
o If true, then print ‘a’ as the greatest number.
o If false, then print ‘c’ as the greatest number.
 If false, then check if bis greater than num3.
o If true, then print ‘b’ as the greatest number.
o If false, then print ‘c’ as the greatest number
 end

Flow Chart:

Source Code:
#include <stdio.h>

int main()
{
int A, B, C;

printf("Enter the numbers A, B and C: ");

23
scanf("%d %d %d", &A, &B, &C);

// finding max using compound expressions


if (A >= B && A >= C)
printf("%d is the largest number.", A);

else if (B >= A && B >= C)


printf("%d is the largest number.", B);

else
printf("%d is the largest number.", C);

return 0;
}
Input:
Enter the numbers A, B and C: 5 7 9

Output:
9 is largest number

IV. WAP that finds whether a given number is even or odd.

Algorithm:
 Step 1- Start the program.
 Step 2- Read/input the number.
 Step 3- if n%2==0 then the number is even.
 Step 4- else number is odd.
 Step 5- display the output.
 Step 6- Stop the program.

Flow Chart:

24
Source Code:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

// true if num is perfectly divisible by 2


if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);

return 0;
}

Input:
Enter an integer: -7
Output:
-7 is odd.
Program #3

Objective:
I WAP that tells whether a given year is a leap year or not.

II WAP that accepts marks of five subjects and finds percentage and prints grades
according to the following criteria:
Between 90-100%-----Print ‘A’
80-90%-----------------Print ‘B’
60-80%-----------------Print ‘C’
Below 60%-------------Print ‘D’

III WAP that takes two operands and one operator from the user, perform the
operation, and prints the result by using switch statement.

Software required: Dev C++ /Tubo C / GCC

I. WAP that tells whether a given year is a leap year or not.

Algorithm:
START
Step 1 → Take integer variable year
Step 2 → Assign value to the variable

25
Step 3 → Check if year is divisible by 4 but not 100, DISPLAY "leap year"
Step 4 → Check if year is divisible by 400, DISPLAY "leap year"
Step 5 → Otherwise, DISPLAY "not leap year"
STOP

Flow Chart:

Source Code:
#include <stdio.h>

int main() {
int inputYear;

printf("Enter a year: ");


scanf("%d", &inputYear);

if ((inputYear % 4 == 0 && inputYear % 100 != 0) || (inputYear % 400 == 0)) {


printf("%d is a leap year.\n", inputYear);
} else {
printf("%d is not a leap year.\n", inputYear);
}

return 0;
}

Input:
Enter a year: 2020
Output:
2020 is a leap year

II. WAP that accepts percentage and prints grades according to the following criteria:
26
Between 90-100%-----Print ‘A’
80-90%-----------------Print ‘B’
60-80%-----------------Print ‘C’
Below 60%-------------Print ‘D’
.
Algorithm:
Step 1: Start
Step 2: Enter the marks obtained as M
Step 3: IF M >= 90
Print "A"
Step 3: IF M >= 80 and M < 90
Print "B"
Step 4: IF M >= 60 and M < 80
Print "C"
ELSE
Print "D"
[END OF IF]
Step 6: End

Flow Chart:

Source Code:
#include<stdio.h>
void main()
{
int marks;
27
printf("Enter your marks ");
scanf("%d",&marks);
if(marks<0 || marks>100)
{
printf("Wrong Entry");
}
else if(marks<60)
{
printf("Grade D");
}
else if(marks>=60 && marks<80)
{
printf("Grade C");
}
else if(marks>=80 && marks<90)
{
printf("Grade B");
}
else if(marks>=90 && marks<=100)
{
printf("Grade A");
}
else if(marks>=80 && marks<90)
}

Input:
92
Output:
Grade A
III. WAP that takes two operands and one operator from the user, perform the operation, and
prints the result by using switch statement.

Algorithm:
1. Start
2. Declare 4 variables - num1, num2, result, opt. The variables num1 and num2 are for storing
the values or operands, for storing the calculation result, and opt to take the operator as
inputs.
3. Take inputs of the operands, operator.
4. Use switch case or conditional statements to check the operator.
5. Display the result according to the operator.
6. End

Flow Chart:

28
Source Code:
#include <stdio.h>

int main() {

char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);

switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
29
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}

Input:
Enter an operator (+, -, *, /): +
Enter two operands: 23 10
Output:
23.0 + 10.0 = 33.0
.

Program #4

Objective:
I WAP to print the sum of all numbers up to a given number.
II WAP to find the factorial of a given number.
III WAP to print sum of even and odd numbers from 1 to N numbers.
IV WAP to print the Fibonacci series.

Software required: Dev C++ /Tubo C / GCC

I. WAP to print the sum of all numbers up to a given number.

Algorithm:
 start
 Include the required Header files using include keyword.
 Initialize the required variables.
 Create a variable sum = 0
 Run for loop in iterations of (I) in between [a, b]
o In each iteration add I to sum value
o Print sum value
 end

30
Flow Chart:

Source Code:
#include <stdio.h>
int main() {
int n, i, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &n);

for (i = 1; i <= n; ++i) {


sum += i;
}

printf("Sum = %d", sum);


return 0;
}

Input:
Enter a positive integer: 100

Output:
Sum = 5050

II. WAP to find the factorial of a given number.


Algorithm:

31
 Start program
 Ask the user to enter an integer to find the factorial
 Read the integer and assign it to a variable
 From the value of the integer up to 1, multiply each digit and update the final value
 The final value at the end of all the multiplication till 1 is the factorial
 End program

Flow Chart:

Source Code:
#include <stdio.h>
int main() {
int n, i;
unsigned long 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 = %llu", n, fact);
}

return 0;
}

Input:
Enter an integer: 5

32
Output:
Factorial of 5 = 120

III. WAP to print sum of even and odd numbers from 1 to N numbers.

Algorithm:
1. Start
2. Take the number N upto which we have to find the sum as input.
3. Using for loop take the elements one by one from 1 to N.
4. Using if,else statements separate the element as even or odd.
5. Add the even and odd numbers separately and store it in different variables.
6. Print the sum separately and exit.

Flow Chart:

Source Code:

#include <stdio.h>

void main()
{
int i, num, odd_sum = 0, even_sum = 0;

33
printf("Enter the value of num\n");
scanf("%d", &num);
for (i = 1; i <= num; i++)
{
if (i % 2 == 0)
even_sum = even_sum + i;
else
odd_sum = odd_sum + i;
}
printf("Sum of all odd numbers = %d\n", odd_sum);
printf("Sum of all even numbers = %d\n", even_sum);
}

Input:
Enter the value of num
10
Output:
Sum of all odd numbers = 25
Sum of all even numbers = 30

IV. WAP to print the Fibonacci series.

Algorithm:
START
Step 1 → Take integer variable A, B, C
Step 2 → Set A = 0, B = 0
Step 3 → DISPLAY A, B
Step 4 → C = A + B
Step 5 → DISPLAY C
Step 6 → Set A = B, B = C
Step 7 → REPEAT from 4 - 6, for n times
STOP

Flow Chart:

34
Source Code:
#include <stdio.h>
int main() {

int i, n;

// initialize first and second terms


int t1 = 0, t2 = 1;

// initialize the next term (3rd term)


int nextTerm = t1 + t2;

// get no. of terms from user


printf("Enter the number of terms: ");
scanf("%d", &n);

// print the first two terms t1 and t2


printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms


for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
35
}

return 0;
}
Input:
Enter the number of terms: 10

Output:
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

Program #5

Objective:
I WAP to check whether the entered number is prime or not.
II WAP to find the sum of digits of the entered number.

III WAP to find the reverse of a number using function.


IV WAP to print Armstrong numbers from 1 to 100.
V WAP to convert binary number into decimal number and vice versa.

Software required: Dev C++ /Tubo C / GCC

I. WAP to check whether the entered number is prime or not.

Algorithm:
STEP 1: start, Take num as input.
STEP 2: Initialize a variable temp to 1.
STEP 3: Iterate a “for” loop from 2 to sqrt(num).
36
STEP 4: If num is divisible by loop iterator, then update temp value to 0.
STEP 5: If the temp is equal to 1,
Return “Num IS PRIME”.
Else,
Return “Num IS NOT PRIME”.
STEP 6: Stop

Flow Chart:

Source Code:
#include <stdio.h>

int main() {

int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

if (n == 0 || n == 1)
flag = 1;

for (i = 2; i <= n / 2; ++i)


{

if (n % i == 0) {
flag = 1;
break;
}
37
}

// flag is 0 for prime numbers


if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);

return 0;
}
Input:
Enter a positive integer: 7
Output:
7 is not prime number

II. WAP to find the sum of digits of the entered number.

Algorithm:
o Step 1: start, Get number by user
o Step 2: Get the modulus/remainder of the number
o Step 3: sum the remainder of the number
o Step 4: Divide the number by 10
o Step 5: Repeat the step 2 while number is greater than 0.
o Step 6: Stop

Flow Chart:

Source Code:
#include<stdio.h>
int main()
{
int n,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
return 0;
}

38
Input:
Enter a number:654

Output:
Sum is: 15

III. WAP to find the reverse of a number using function.

Algorithm:
1. Start
2. Take the input from the user
3. Perform remainder = number % 10
4. Print remainder
5. Perform number/10
6. Repeat steps 2 and 4 until the input number cannot be further divided.
7. Stop

Flow Chart:

Source Code:
#include<stdio.h>
int main()
{
int n, reverse=0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{

39
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number: %d",reverse);
return 0;
}

Input:
Enter a number: 123
Output:
Reversed Number: 321

IV. WAP to print Armstrong numbers from 1 to 100.

Source Code:
#include <math.h>
#include <stdio.h>

int main()
{
int i, sum, num, count = 0;
printf("All Armstrong number between 1 and 100 are:\n");

// This loop will run for 1 to 100


for (i = 1; i <= 100; i++) {
num = i;
// Count number of digits.
while (num != 0) {
num /= 10;
count++;
}
num = i;
sum = pow(num % 10, count)
+ pow((num % 100 - num % 10) / 10, count)
+ pow((num % 1000 - num % 100) / 100, count);
// Check for Armstrong Number
if (sum == i) {
printf("%d ", i);
}
count = 0;
}
}
Input:

40
No Input
Output:
123456789

V. WAP to convert binary number into decimal number and vice versa.

Source Code:
include <stdio.h>
#include <math.h>
int convert(long long);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}

int convert(long long n) {


int dec = 0, i = 0, rem;
while (n != 0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}

Input:
Enter a binary number: 1101

Output:
1101 in binary = 13 in decimal

41
Program #6

Objective:
I WAP that simply takes elements of the array from the user and finds the sum of
these elements.
II WAP that inputs two arrays and saves sum of corresponding elements of these
arrays in a third array and prints them.

III WAP to find the minimum and maximum element of the array.
IV WAP to search an element in a array using Linear Search.

V WAP to add and multiply two matrices of order nxn.

VI WAP that finds the sum of diagonal elements of a mxn matrix.

Software required: Dev C++ /Tubo C / GCC

I. WAP that simply takes elements of the array from the user and finds the sum of these
elements.

Source Code:
#include <stdio.h>
int main()

42
{
int a[100];
int i, n, sum = 0;
printf("\n\nFind sum of all elements of array:\n");
printf("--------------------------------------\n");
printf("Input the number of elements to be stored in the array :");
scanf("%d", &n);
printf("Input %d elements in the array :\n", n);
for (i = 0; i < n; i++)
{
printf("element - %d : ", i);
scanf("%d", &a[i]);
}

for (i = 0; i < n; i++)


{
sum += a[i];
}
printf("Sum of all elements stored in the array is : %d\n\n", sum);
return 0;
}
Input:
Find sum of all elements of array:
--------------------------------------
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 2
element - 1 : 5
element - 2 : 8

Output:
Sum of all elements stored in the array is : 15

II. WAP that inputs two arrays and saves sum of corresponding elements of these arrays in
a third array and prints them.

Source Code:
#include <stdio.h>

void main()
{
int a[10], b[10], c[10], n, i;

43
printf("Enter the number of elements:\t");
scanf("%d", &n);

printf("Enter %d elements for array 1:\n", n);

for (i = 0; i < n; i++)


scanf("%d", &a[i]);

printf("Enter %d elements for array 2:\n", n);

for (i = 0; i < n; i++)


scanf("%d", &b[i]);

for (i = 0; i < n; i++)


c[i] = a[i] + b[i];

printf("Sum of two array elements are:\n");

for (i = 0; i < n; i++)


printf("%d\n", c[i]);
}

Input:
Enter the number of elements: 5
Enter 5 elements for array 1:
93
37
71
03
17
Enter 5 elements for array 2:
29
84
28
75
63

Output:
Sum of two array elements are:
122
121
99
78
80

44
III. WAP to find the minimum and maximum element of the array.

Source Code:
#include<stdio.h>
void check(int arr[], int n)
{
int small, large;
small = large = arr[0];
for(int i = 1; i < n ;i++)
{
if(arr[i] < small)
small = arr[i];
if(arr[i] > large)
large = arr[i];
}
printf("Smallest Number: %d\n",small);
printf("Largest Number: %d", large);
}
int main()
{
int n;
printf("Enter the size of array: ");
scanf("%d",&n);
int arr[n];
printf("Enter the elements of array: ");
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
check(arr, n);
return 0;
}

Input:
Enter the size of array: 5
Enter the elements: 10 20 5 40 30

Output:
Smallest Number: 5
Largest Number: 40

45
IV. WAP to search an element in a array using Linear Search.

Source Code:
#include <stdio.h>
void main()
{
int num;
int i, key, element_found = 0;
printf("Enter number of elements you would like to take as input: ");
scanf("%d", &num);
int arr[num];
printf("\nEnter all the elements of your choice:");
for (i = 0; i < num; i++)
{
scanf("%d", &arr[i]);
}
printf("\nEnter the key element that you would like to be searched: ");
scanf("%d", &key);
/* Linear search starts */
for (i = 0; i < num ; i++)
{
if (key == arr[i] )
{
element_found = 1;
break;
}
}
if (element_found == 1)
printf("we got the element at index %d",i+1);
else
printf("we haven’t got element at any index in the array\n");
}

Input:
Enter number of elements you would like to take as input: 8
Enter all the elements of your choice:
10, 50, 30, 70, 80, 20, 90, 40
Enter the key element that you would like to be searched: 52
Output:
we haven’t got element at any index in the array

V. WAP to add and multiply two matrices of order nxn.

46
Source Code:
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}

printf("multiply of the matrix=\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)

47
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}

Input:
enter the number of row=3
enter the number of column=3
enter the first matrix element=
111
222
333
enter the second matrix element=
111
222
333

Output:
multiply of the matrix=
666
12 12 12
18 18 18

VI. WAP that finds the sum of diagonal elements of a mxn matrix.

Source Code:
#include <stdio.h>

int main()
{

int i, j, m = 3, n = 3, a = 0, sum = 0;

// input matrix
int matrix[3][3]
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

// if both rows and columns are equal then it is


// possible to calculate diagonal sum
if (m == n) {

48
// printing the input matrix
printf("The matrix is \n");

// iterates number of rows


for (i = 0; i < m; ++i) {

// iterates number of columns


for (j = 0; j < n; ++j) {
printf(" %d", matrix[i][j]);
}
printf("\n");
}
for (i = 0; i < m; ++i) {

// calculating the main diagonal sum


sum = sum + matrix[i][i];

// calculating the off diagonal sum


a = a + matrix[i][m - i - 1];
}

// printing the result


printf("\nMain diagonal elements sum is = %d\n", sum);
printf("Off-diagonal elements sum is = %d\n", a);
}
else
// if both rows and columns are not equal then it is
// not possible to calculate the sum
printf("not a square matrix\n");
return 0;
}
Input:
123
456
789

Output:
The matrix is
123
456
789

Main diagonal elements sum is = 15


Off-diagonal elements sum is = 15

49
Program #7

Objective:
I WAP to swap two elements using the concept of pointers.

II WAP to sort the elements of the array in ascending order using Bubble Sort
technique and pointer.

Software required: Dev C++ /Tubo C / GCC

I. WAP to swap two elements using the concept of pointers.

Source Code:
#include <stdio.h>
void swap(int *, int *);
int main()
{
50
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}

void swap (int *a, int *b)


{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b);
}

Input:
a = 10;
b = 20;

Output:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10

II. WAP to sort the elements of the array in ascending order using Bubble Sort technique
and pointer.

Source Code:
#include <stdio.h>

void swap(int *ip, int *jp)


{
int temp = *ip;
*ip = *jp;
*jp = temp;
}

void bubbleSort(int *array, int n)


{
for(int l = 0; l < n-1; l++)
{
for(int m = 0; m < n-l-1; m++)
{

51
if(*(array+m) > *(array+m+1))
{
swap((array+m), (array+m+1));
}
}
}
}

void printArray(int *array, int size)


{
for (int l = 0; l < size; l++)
printf("%d ", *(array + l));
printf("\n");
}

int main() {
int array[] = {64, 47, 25, 28, 22, 17, 90};
int n = sizeof(array)/sizeof(array[0]);
bubbleSort(array, n);
printf("Sorted array: \n");
printArray(array, n);
return 0;
}

Input:
64, 47, 25, 28, 22, 17, 90
Output:
Sorted array:

17 22 25 28 47 64 90

52
Program #8

Objective:
I WAP to compare the contents of two files and determine whether they are same or
not.
II WAP to check whether a given word exists in a file or not. If yes then find the
number of times it occurs

Software required: Dev C++ /Tubo C / GCC

I. WAP to compare the contents of two files and determine whether they are same or not.

Source Code:

#include <stdio.h>
#include <stdlib.h>
53
int compareFile(FILE * fPtr1, FILE * fPtr2, int * line, int * col);

int main()
{
FILE * fPtr1;
FILE * fPtr2;
char path1[100];
char path2[100];
int diff;
int line, col;
printf("Enter path of first file: ");
scanf("%s", path1);
printf("Enter path of second file: ");
scanf("%s", path2);
fPtr1 = fopen(path1, "r");
fPtr2 = fopen(path2, "r");
if (fPtr1 == NULL || fPtr2 == NULL)
{
printf("\nUnable to open file.\n");
printf("Please check whether file exists and you have read privilege.\n");
exit(EXIT_FAILURE);
}
diff = compareFile(fPtr1, fPtr2, &line, &col);

if (diff == 0)
{
printf("\nBoth files are equal.");
}
else
{
printf("\nFiles are not equal.\n");
printf("Line: %d, col: %d\n", line, col);
}

fclose(fPtr1);
fclose(fPtr2);

return 0;
}

int compareFile(FILE * fPtr1, FILE * fPtr2, int * line, int * col)


{

54
char ch1, ch2;

*line = 1;
*col = 0;

do
{
ch1 = fgetc(fPtr1);
ch2 = fgetc(fPtr2);

if (ch1 == '\n')
{
*line += 1;
*col = 0;
}

if (ch1 != ch2)
return -1;

*col += 1;

} while (ch1 != EOF && ch2 != EOF);

if (ch1 == EOF && ch2 == EOF)


return 0;
else
return -1;
}
Input:
File contents of <strong>data\compare1.txt</strong>
Learn C programming at Codeforwin.
Working with files and directories.
File contents of <strong>data\compare2.txt</strong>
Learn C programming at Codeforwin.
Working with array and pointers.

Output:
Files are not equal
Line: 2 , col: 14

II. WAP to check whether a given word exists in a file or not. If yes then find the number of
times it occurs.

Source Code:

55
#include<stdio.h>
void main()
{

FILE* filePointer;
int wordExist=0;
int bufferLength = 255;
char search[100];
printf("Enter word to be search=");
scanf("%s",search);
char line[bufferLength];
filePointer = fopen("D:\\file.txt", "r");
while(fgets(line, bufferLength, filePointer))
{
char *ptr = strstr(line, search);
if (ptr != NULL)
{
wordExist=1;
break;
}
}
fclose(filePointer);
if (wordExist==1)
{
printf("Word exists.");
}
else
{
printf("Word doesn't exist.");
}
}
Input:
File contain lines:
how are you.
this is a boy
i am good

Output:

Enter word to be search=is


Word exists.

56
Lab Viva Questions with Answers

Q 1: What is an algorithm?
Ans: An algorithm is a step-by-step procedure or a well-defined set of instructions that is
designed to solve a specific problem or perform a particular task. It is a systematic approach
to problem-solving and provides a clear and unambiguous sequence of operations that can
be executed by a computer or a human to achieve the desired result.
Q 2: What are the characteristics of a good algorithm?
Ans: A good algorithm possesses the following characteristics:
1. Correctness: The algorithm should produce the correct output for all valid inputs.
2. Efficiency: It should be able to solve the problem with reasonable resource utilization,
such as time and memory.
3. Finiteness: The algorithm must terminate after a finite number of steps.
4. Input: The algorithm should take zero or more inputs, which are well-defined.
5. Output: It should produce at least one output, which is related to the input.
6. Unambiguous: Each step in the algorithm should be clear and unambiguous, leaving no
room for ambiguity or misinterpretation.
7. Determinism: The algorithm should behave predictably and consistently for a given input.
8. Generality: It should be applicable to a wide range of problem instances within its domain.
Q 3: What is the difference between an algorithm and a program?
Ans: An algorithm is a high-level, abstract description of the steps needed to solve a
problem, whereas a program is a concrete implementation of those steps in a specific
programming language to be executed by a computer. An algorithm is a set of instructions
that can be executed by a human or a computer, while a program is a sequence of coded
instructions written in a programming language that is executed by a computer.
Q 4: What are tokens in C?

Ans: The Tokens are identifiers or the smallest single unit in a program that is meaningful to
the compiler. In C we have the following tokens:
 Keywords: Predefined or reserved words in the C programming language. Every keyword
is meant to perform a specific task in a program. C Programming language supports 32
keywords.
 Identifiers: Identifiers are user-defined names that consist of an arbitrarily long sequence
of digits or letters with either a letter or the underscore (_) as a first Character. Identifier
names can’t be equal to any reserved keywords in the C programming language. There
are a set of rules which a programmer must follow in order to name an identifier in C.
 Constants: Constants are normal variables that cannot be modified in the program once
they are defined. Constants refer to a fixed value. They are also referred to as literals.
 Strings: Strings in C are an array of characters that end with a null character (‘\0). Null
character indicates the end of the string;
 Special Symbols: Some special symbols in C have some special meaning and thus, they
cannot be used for any other purpose in the program. # = {} () , * ; [] are the special
symbols in C programming language.
 Operators: Symbols that trigger an action when they are applied to any variable or any
57
other object. Unary, Binary, and ternary operators are used in the C Programming
language.

Q 5: What do you mean by the scope of the variable?


Ans: The Scope in a programming language is the block or a region where a defined variable
will have its existence and beyond that region, the variable is automatically destroyed. Every
variable has its defined scope. In simple terms, the scope of a variable is equal to its life in
the program. The variable can be declared in three places These are:
 Local Variables: Inside a given function or a block
 Global Variables: Out of all functions globally inside the program.
 Formal Parameters: In-function parameters only.

Q 6: What are preprocessor directives in C?


Ans: In C preprocessor directives are considered the built-in predefined functions or macros
that act as a directive to the compiler and are executed before the program execution. There
are multiple steps involved in writing and executing a program in C. Main types of
Preprocessor Directives are Macros, File Inclusion, Conditional Compilation, and Other
directives like #undef, #pragma, etc. .

Q7. How can we store a negative integer?

Ans: To store a negative integer, we need to follow the following steps. Calculate the two’s
complement of the same positive integer.
Eg: 1011 (-5)
Step-1 − One’s complement of 5: 1010
Step-2 − Add 1 to above, giving 1011, which is -5

Q8. Differentiate between Actual Parameters and Formal Parameters.

Ans: The Parameters which are sent from main function to the subdivided function are called
as Actual Parameters and the parameters which are declared a the Subdivided function end
are called as Formal Parameters.

Q9. Can a C program be compiled or executed in the absence of a main()?


Ans: The program will be compiled but will not be executed. To execute any C program,
main() is required.

Q10. What do you mean by a Nested Structure?


Ans: When a data member of one structure is referred by the data member of another
function, then the structure is called a Nested Structure.

Q11. What is a C Token?


Ans: Keywords, Constants, Special Symbols, Strings, Operators, Identifiers used in C program
are referred to as C Tokens.

58
Q12. What is Preprocessor?
Ans: A Preprocessor Directive is considered as a built-in predefined function or macro that
acts as a directive to the compiler and it gets executed before the actual C Program is
executed.

Q13. Why is C called the Mother of all Languages?


Ans: C introduced many core concepts and data structures like arrays, lists, functions,
strings, etc. Many languages designed after C are designed on the basis of C Language.
Hence, it is considered as the mother of all languages.

Q14. Mention the features of C Programming Language.


Ans:

Q15. What is the purpose of printf() and scanf() in C Program?


Ans: printf() is used to print the values on the screen. To print certain values, and on the
other hand, scanf() is used to scan the values. We need an appropriate datatype format
specifier for both printing and scanning purposes. For example,
 %d: It is a datatype format specifier used to print and scan an integer value.
 %s: It is a datatype format specifier used to print and scan a string.
 %c: It is a datatype format specifier used to display and scan a character value.
 %f: It is a datatype format specifier used to display and scan a float value.

Q16. What is an array?


Ans. The array is a simple data structure that stores multiple elements of the same datatype
in a reserved and sequential manner. There are three types of arrays, namely,
 One Dimensional Array
 Two Dimensional Array
 Multi-Dimensional Array

59
Q17. What is /0 character?
Ans: The Symbol mentioned is called a Null Character. It is considered as the terminating
character used in strings to notify the end of the string to the compiler.

Q18. What is the main difference between the Compiler and the Interpreter?
Ans: Compiler is used in C Language and it translates the complete code into the Machine
Code in one shot. On the other hand, Interpreter is used in Java Programming Langauge and
other high-end programming languages. It is designed to compile code in line by line fashion.

Q19. Can I use int datatype to store 32768 value?


Ans: No, Integer datatype will support the range between -32768 and 32767. Any value
exceeding that will not be stored. We can either use float or long int.

Q20. How is a Function declared in C Language?


Ans: A function in C language is declared as follows,
1 return_type function_name(formal parameter list)
2 {
3 Function_Body;
4 }

Q21. What is Dynamic Memory allocation? Mention the syntax.


Ans: Dynamic Memory Allocation is the process of allocating memory to the program and its
variables in runtime. Dynamic Memory Allocation process involves three functions for
allocating memory and one function to free the used memory.
malloc() – Allocates memory
Syntax:
1 ptr = (cast-type*) malloc(byte-size);
calloc() – Allocates memory
Syntax:
1 ptr = (cast-type*)calloc(n, element-size);
realloc() – Resize Allocates memory
free() – Deallocates the used memory

Q22. What do you mean by Dangling Pointer Variable in C Programming?


Ans: A Pointer in C Programming is used to point the memory location of an existing variable.
In case if that particular variable is deleted and the Pointer is still pointing to the same
memory location, then that particular pointer variable is called as a Dangling Pointer
Variable.

Q23. Where can we not use &(address operator in C)?


Ans: We cannot use & on constants and on a variable which is declared using the register
storage class.

60

You might also like