Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab Manual CP Lab Final

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 80

JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

LAB MANUAL

Lab Name : Computer Programming Lab

Lab Code : (1FY3-24/2FY3-24)

Branch : Common to all

Year : First Year

Department of First Year


Jaipur Engineering College and Research Centre, Jaipur
(Affiliated to RTU, Kota)

1
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

CP LAB MANUAL
INDEX

Item Page No.

Vision and Mission of the Institute 3

Program Outcomes (POs) 4

RTU Scheme, Syllabus with List of Experiments 5-8

Course Outcomes 9

CO/PO Mapping 9

Introduction about Lab: Brief Description, Applications and Industry use 10-11

Instruction Sheet 12-13


Experiment-1: Write C Programs To learn about the C Library, Pre-processor
14-24
directive, Input-output statement, Data types & Variables.
Experiment-2: Write C Programs to understand Conditional Statements (if-else,
25-36
Nested & Ladder if-else, Switch-Case).
Experiment-3: Write C Programs to learn Iterative Statements like while and do-
37-42
while loops.
Experiment-4: Write C Programs to understand for loops for Iterative Statements. 43-47
Experiment-5: Write C Programs to learn about Array (1-D & 2-D Array) and
48-50
String operations.
Experiment-6: Write C Programs to understand Sorting and Searching using
51-53
array.
Experiment-7: Write C Programs to learn Functions and Recursive Functions. 54-58

Experiment-8: Write C Programs to understand Structure and Union operation. 59-62

Experiment-9: Write C Programs to learn Pointer operations. 63-65

Experiment-10: Write C Programs to understand File Handling operations. 66-68


Experiment-11:Write C Programs to input data through Command Line
69-70
Argument.
Viva Questions 71-72

2
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

Some Special Programs in C

Vision of the Institute

To become a renowned centre of outcome- based learning, and work towards academic,
professional, cultural and social enrichment of the lives of individuals and communities.

Mission of the Institute

1. Identify, based on informed perception of Indian, regional and global needs, the areas
of focus and provide platform to gain knowledge and solutions.

2. Focus on evaluation of learning outcomes and motivate students to inculcate research


aptitude by project-based learning.

3. Offer opportunities for interaction between academia and industry.

4. Develop human potential to its fullest extent so that intellectually capable and
imaginatively gifted leaders may emerge in a range of professions.

3
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

PROGRAM OUTCOMES

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, 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 using.
9. Individual and Team Work: 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
changes needed.

4
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

5
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

6
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

7
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

JECRC
B. Tech First Year (Common to All)
Name of Laboratory: Computer Programming Lab
Subject Code: 1FY3-24/2FY3-24
Year/Semester - I/I &II
Lab Course outcomes:
Students will be able to:
CO1: Identify and Analyze the Input /Output operation, Decision Making statements and
Looping.
CO2: Analyze and implement Arrays, Functions, Pointers and Dynamic Memory Allocation.
CO3: Apply Structure, Union and Data Handling through files in ‘C’ Programming Language.
List of Experiment:

Experiment
Objective CO
No.
Write C Programs To learn about the C Library, Pre-processor
CO1
1. directive, Input-output statement, Data types & Variables.
Write C Programs to understand Conditional Statements (if-else,
CO1
2. Nested & Ladder if-else, Switch-Case).
Write C Programs to learn Iterative Statements like while and do-while
CO1
3. loops.

4. Write C Programs to understand for loops for Iterative Statements. CO1

Write C Programs to learn about Array (1-D & 2-D Array) and String
CO2
5. operations.

Write C Programs to understand Sorting and Searching using array. CO2


6.

Write C Programs to learn Functions and Recursive Functions. CO2


7.

Write C Programs to understand Structure and Union operation. CO3


8.

9. Write C Programs to learn Pointer operations. CO2

10. Write C Programs to understand File Handling operations. CO3

11. Write C Programs to input data through Command Line argument. CO3

8
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

Course Outcomes

Subject – Computer Programming Lab Code – 1FY3-24/2FY3-24


Branch – All Semester–I& II
Upon successful completion of this course students will be able to:

CO1: Identify and analyze the input /output operation, decision making statements and looping.

CO2: Analyze and implement arrays, functions, pointers and dynamic memory allocation.

CO3: Apply structure, union and data handling through files in ‘C’ Programming Language.

Mapping of CO & PO
Conduct Invest. of Complex Problems

Project Management and Finance


Environment and Sustainability
Design/Development of Solution

Individual and Team Work


The Engineer and Society

Mapping of
Engineering Knowledge

Course Outcomes
& POs
Modern Tool Usage

Life-long Learning
Problem Analysis

Communication
Ethics

PO1 PO1 PO1


PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9
0 1 2
CO-1 1 1 1 1 0 1 NA NA 1 0 NA 1
CO-2 2 1 2 0 1 1 NA NA 1 1 NA 1
CO-3 3 2 2 0 1 1 NA NA 1 1 NA 0

2 1.33 1.67 0.33 0.67 1 NA NA 1 0.67 NA 0.67

Nil = 0 L - Low =1 M – Medium=2 H – High=3

9
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

Introduction about Lab


Computer Programming Lab: C is a general-purpose, procedural, imperative computer
programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone
Laboratories to develop the UNIX operating system. C is the most widely used computer
language. It keeps fluctuating at number one scale of popularity along with Java programming
language, which is also equally popular and most widely used among modern software
programmers.

