Lab Manual CP Lab Final
Lab Manual CP Lab Final
Lab Manual CP Lab Final
LAB MANUAL
1
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
CP LAB MANUAL
INDEX
Course Outcomes 9
CO/PO Mapping 9
Introduction about Lab: Brief Description, Applications and Industry use 10-11
2
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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.
1. Identify, based on informed perception of Indian, regional and global needs, the areas
of focus and provide platform to gain knowledge and solutions.
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
PROGRAM OUTCOMES
4
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
5
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
6
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
7
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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.
Write C Programs to learn about Array (1-D & 2-D Array) and String
CO2
5. operations.
11. Write C Programs to input data through Command Line argument. CO3
8
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Course Outcomes
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
Mapping of
Engineering Knowledge
Course Outcomes
& POs
Modern Tool Usage
Life-long Learning
Problem Analysis
Communication
Ethics
9
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Uses of C language:
• Database Systems
• Language Interpreters
• Operating Systems
• Network Drivers
• Word Processors
Features of C language:
• C is highly portable.
• C is easily extensible.
C Preprocessor directive:
10
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
11
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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.
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
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.
4. Any student not following these instructions will be denied entry in the lab.
3. Get the output of the current experiment checked by the instructor in the lab copy.
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
CP LAB EXPERIMENT: -1
14
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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
15
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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
16
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
#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
17
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
getch();
}
OUTPUT
Input Principle, Time & Rate of interest
500
5
2
Simple Interest = 50
18
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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
19
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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
20
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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
21
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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.
22
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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
#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
#include <conio.h>
int main()
24
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
int a, b, c;
clrscr();
if(a>b)
if(a>c)
else
printf("%d is greatest",c);
else
if(b>c)
else
getch();
return 0;
OUTPUT
25
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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:
#include <stdio.h>
#include <conio.h>
int main()
int marks;
clrscr();
scanf("%d",&marks);
if(marks>=60)
printf("First Division!!");
printf("Second Division!!");
26
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
printf("Third Division!!");
printf("Fail!!!");
else
printf("Invalid marks!!");
getch();
return 0;
OUTPUT
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
int age;
clrscr();
scanf("%d",&age);
if(age>0)
if(age>=18)
else
getch();
OUTPUT
#include <stdio.h>
#include<conio.h>
void main()
28
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
char c;
clrscr();
scanf("%c",&c);
else
getch();
OUTPUT
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();
scanf("%d", &year);
if (year % 400 == 0) {
29
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
else if (year % 4 == 0) {
else {
getch();
return 0;
OUTPUT
#include <stdio.h>
#include<conio.h>
void main()
int n;
30
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
clrscr();
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
break;
getch();
OUTPUT
Four.
#include <stdio.h>
#include <conio.h>
int main() {
char op;
clrscr();
scanf("%c", &op);
switch (op) {
case '+':
break;
case '-':
32
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
break;
case '*':
break;
case '/':
break;
default:
getch();
return 0;
OUTPUT
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
#include <stdio.h>
#include<conio.h>
void main()
float units;
clrscr();
scanf("%f", &units);
else
34
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
sa = 0.20 * amt;
ta = sa + amt;
getch();
OUTPUT
#include <stdio.h>
#include<conio.h>
void main()
int s1,s2,s3;
clrscr();
scanf("%d", &s1);
scanf("%d", &s2);
scanf("%d", &s3);
if(((s1 + s2) <= s3) || ((s2 + s3) <= s1) || ((s1 + s3) <= s2))
35
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
else
if(s1 == s2 == s3)
else
getch();
OUTPUT
36
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
CP LAB EXPERIMENT:-3
OBJECTIVES: Write C Programs to learn Iterative Statements like While and Do-while
Loops.
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
#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
38
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
int i=1;
clrscr();
while(i<=20)
{
printf(" * ");
i++;
}
getch();
}
OUTPUT
* * * * * * * * * * * * * * * * * * * *
39
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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
40
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
}
OUTPUT
Enter an integer: 263548
Reversed Number = 845362
#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
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
43
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
CP LAB EXPERIMENT:-4
44
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
}
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
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
Sum :91
#include <stdio.h>
#include <conio.h>
int main()
clrscr();
scanf("%d", &num);
if (num % i == 0)
temp++;
break;
else
47
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
getch();
return 0;
OUTPUT
Enter any numb to Check for Prime: 256
#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
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
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
CP LAB ASSIGNMENT:-5
OBJECTIVES: Write C Programs to learn about Array (1-D & 2-D Array) and String
Operations.
51
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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
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
53
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
1 2 2
3 2 5
3 6 3
54
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char str[str_size];
int i, wrd;
clrscr();
i = 0;
wrd = 1;
wrd++;
i++;
getch();
return 0;
OUTPUT
55
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
#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
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
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
#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,x,n;
printf("How many elements: ");
scanf("%d",&n);
58
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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
#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
#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
b=10
#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
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
CP LAB ASSIGNMENT:-8
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
65
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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
67
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
CP LAB ASSIGNMENT:-9
68
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
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
#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
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
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
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
printf("%c",getc(fp));
fclose(fp);
return 0;
}
OUTPUT
Enter file name to create :PPS
Segmentation fault
CP LABEXPERIMENT:-11
#include <stdio.h>
73
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
if (argc == 1)
if (argc >= 2) {
"Arguments Passed----");
return 0;
OUTPUT
74
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Viva Questions
Year: B. Tech. I Year Semester-I & II
Subject& Code: Computer Programming Lab (1FY3-24/2FY3-24)
75
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
76
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
77
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
78
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
79
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
80