Uses of C language:

• Database Systems

• Language Interpreters

• Compilers and Assemblers

• Operating Systems

• Network Drivers

• Word Processors

Features of C language:

• C is robust language with rich set of built-in functions and operators

• Programs written in C are efficient and fast.

• C is highly portable.

• C is basically a collection of C library functions.

• C is easily extensible.

C Preprocessor directive:

• Before a C program is compiled in a compiler, source code is processed by a program called


preprocessor. This process is called pre-processing.
• Commands used in pre-processor are called pre- processor directives and they begin with
“#” symbol.

10
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

11
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

INSTRUCTIONS SHEET
DO’s
1. Please switch off the Mobile/Cell phone before entering in the lab.
2. Enter in the Lab with complete preparation for programming.
3. Check whether all peripherals are available at your desktop before
proceeding for the program.

4. Intimate the lab in charge whenever you are in compatible in using


the system or in case software get corrupted/infected by virus.

5. Arrange all the devices / peripheral and seats before leaving the lab.
6. Properly shut down the system before leaving the lab.
7. Keep the bag outside from the lab.
8. Enter in the lab on schedule time and leave at proper time after permission.
9. Maintain the decorum of the lab.
10. Utilize lab hours in the corresponding experiment.
11. Get your external storage device checked by lab in-charge before using it in
the lab.
Don’ts
1. No one is allowed to bring storage devices like external hard disk in the lab
without permission.
2. Don’t mishandle the system.
3. Don’t leave the system on standing for long time (more than 15 min.)
4. Don’t bring any external material in the lab.
5. Don’t make noise in the lab.
6. Don’t bring the mobile in the lab. If extremely necessary then keep ringers
off.
7. Don’t enter in the lab without permission of lab in-charge.
8. Don’t delete or make any modification in system files.
9. Don’t carry any lab equipments outside the lab.
10. Avoid stepping on electric wires or any other computer cables.

12
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

BEFORE ENTERING IN THE LAB

1. All the students are supposed to prepare the theory regarding the next experiment.

2. Students are supposed to bring the practical file and the lab copy.

3. Previous practical should be written in the practical file.

4. Any student not following these instructions will be denied entry in the lab.

WHILE WORKING IN THE LAB

1. Adhere to experimental schedule as instructed by the lab in-charge.

2. Get the previously executed experiment signed by the instructor.

3. Get the output of the current experiment checked by the instructor in the lab copy.

4. Take responsibility of valuable accessories.

5. If anyone is caught carrying any equipment of the lab outside without permission, they
will face strict disciplinary action.

13
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

CP LAB EXPERIMENT: -1

OBJECTIVES: Write C Programs To learn about the C Library, Pre-processor directive,


Input-output statement, Data types & Variables.

Program:-1 Write a C Program to print “HELLO” message.


#include <stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("HELLO");
getch();
}
OUTPUT
HELLO

Program:-2 Write a C Program to add any two numbers.


#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, sum;
clrscr();
printf("Enter first number");
scanf("%d",&a);
printf("Enter second number");
scanf("%d",&b);

14
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

sum = a+b;
printf("Sum of %d and %d is %d”, a,b,sum);
getch();
}
OUTPUT
Enter first number :10
Enter second number :20
The sum of 10 and 20 is 30

Program:-3 Write a C Program to calculate sum and average of five numbers.


#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c, d, e, sum;
float avg;
clrscr();
printf("Enter 5 numbers\n");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
sum=a+b+c+d+e;
avg = sum/5;
printf("\n sum is %d", sum);
printf("\n Average is %f", avg);
getch();
}
OUTPUT
Enter 5 numbers:
5
4

15
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

7
9
6
Sum is 31
Average is 6.2

Program:-4 Write a C Program to find area of rectangle of given rectangle and length and
breadth.
#include <stdio.h>
#include <conio.h>
void main()
{
int l, b, area;
clrscr();
printf("Enter the value of l and b \n");
scanf(“%d%d”, &l,&b);
area=l*b;
printf("The area of rectangle is %d", area);
getch();
}
OUTPUT
Enter the value of l and b
5
4
The area of rectangle is 20

Program:-5 Write a C Program to find perimeter of rectangle of given rectangle and


length and breadth.
#include <stdio.h>

16
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

#include <conio.h>
void main()
{
int l, b, perimeter;
clrscr();
printf("Enter the value of l and b \n");
scanf(“%d%d”, &l,&b);
perimeter =2*(l+b);
printf("The area of perimeter is %d", perimeter);
getch();
}
OUTPUT
Enter the value of l and b
5
6
The area of perimeter is 22

Program:-6 Write a C Program to calculate simple interest.


#include <stdio.h>
#include <conio.h>
void main()
{
int p, r, t;
int amt;
clrscr();
printf("Input Principle, Time & Rate of interest \n");
scanf("%d%d%d",&p,&t,&r);
amt=(p*r*t)/100;
printf("Simple Interest = %d",amt);

17
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

getch();
}
OUTPUT
Input Principle, Time & Rate of interest
500
5
2
Simple Interest = 50

Program:-7 Write a C Program to calculate area and circumference of circle.


#include <stdio.h>
#include <conio.h>
#define PIE 3.14
void main()
{
float r, c, area;
clrscr();
printf("Enter the radius for the circle \n");
scanf("%f",&r);
area =PIE*r*r;
printf("The area of circle is %f\n ",area);
c= 2*PIE*r;
printf("The circumference of circle is %f”,c);
getch();
}
OUTPUT
Enter the radius for the circle
5
The area of circle is 78.530000

18
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

The circumference of circle is 31.410000

Program:-8 Write a C Program to swap of two numbers without using third variable
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter value for a&b: ");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("After swapping value of a&b : %d %d",a,b);
getch();
}
OUTPUT
Enter value for a&b: 5
4
After swapping value of a&b : 4 5

Program:-9 Write a C Program to change a character in Uppercase to Lowercase and


Lowercase to Uppercase .
#include <stdio.h>
#include <conio.h>
int main ()
{
char u, l;

19
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

int ascii;
clrscr();
printf (" Enter the Upper Case Character: ");
scanf (" %c", &u);
ascii = u + 32;
printf (" %c character in Lower case is: %c", u, ascii);
printf (" \n Enter the Lower Case Character: ");
scanf (" %c", &l);
ascii = l - 32;
printf (" %c character in the Upper case is: %c", l, ascii);
getch();
return 0;
}
OUTPUT
Enter the Upper Case Character: K
K character in Lower case is: k
Enter the Lower Case Character: d
d character in the Upper case is: D

Program:- 10 Write a C Program to print ASCII value of a character.


#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character: ");
scanf("%c",&ch);
printf("\n ASCII value of %c=%d", ch, ch);

20
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

getch();
}
OUTPUT
Enter the Character:A
ASCII Value of A = 65

Program:-11 Write a program to input the temperature in degree, Fahrenheit and convert
it into degree Celsius.
#include<stdio.h>
#include<conio.h>
void main()
{
float F,C;
clrscr ();
printf("Enter temperature in degree Fahrenheit =");
scanf("%f",&F);
C=5*(F-32)/9;
printf("Hence temperature in degree Celsius will be %f", C);
getch();
}
OUTPUT
Enter temperature in degree Fahrenheit = 41
Hence temperature in degree Celsius will be 5.000000

Program:-12 Write a C Program to print the reverse of a two digit number.


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

21
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

int a;
clrscr();
printf("Enter a two digit number =");
scanf("%d",&a);
a=(a%10)*10+(a/10);
printf("the reverse number is %d",a);
getch();
}
OUTPUT
Enter the two digit number=45
The reverse number is 54

Program:-13 Mahesh’s basic salary is inputted through the keyboard. His Dearness
Allowance is 40% of basic salary , and House Rent Allowance is 20% of basic salary.
Write a C Program to calculate his Gross Salary.

[Example: Gross salary= Basic + DA+HRA]


#include <stdio.h>
#include <conio.h>
int main()
{
float bs,da,hra,gs;
clrscr();
printf("Enter basic salary:\n");
scanf("%f",&bs);
da=0.4*bs;
hra=0.2*bs;
gs=bs+da+hra;
printf("\nBasic salary:%f",bs);
printf("\nDearness Allowance:%f",da);

22
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

printf("\nHouse Rent:%f",hra);
printf("\nGross salary:%f",gs);
getch();
return 0;
}
OUTPUT
Enter basic salary:
500
Basic salary: 500.000000
Dearness Allowance: 200.000000
House Rent: 100.000000
Gross salary: 800.000000

CP LAB EXPERIMENT: -2

23
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

OBJECTIVES:Write C Programs to understand Conditional Statements (if-else, Nested &


Ladder if-else, Switch-Case).

Program:-14 Write a C Program to check whether a number is positive or negative.

#include <stdio.h>
#include <conio.h>
void main()
{
int a;
clrscr();
printf("Enter a number");
scanf("%d",&a);
if(a>=0)
{
printf("The given number is positive");
}
else
{
printf("The given number is negative");
}
getch();
}
OUTPUT
Enter a number :5
The given number is positive

Program:-15 Write a C Program to find maximum among three numbers.


#include <stdio.h>

#include <conio.h>

int main()

24
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

int a, b, c;

clrscr();

printf("Enter the three numbers \n");

scanf("%d%d%d",&a, & b, & c);

if(a>b)

if(a>c)

printf("%d is the greatest",a);

else

printf("%d is greatest",c);

else

if(b>c)

printf("%d is the greatest",b);

else

printf("%d is the greatest",c);

getch();

return 0;

OUTPUT

Enter the three numbers


5
7

25
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

4
7 is the greatest

Program:-16 The marks obtained by a students in five different subjects are input through
the keyboard. The students gets a division as per the following rules:

• Percentage above or equal to 60 – First Division

• Percentage between 50 and 59 – Second division

• Percentage between 40 and 49 – Third division

• Percentage less than 40 – Fail

Write a C Program to calculate the division obtained by the student.

#include <stdio.h>

#include <conio.h>

int main()

int marks;

clrscr();

printf("Enter your marks: ");

scanf("%d",&marks);

if(marks>=60)

printf("First Division!!");

else if(marks>=50 && marks<60)

printf("Second Division!!");

26
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

else if(marks>=40 && marks<50)

printf("Third Division!!");

else if(marks>=0 && marks<40)

printf("Fail!!!");

else

printf("Invalid marks!!");

getch();

return 0;

OUTPUT

Enter your marks: 89

First Division!!

Program:-17 Write a C Program to check a voter is eligible for voting or not? If age is
equal to or greater than 18 display “Eligible” other wise display “Not Eligible”.

#include <stdio.h>

#include<conio.h>

void main()

27
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

int age;

clrscr();

printf("Enter your age to check voting eligibility: ");

scanf("%d",&age);

if(age>0)

if(age>=18)

printf("Yes, You are eligible to vote!!");

else

printf("Sorry, You can not vote!!");

getch();

OUTPUT

Enter your age to check voting eligibility: 20

Yes, You are eligible to vote!!

Program:-18 Write a C program to check whether a character is alphabet or not.

#include <stdio.h>

#include<conio.h>

void main()

28
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

char c;

clrscr();

printf("Enter a character to check if it is an alphabet or not: ");

scanf("%c",&c);

if((c >= 97 && c <= 122) || (c >= 65 && c <= 90))

printf("The entered character %c is an Alphabet",c);

else

printf("The entered character %c is not an Alphabet",c);

getch();

OUTPUT

Enter a character to check if it is an alphabet or not: f

The entered character f is an Alphabet

Program:-19 Write a C program to input a year and find year is leap year or not.

#include <stdio.h>

#include <conio.h>

int main() {

int year;

clrscr();

printf("Enter a year: ");

scanf("%d", &year);

if (year % 400 == 0) {

printf("%d is a leap year.", year);

29
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

else if (year % 100 == 0) {

printf("%d is not a leap year.", year);

else if (year % 4 == 0) {

printf("%d is a leap year.", year);

else {

printf("%d is not a leap year.", year);

getch();

return 0;

OUTPUT

Enter the year you want to check if it is a leap year: 2013

The year you entered is not a leap year!!

Program:-20 Write a program that takes a number between 1 to 9 as input, Program


prints input number in words as output.

#include <stdio.h>

#include<conio.h>

void main()

int n;

30
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

clrscr();

printf(" Entent no. \n");

scanf("%d",&n);

switch (n)

case 1:printf("One");

break;

case 2:printf("Two");

break;

case 3:printf("Three");

break;

case 4:printf("Four");

break;

case 5:printf("Five");

break;

case 6:printf("Six");

break;

case 7:printf("Seven");

break;

case 8:printf("Eight");

break;

case 9:printf("Nine");

break;

default:

printf(“Invalid Input”);

31
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

break;

getch();

OUTPUT

Four.

Program:-21 Write a program for Simple Calculator using switch Statement.

#include <stdio.h>

#include <conio.h>

int main() {

char op;

clrscr();

float first, second;

printf("Enter an operator (+, -, *, /): ");

scanf("%c", &op);

printf("Enter two operands: ");

scanf("%f %f", &first, &second);

switch (op) {

case '+':

printf("%f + %f = %f", first, second, first + second);

break;

case '-':

printf("%f - %f = %f", first, second, first - second);

32
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

break;

case '*':

printf("%f * %f = %f", first, second, first * second);

break;

case '/':

printf("%f / %f = %f", first, second, first / second);

break;

default:

printf("Error! operator is not correct");

getch();

return 0;

OUTPUT

Enter an operator (+, -, *, /): *

Enter two operands: 2

2.000000 * 5.000000 = 10.000000

Program:-22 Write a C program to input electricity unit charges and calculate total
electricity bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill

33
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

#include <stdio.h>

#include<conio.h>

void main()

float units;

float amt, ta, sa;

clrscr();

printf("Enter the electricity usage in Units: ");

scanf("%f", &units);

if(units <= 50)

amt = units * 0.50;

else if(units <= 150)

amt = 50 * 0.50 + ((units-50) * 0.75);

else if(units <= 250)

amt = (50 * 0.50) + (100 * 0.75) + ((units-150) * 1.20);

else

amt = (50 * 0.50) + (100 * 0.75) + (100 * 1.20) + ((units-250) * 1.50);

34
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

sa = 0.20 * amt;

ta = sa + amt;

printf("The final amount electricity bill: %f",ta);

getch();

OUTPUT

Enter the electricity usage in Units: 389

The final amount electricity bill: 514.200012

Program:-23 Write a C program to check whether the triangle is equilateral, isosceles or


scalene triangle.

#include <stdio.h>

#include<conio.h>

void main()

int s1,s2,s3;

clrscr();

printf("Enter the length of the first side of the triangle: ");

scanf("%d", &s1);

printf("Enter the length of the second side of the triangle: ");

scanf("%d", &s2);

printf("Enter the length of the third side of the triangle: ");

scanf("%d", &s3);

if(((s1 + s2) <= s3) || ((s2 + s3) <= s1) || ((s1 + s3) <= s2))

35
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

printf("The triangle of entered sides is not a valid triangle.");

else

if(s1 == s2 == s3)

printf("This is an equilateral triangle.");

else if(s1 == s2 || s2 == s3 || s1 == s3)

printf("This is an isoceles triangle.");

else

printf("This is a scalene triangle.");

getch();

OUTPUT

Enter the length of the first side of the triangle: 2

Enter the length of the second side of the triangle: 3

Enter the length of the third side of the triangle: 2

This is an isosceles triangle.

36
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

CP LAB EXPERIMENT:-3

OBJECTIVES: Write C Programs to learn Iterative Statements like While and Do-while
Loops.

Program:-24 Write a C Program to print 1 to n numbers.


#include <stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("Enter the Value of n:");
scanf("%d",&n);
printf("All Numbers from 1 to %d :\n",n);
i=1;
while(i<=n)
{
printf(" %d \t",i);
i++;
}
getch();
}
OUTPUT
Enter the Value of n: 10
All numbers from 1 to 10:
1 2 3 4 5 6 7 8 9 10

Program:-25 Write a C Program to calculate sum and average first n integer numbers.
#include <stdio.h>

37
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

#include<conio.h>
void main()
{
int i,sum=0,n;
float avg;
clrscr();
printf("Please Enter How many Number you want: \n");

scanf("%d",&n);

i=1;
while(i<=n)
{
sum=sum+i;
i++;
}
avg= sum/n;
printf("The Sum of first n integer=%d \n",sum);
printf("The Average of first n integer = %f",avg);
getch();
}
OUTPUT
Please Enter How many Number you want:
10
The Sum of first n integer=55
The Average of first n integer = 5.000000

Program:-26 Write a C Program to print 20 horizontal asterisks (*).


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

38
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

int i=1;
clrscr();
while(i<=20)
{
printf(" * ");
i++;
}
getch();
}
OUTPUT
* * * * * * * * * * * * * * * * * * * *

Program:-27 Write a C Program to generate a table of any given input number.


#include <stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("Enter the number to generate its table ");
scanf("%d",&n);
i=1;
while(i<=10)
{
printf("\n %d*%d=%d",n,i,n*i);
i++;
}
getch();
}

39
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

OUTPUT
Enter the number to generate its table 4
4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
4*10=40

Program:-28 Write a C Program to print the reverse of a number.


#include <stdio.h>
#include <conio.h>
int main() {
int num, revNum = 0, rem;
clrscr();
printf("Enter an integer: ");
scanf("%d", &num);
while(num != 0) {
rem = num % 10;
revNum = revNum * 10 + rem;
num /= 10;
}
printf("Reversed Number = %d", revNum);
getch();
return 0;

40
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

}
OUTPUT
Enter an integer: 263548
Reversed Number = 845362

Program:-29 Write a C Program to check whether the number is palindrome or not.

#include <stdio.h>
#include<conio.h>
void main()
{
long int n, reverse=0, rem,temp;
clrscr();
printf("Enter an integer: ");
scanf("%ld", &n);
temp=n;
while(temp!=0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp/=10;
}
if(reverse==n)
printf("%ld is a palindrome.",n);
else
printf("%ld is not a palindrome.",n);
getch();
}
OUTPUT

41
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

Enter the integer 123


Not a palindrome

Program:-30 Write a C Program to check whether the three digite number is Armstrong
or not.

#include <stdio.h>

#include<conio.h>
int main()
{
int n, n1, rem, num=0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num==n)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
getch();
}
OUTPUT
Enter a positive number 153

42
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

153 is an Armstrong number

Program:-31 Write a program to find sum of digits of a given number .


#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,m;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
getch();
}
OUTPUT
Enter a number: 2356
Sum is=16

43
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

CP LAB EXPERIMENT:-4

OBJECTIVES: Write C Programs to understand for loops for Iterative Statements.

Program:-32 Write a C Program to print following output


*
***
*****
*******
*********
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,rows,space;
clrscr();
printf("Enter the number of lines:");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for (space=1;space<=rows-i;space++)
{
printf (" ");
}
for(j=0;j!=2*i-1;j++)
{
printf("*");
}
printf("\n");
}
getch();

44
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

}
OUTPUT
Enter the number of lines:5
*
***
*****
*******
*********

Program:-33 Write a C Program to print following pattern but take the input by the user.
12345
1234
123
12
1
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Enter the no. of line: \n");
scanf("%d", &n);
for( i=n;i>=1;i--)
{
for( j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
getch();
}

45
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

OUTPUT
Enter the no. of line:

12345
1234
123
12
1

Program:-34 Write a C Program to print the sum of the series:1 2 +22+32+42 ……….. upto
n.

#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, sum = 0;
clrscr();
printf("Enter the number:: ");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
sum =sum+ i * i;
}
printf("sum: %d ",sum);
getch();
}
OUTPUT
Enter the number 6

46
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

Sum :91

Program:-35 Write a C program for prime numbers Using For Loop.

#include <stdio.h>

#include <conio.h>

int main()

int i, num, temp = 0;

clrscr();

printf("Enter any numb to Check for Prime: ");

scanf("%d", &num);

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

if (num % i == 0)

temp++;

break;

if (temp == 0 && num != 1)

printf("%d is a Prime number", num);

else

47
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

printf("%d is not a Prime number", num);

getch();

return 0;

OUTPUT
Enter any numb to Check for Prime: 256

256 is not a Prime number

Program:-36 Write a C Program to print the Fibonacci series for 1 to n.

#include<stdio.h>

#include<conio.h>
void main()
{
int n, first = 0, second = 1, next, c;
clrscr();
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c<= 1 )
next = c;
else
{
next = first + second;
first = second;

48
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

second = next;
}
printf("%d\t",next);
}
getch();
}
OUTPUT
Enter the number of terms 7
0 1 1 2 3 5 8

Program:-37 Write a C Program to check whether the any number is Armstrong or not.

#include <math.h>

#include <stdio.h>
#include <conio.h>
int main() {
int num, originalNum, remainder, n = 0;
float result = 0.0;
clrscr();
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
for (originalNum = num; originalNum != 0; ++n) {
originalNum /= 10;
}
for (originalNum = num; originalNum != 0; originalNum /= 10) {
remainder = originalNum % 10;
result += pow(remainder, n);
}

49
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

if ((int)result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
getch();
return 0;
}
OUTPUT
Enter an integer: 92727
92727 is an Armstrong number.

50
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

CP LAB ASSIGNMENT:-5
OBJECTIVES: Write C Programs to learn about Array (1-D & 2-D Array) and String
Operations.

Program:-38 Write a C Program to scan and print elements of an Array.


#include<stdio.h>
#include<conio.h>
void main()
{
Int x[5], i;
clrscr();
printf("\n Enter 5 Numbers:");
for(i=0;i<5;i++)
scanf("%d", &x[i]);
printf("\n Entered Numbers are:");
for(i=0;i<5;i++)
printf("\n %d \t", x[i]);
getch();
}
OUTPUT
Enter 5 Numbers:2
6
7
3
9
Entered Numbers are
2 6 7 3 9

Program:-39 Write a C Program to Calculate Average of Array of 10 Elements.


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

51
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

void main()
{
int x[10], i, sum=0;
clrscr();
printf("\n Enter 10 Numbers:");
for(i=0;i<10;i++)
scanf("%d",&x[i]);
for(i=0;i<10;i++)
sum=sum+x[i];
printf("\n Average of Given 10 Numbers=%f", sum/10.0);
getch();
}
OUTPUT
Enter 10 Numbers:25
12
45
34
23
45
67
14
90
10
Average of Given 10 Numbers = 36.500000

Program:-40 Write a C Program to find the maximum and minimum element in an Array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],min, max;
int i, n;

52
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

clrscr();
printf("Enter the Number of Elements:\n");
scanf("%d",&n);
printf("Enter the Elements\n");
for( i=0; i<n; i++)
{
scanf("%d",&a[i]);
if(i==0)
min=max=a[i];
if(a[i]<min)
min=a[i];
if(a[i]>max)
max=a[i];
}
printf("Biggest Element is %d and Smallest Element is %d ",max, min);
}
OUTPUT
10 8 7 4 1
Biggest Element is 10 and Smallest Element is 1

Program:-41 Write a C Program to print matrix


#include <stdio.h>
#include <conio.h>
void main()
{
int arr1[3][3], i, j;
clrscr();
printf("\n Read a 2D array of size 3x3 and print the matrix :\n");
printf("------------------------------------------------------\n");
printf("Input elements in the matrix :\n");
for (i = 0; i < 3; i++)
{

53
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

for (j = 0; j < 3; j++)


{
printf("element - [%d],[%d] : \n ", i, j);
scanf("%d", &arr1[i][j]);
}
}
printf("\n The matrix is : \n");
for (i = 0; i < 3; i++)
{
printf("\n");
for (j = 0; j < 3; j++)
printf("%d\t", arr1[i][j]);
}
printf("\n\n");
getch();
}
OUTPUT
Read a 2D array of size 3x3 and print the matrix :
------------------------------------------------------
Input elements in the matrix :
element - [0],[0] : 1 2
element - [0],[1] : element - [0],[2] : 2 3
element - [1],[0] : element - [1],[1] : 2 5
element - [1],[2] : element - [2],[0] : 3 6
element - [2],[1] : element - [2],[2] : 3 9
The matrix is :

1 2 2
3 2 5
3 6 3

54
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

Program:-42 Program to Count the total number of words in a string.

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include < conio.h>

#define str_size 100

int main() {

char str[str_size];

int i, wrd;

clrscr();

printf("Input the string : ");

fgets(str, sizeof str, stdin);

i = 0;

wrd = 1;

while (str[i] != '\0') {

if (str[i] == ' ' || str[i] == '\n' || str[i] == '\t') {

wrd++;

i++;

printf("Total number of words in the string is : %d\n", wrd - 1);

getch();

return 0;

OUTPUT

55
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

Input the string : hello world

Total number of words in the string is : 2

Program:-43 Copy one string into another string

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
int main() {
char str1[100], str2[100];
int i;
clrscr();
printf("\n Copy one string into another string :\n");
printf("-----------------------------------------\n");
printf("Input the string : ");
fgets(str1, sizeof str1, stdin);
i = 0;
while (str1[i] != '\0') {
str2[i] = str1[i];
i++;
}
str2[i] = '\0';
printf("\nThe First string is : %s\n", str1);
printf("The Second string is : %s\n", str2);
printf("Number of characters copied : %d\n\n", i);
getch();
return 0;
}
OUTPUT
Copy one string into another string :
-----------------------------------------

56
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

Input the string : hello

The First string is : hello

The Second string is : hello

Number of characters copied : 6

CP LAB ASSIGNMENT:-6
OBJECTIVES: Write C Programs to understand Sorting and Searching using Array.

Program:-44 Write a C Program to sort the number of elements in an Array (Bubble Sort).
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],t;
int i, j=0;
printf("Enter 5 values into the Array ");
for(i=0;i<5;i++)
{
scanf("%d", &a[i]);
}
printf("the sorted order of elements");
for(i=0;i<5;i++)
{
for (j=0;j<5;j++)
{
if(a[i]<a[j])
{

57
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<5;i++)
{
printf("%d \n", a[i]);
}
getch();
}
OUTPUT
Enter 5 values in an Array
10 8 7 5 3
The Sorted Order of Elements are
3 5 7 8 10

Program:-45 Write a C Program to implement Linear Search in 1-D Array.

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,x,n;
printf("How many elements: ");
scanf("%d",&n);

printf("Enter array elements:\n");


for(i=0;i<n;++i)
scanf("%d",&a[i]);

58
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

printf("Enter element to search: \n");


scanf("%d",&x);

for(i=0;i<n;++i)
if(a[i]==x)
break;

if(i<n)
printf("Element found ");
else
printf("Element not found");

return 0;
}

OUTPUT
How many elements:5
Enter array elements:
25
26
13
14
85
Enter element to search:
13
Element found

CP LAB ASSIGNMENT:-7

59
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

OBJECTIVES: Write C Programs to learn Functions and Recursive Functions.

Program:-44 Write a C Program to add two integers using functions.

#include <stdio.h>
#include<conio.h>
int addNumbers(int a, int b); // function prototype
void main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("Sum = %d",sum);
getch();
}
int addNumbers(int a,int b) // function definition
{
int result;
result = a+b;
return result; // return statement
}
OUTPUT
Enters two numbers:10
30
Sum = 40

Program:-45 Write a C Program the Swapping the values of the two variables using Call
by Value.

#include <stdio.h>

60
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

#include<conio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before Swapping the values in main \n a = %d\n b = %d\n", a, b);
swap(a,b);
printf("After Swapping values in main a = %d, b = %d\n", a, b);
return 0;
}
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);
getch();
}

OUTPUT
Before Swapping the values in main
a=10
b=20
After Swapping the values in main
a=10
b=20
After Swapping the values in function
a=20

61
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

b=10

Program:-46 Write a C Program to calculate factorial of a number using RECURSION.


#include<stdio.h>
#include<conio.h>
int factorial(int n)
{
if(n<=1)
return 1;
else
return n*factorial(n-1);
}
int main()
{
int n=5;
printf("Factorial of %d is %d \n",n,factorial(n));
return 0;
}
OUTPUT
Factorial of 5 is 120
Program:-48 Write a C Program to generate the Fibonacci series using Recursion.

#include<stdio.h>
#include<conio.h>
int fibonacci(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;

62
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

else
return fibonacci(n-1)+ fibonacci(n-2);
}
int main()
{
int i;
for(i=0;i<10;i++)
{
printf("%d\t", fibonacci(i));
}
return 0;
}

OUTPUT
0 1 1 2 3 5 8 13 21 34
Program:-49 Write a C Program to find the GCD of two given integers using Recursion.

#include<stdio.h>
#include<conio.h>
int hcf(int n1, int n2);
int main()
{
int n1, n2;
clrscr();
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}

63
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

int hcf(int n1, int n2)


{
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}
OUTPUT
Enter two positive integers 3
4
GCD of 3 and 4 is 1

CP LAB ASSIGNMENT:-8

OBJECTIVES: Write C Programs to understand Structure and Union operation.

Program:-50 Write a C Program to Store Information (name, roll and marks) of a Student
using structure.
#include <stdio.h>
#include<conio.h>
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
struct student s;
printf("Enter information of students:\n\n");

64
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

printf("Enter name: ");


scanf("%s",s.name);
printf("Enter roll number: ");
scanf("%d",&s.roll);
printf("Enter marks: ");
scanf("%f",&s.marks);
printf("\nDisplaying Information\n");
printf("Name: %s\n",s.name);
printf("Roll: %d\n",s.roll);
printf("Marks: %.2f\n",s.marks);
return 0;
}
OUTPUT
Enter information of students:
Enter name: rahul
Enter roll number: 21
Enter marks:321
Displaying Information
Name: rahul
Roll number: 21
Marks:321
Program:-51 Write a C Program to store information of 50 students using array of
structure.
#include<stdio.h>
#include<conio.h>
struct student
{
charname[50];
int roll;

65
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

float marks;
};
intmain()
{
struct student s[50];
inti;
printf("Enter information of students:\n");
for(i=0;i<50;++i)
{
s[i].roll=i+1;
printf("\nFor roll number %d\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
printf("Displaying information of students:\n\n");
for(i=0;i<50;++i)
{
printf("\nInformation for roll number %d:\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
}
return0;
}
OUTPUT
Enter information of students:
For roll number 1
Enter name: Tom
Enter marks: 98

66
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

For roll number 2


Enter name: Jerry
Enter marks: 89
.
.
.
Displaying information of students:
Information for roll number 1:
Name: Tom
Marks: 98
Information for roll number 2:
Name: Jerry
Marks: 89
.
.Program:-52 Write a C Program to illustrate concept of unions.
#include<stdio.h>
#include<conio.h>
void main()
{
union universal
{
int ival;
float fval;
char cval;
char *p;
};
union universal u1;
u1.ival=16;
u1.fval=25.16;
u1.p="hello world";

67
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

printf("value of ival is %d\n",u1.ival);


printf("value of fval is %f",u1.fval);
getch();
}
OUTPUT
value of ival is 16
value of fval is 25.16

CP LAB ASSIGNMENT:-9

OBJECTIVES: Write C Programs to learn Pointer operations.

Program:-53 Write a C Program to show the use of pointer.


#include <stdio.h>
#include<conio.h>
void main()
{
int balance;
int *address;
int value;
balance = 5000;
address = &balance;
value = *address;
printf("Balance is : %d\n" , value);
}
Output:
Balance is = 5000
Program:-54 Write a C Program to show the use of arithmetic operator using pointer.
#include<stdio.h>
#include<conio.h>

68
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

int main()
{
int a, b, *p1, *p2, x, y, z;
a = 12;
b = 4;
p1 = &a;
p2 = &b;
x = *p1 * *p2 - 6;
y = 4* - *p2 / *p1 + 10;
printf("Address of a = %u\n", p1);
printf("Address of b = %u\n", p2);
printf("\n");
printf("a = %d, b = %d\n", a, b);
printf("x = %d, y = %d\n", x, y);
*p2 = *p2 + 3;
*p1 = *p2 - 5;
z = *p1 * *p2 - 6;
printf("\na = %d, b = %d,", a, b);
printf(" z = %d\n", z);
return 0;
}
OUTPUT
Address of a = 4020
Address of b = 4016
a = 12, b = 4
x = 42, y = 9
a = 2, b = 7, z = 8
Program:-55 Write a C Program to show the demo of pointer to a pointer variable.

69
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

#include<stdio.h>

#include<conio.h>
int main()
{
char **ptr = NULL;
char *p = NULL;
char c = 'd';
p = &c;
ptr = &p;
printf("\n c = [%c]\n",c);
printf("\n *p = [%c]\n",*p);
printf("\n **ptr = [%c]\n",**ptr);
return 0;
}
OUTPUT
c = [d]
*p = [d]
**ptr = [d]

CP LAB EXPERIMENT:-10

OBJECTIVES: Write C Programs to understand File Handling operations.

Program:-56 Write a C Program for Writing to and reading from a file (File handling).

#include <stdio.h>
#include <stdlib.h>
struct threeNum
{

70
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

int n1, n2, n3;


};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("C:\\program.bin","rb")) == NULL)
{
printf("Error! opening file");
exit(1);
}
for(n = 1; n < 5; ++n)
{
fread(&num, sizeof(struct threeNum), 1, fptr);
printf("n1: %d\tn2: %d\tn3: %d", num.n1, num.n2, num.n3);
}
fclose(fptr);
return 0;
}
OUTPUT

Error! opening file

Program :-57 Write a C Program to write characters A to Z into a file and read the file
and print the characters in lowercase. (File handling)

#include<stdio.h>
#include<stdlib.h>

71
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

int main()
{
FILE *fp; /* file pointer*/
char fName[20];
printf("\nEnter file name to create :");
scanf("%s",fName);
/*creating (open) a file*/
fp=fopen(fName,"w");
/*check file created or not*/
if(fp==NULL)
{
printf("File does not created!!!");
}
printf("File created successfully.");
/*writting into file*/
putc('A',fp);
putc('B',fp);
putc('C',fp);
printf("\nData written successfully.");
fclose(fp);
/*again open file to read data*/
fp=fopen(fName,"r");
if(fp==NULL)
{
printf("\nCan't open file!!!");
}
printf("Contents of file is :\n");
printf("%c",getc(fp));
printf("%c",getc(fp));

72
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

printf("%c",getc(fp));
fclose(fp);
return 0;
}

OUTPUT
Enter file name to create :PPS
Segmentation fault

CP LABEXPERIMENT:-11

OBJECTIVES:- Write C Programs to input data through Command line argument.

Program:-58 Write a C Program to input data through Command line argument.

#include <stdio.h>

int main(int argc, char* argv[])

printf("Program name is: %s", argv[0]);

73
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

if (argc == 1)

printf("\n No Extra Command Line Argument Passed "

"Other Than Program Name");

if (argc >= 2) {

printf("\n Number Of Arguments Passed: %d", argc);

printf("\n ----Following Are The Command Line "

"Arguments Passed----");

for (int i = 0; i < argc; i++)

printf("\nargv[%d]: %s", i, argv[i]);

return 0;

OUTPUT

Program name is: /tmp/WJqKhWnmRT.o

No Extra Command Line Argument Passed Other Than Program Name

74
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

Viva Questions
Year: B. Tech. I Year Semester-I & II
Subject& Code: Computer Programming Lab (1FY3-24/2FY3-24)

Object of the Experiment


S.No. CO

Identify and Analyze the input/output operation, decision making


1. CO1
statements and looping
1. What is Flowchart?
2. Name the four basic data types in “C” language?

75
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

3. Describe at least five different format specifiers?


4. Define and explain scanf () function?
5. Define and explain printf () function?
6. What is pre-processor?
7. What are Loop control statements?
8. List a few unconditional control statements in C.
9. Explain for loop.
10. Explain while loop.
11. Define Keywords.
Analyze and Implement arrays, functions, pointers and dynamic
2. CO2
memory allocation.
1. What is an array?
2. What is Multidimensional Arrays
3. Explain about the functions strcat() and strcmp()?
4. Define function.
5. Differentiate built-in functions and user – defined functions.
6. Distinguish between actual and formal arguments.
7. Explain the concept and use of type void.
8. What is recursion?
9. What is the usage of the pointer in C?
10. What is dynamic memory allocation?
Apply structure, union and data handling through files in
3. CO3
‘C’ Programming Language
1. What is the Structure?
2. What is Union?
3. What is an auto keyword in C?
4. What are the functions to open and close the file in C
language?
5. What is typecasting?
6. What is the acronym for ANSI?
7. What is the basic difference between Structure & Union?

76
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

8. Can we compile a program without main() function?


9. What are the different operations of file handling?

77
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

78
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

79
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

JECRC Campus, Shri Ram Ki Nangal, Via-Vatika, Jaipur

80

You might also like