CP Lab Exp 1 To 12
CP Lab Exp 1 To 12
List of Experiment
Exp. Name of experiment
no
1 Simple Input output program.
2.1 W
AP
to
cre
ate
a
file
and
writ
e
dat
a in
it.
2.2 WAP to write any sentence on file.
2.3 WAP to read a line from a file and display it.
2.4 WAP to print size of a file
WA
P to
calc
ulat
e
sim
ple
inte
rest.
WA
P to
swa
p
two
nu
mbe
rs
wit
hou
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 1
2
t
usin
g
thir
d
vari
able
.
WA
P to
che
ck
whe
ther
a
give
n
nu
mbe
r is
eve
n or
odd.
WA
P to
calc
ulat
e
perc
enta
ge
and
defi
nes
its
gra
des
usin
g if
else
if
ladd
er
stat
eme
nt.
10 Pointers
10.1 WAP to print many values of variable using pointers.
10.2 WAP to perform arithmetic operations using pointers
10.3 WAP to print addition of array elements using pointers.
11.1 WAP to write a some of the character (without taking input from
the keyboard) and reading, printing , written characters.
11.2 WAP to write a string to a file & print.
11.3 WAP to implement fprintf() & fscanf().
DO’S
1. Student should get the record of previous experiment checked before starting
the new experiment.
2. Read the manual carefully before starting the experiment.
3. Before starting the experiment, get circuit diagram checked by the teacher.
4. Before switching on the power supply, get the circuit connections checked.
5. Get your readings checked by the teacher.
6. Apparatus must be handled carefully.
7. Maintain strict discipline.
8. Keep your mobile phone switched off or in vibration mode.
9. Students should get the experiment allotted for next turn, before leaving the lab.
DONT’S
1. Do not touch or attempt to touch the mains power supply Wire with bare
hands.
2. Do not overcrowd the tables.
3. Do not tamper with equipments.
4. Do not leave the without permission from the teacher.
EXPERIMENT NO. 1
#include <stdio.h>
int main()
{
// printf() displays the string inside quotation
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 5
6
printf("Hello, World!");
return 0;
}
Output
Hello, World!
#include <stdio.h>
int main()
{
int number;
Output
Enter a integer: 25
You entered: 25
#include <stdio.h>
int main()
{
int firstNumber, secondNumber, sumOfTwoNumbers;
// Displays sum
printf("%d + %d = %d", firstNumber, secondNumber, sumOfTwoNumbers);
return 0;
}
Output
Enter two integers: 12
11
12 + 11 = 23
#include<stdio.h>
#include<conio.h>
int main() {
int length, breadth, area;
return (0);
}
Output:
Enter the Length of Rectangle : 5
Enter the Breadth of Rectangle : 4
Area of Rectangle : 2
1.Write down short note on c language. And also describe its features.
Interactivity
Modularity
Efficiency and Effectiveness
EXPERIMENT NO. 2
Theory:-
If-else statement: The if-else statement is a two way branch: it means do one thing or
the other. When it is executed, the condition is evaluated and if it has the value `true'
(i.e. not zero) then statement1 is executed. If the condition is `false' (or zero) then
statement2 is executed. The if-else construction often saves an unnecessary test from
having to be made.
if (condition)
{
statements
}
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 8
9
else
{
statements
}
#include<stdio.h>
int main()
{
int amount, rate, time, si;
printf("\nEnter Principal Amount : ");
scanf("%d", &amount);
printf("\nEnter Rate of Interest : ");
scanf("%d", &rate);
return(0);
}
Output :
Enter Principal Amount : 500
Enter Rate of interest : 5
Enter Period of Time : 2
Simple Interest : 50
In the first instance, taking a variable that contains an item of data, and swapping it
with another variable containing the same type. The actual values are sapped, so that
variable one contains the value of variable two, and variable two contains the value of
variable one.
In the second instance, Reference swapping performs the same function, but it does
not actually swap the value, but only a pointer to that value in memory.
Source Code:
#include <stdio.h>
main()
{
int a,b,temp;
printf("Before Swapping variable a :");
scanf("%d",&a);
printf("Before Swapping variable b :");
scanf("%d",&b);
temp=a;
a=b;
b=temp;
printf("After Swapping variable a=%d",a);
printf("After Swapping variable b=%d",b);
}
Output:
Before Swapping variable a: 3
Before Swapping variable b: 5
After Swapping variable a=5
After Swapping variable b=3
#include <stdio.h>
main()
{
int a,b,temp;
printf("Before Swapping variable a :");
scanf("%d",&a);
printf("Before Swapping variable b :");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("After Swapping variable a=%d",a);
printf("After Swapping variable b=%d",b);
}
Output:
Before Swapping variable a: 3
Before Swapping variable b: 5
After Swapping variable a=5
After Swapping variable b=3
#include <stdio.h>
main()
{
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 10
11
int n;
printf("enter a number :");
scanf("%d",&n);
if(n%2==0)
{
printf("no is even\n");
}
else
{
printf("no. is odd\n");
}
}
Output:
Enter a number: 121
no. is odd.
2.4 WAP to calculate percentage and defines its grades using if else if ladder
statement.
If-Else-If ladder: When a series of many conditions have to be checked we may use
the ladder else if statement which takes the following general form.
If(cond1)
{
Statement1;
}else if(cond2)
{
Statement2;
}else if(cond3)
{
Statement3;
}else
{
Default statement;
}
This construct is known as if else construct or ladder. The conditions are evaluated
from the top of the ladder to downwards. As soon on the true condition is found, the
statement associated with it is executed and the control is transferred to the statement
– x (skipping the rest of the ladder. When all the condition becomes false, the final
else containing the default statement will be executed.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int sub1, sub2, sub3, sub4, sub5, tot, per;
printf("Enter Marks:");
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 11
12
Output:
Enter Marks: 35 45 55 65 75
Percentage of student is = 55
Grade = C
Data Types in c:
1.Fundamental Data Types
Integer types
Floating type
Character type
2. Derived Data Types
Arrays
Pointers
Structures
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 12
13
Enumeration
Keywords
Keywords are predefined, reserved words used in programming that have special
meanings to the compiler.
For example:
int money;
Constants/Literals
A constant is a value or an identifier whose value cannot be altered in a program.
These fixed values are also called literals. Constants can be of any of the basic data
types like an integer constant, a floating constant, a character constant, or a string
literal. As mentioned, an identifier also can be defined as a constant.
const double PI = 3.14
Here, PI is a constant.
Identifiers
Identifier refers to name given to entities such as variables, functions, structures etc.
Identifier must be unique.
They are created to give unique
name to a entity to identify it
during the execution of the
program. For example:
int money;
double
accountBalance;
Here, money and acco untBalance
are identifiers.
Syntax of if statement
if (condition)
{
statements;
... ... ...
}
Flowchart of if statement
If …else statement: If condition returns true then the statements inside the body of
“if” are executed and the statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped and
the statements in “else” are executed.
Syntax of if...else statement
if(condition) {
// Statements inside body of if
}
else {
//Statements inside body of else
}
printf("End of Program");
getch( );
}
Output:
enter the value of x:20
enter the value of y:20
x is equal to y
End of Program
EXPERIMENT NO. 3
Theory:- Nested if-else statement : When an if else statement is present inside the
body of another “if” or “else” then this is called nested if else.
switch statement: Switch case statements are a substitute for long if statements that
compare a variable to several integral values
#include <stdio.h>
int main()
{
double n1, n2, n3;
if (n1>=n2)
{
if(n1>=n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
}
else
{
if(n2>=n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.",n3);
}
return 0;
}
Output:
Enter three numbers: -4.5
3.9
5.6
5.60 is the largest number.
# include <stdio.h>
int main() {
char operator;
double firstNumber,secondNumber;
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber +
secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber -
secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber *
secondNumber);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber /
firstNumber);
break;
return 0;
}
Output
Enter an operator (+, -, *,): *
Enter two operands: 1.5
4.5
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 18
19
1.5 * 4.5 = 6
Ans:- Decision making structures require that the programmer specify one or more
conditions to be evaluated or tested by the program, along with a statement or
statements to be executed if the condition is determined to be true, and optionally,
other statements to be executed if the condition is determined to be false.
Types of decision making statement.
Q.2 Compare the use of the if-else statement with the use of the ?: operator?
Ans:- ?: this is called conditional operators and if-else is decision making statement.
Syntax:
if(condition)
{
if(condition)
{
statements;
}
else
{
statements;
}
}
else
{
statements;
}
In above syntax, the condition is checked first. If it is true, then the program control
flow goes inside the braces and again checks the next condition. If it is true then it
executes the block of statements associated with it else executes else part.
ANS: Break:- The break statement in C programming language has the following
two usages:
switch(expression)
{
case 1 :
statement(s);
break;
case 2 :
statement(s);
break;
default :
statement(s);
}
Experiment No. 4
Object: Programs to learn iterative statements like while and do-while loops.
Theory: In While looping statement allows a number of lines represent until the
condition is satisfied. In do-while loop first execute the statements then it checks the
condition.
#include <stdio.h>
int main()
{
int i, end;
/*
* Input a number from user
*/
printf("Print all natural numbers from 1 to : ");
scanf("%d", &end);
/*
* Print natural numbers from 1 to end
*/
i=1;
while(i<=end)
{
printf("%d\n", i);
i++;
}
return 0;
}
Output:
Print all natural numbers between 1 to :
10
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
int main()
{
//loop counter declaration
int number;
//variable to store limit /N
int n;
//input value of N
printf("Enter the value of N: ");
scanf("%d",&n);
//print statement
printf("Odd Numbers from 1 to %d:\n",n);
return 0;
}
Output:
Enter the value of N: 10
Odd Numbers from 1 to 10:
13579
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,d;
printf("Enter the number:");
scanf("%d",&n);
while (n>0)
{
d=n%10;
sum=sum+d;
n=n/10;
}
printf("\n sum of digits= %d",sum);
printf("\n");
getch();
}
Output:
Enter the number: 15
Sum of digits= 6
“for loop”
“while loop”
“do while loop”
The “for loop” loops from one number to another number and increases by a specified
value each time.
The “for loop” uses the following structure:
WHILE LOOP:
In While looping statement allows a number of lines represent until the condition is
satisfied
Syntax:
while( condition)
Statement1;
...
Statement n;
DO WHILE LOOP:
In DO WHILE LOOP first execute the statements then it checks the condition.
Syntax:
do
Statement1;
...
Statement n;
}while(condition);
Experiment No. 5
#include <stdio.h>
void main()
{
int i, fact = 1, num;
Output:
Enter the number
10
Factorial of 10 = 3628800
*
**
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 27
28
***
* * * *.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
printf("Enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf(“*”);
}
printf(“\n”);
}
}
Output:
Enter the number: 4
*
**
***
****
****
***
**
*
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
printf("Enter the number:");
scanf("%ld",&n);
for(i=1;i<=n;i++)
{
for(j=1;j>=i; j--)
{
printf(“*”);
}
printf(“\n”);
}
}
Output:
Enter the number: 4
****
***
**
*
“while loop”
“do while loop”
“for loop”
Q.3 Differentiate between the Entry controlled and Exit controlled loops in C ?
Ans. In Entry controlled loop the test condition is checked first and if that condition is
true than the block of statement in the loop body will be executed while in exit
controlled loop the body of loop will be executed first and at the end the test condition
is checked, if condition is satisfied than body of loop will be executed again.
Q.4 What rules apply to the nesting of loops?
Ans. A nested loop is a logical structure used in computer programming where two
repeating statements are placed in a "nested" form, i.e., one loop is situated within the
body of the other. In this structure, the first iteration of the outer loop causes the inner
loop to execute. The inner loop then repeats for as many times as is specified. When
the inner loop completes, the outer loop is executed for its second iteration, triggering
the inner loop again, and so on until the requirements for the outer loop are complete.
Q.5 What will be the output of the following programs?
Main()
{
int i=1;
while(i<=10);
{
printf(“%d\n”,i);
i=i+1;
}
}
Ans. The output is :-
Error
Experiment No. 6
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 30
31
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int i, n, sum=0;
/*
* Add each array element to sum
*/
for(i=0; i<n; i++)
{
sum = sum + arr[i];
}
return 0;
}
Output:
Enter size of the array: 10s of array = 550
Enter size of the array: 5
Enter 5 elements:
12345
Sum of all elements of array = 15
#include<stdio.h>
int main() {
int a[30], i, num, largest;
return (0);
}
Output :
Enter no of elements : 5
11 55 33 77 22
Largest Element : 77
#include <stdio.h>
int main(){
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d ",sum[i][j]);
if(j==c-1)
{
printf("\n\n");
}
}
return 0;
}
Output
Enter number of rows (between 1 and 100): 2
Enter number of columns (between 1 and 100): 3
2 8 7
10 8 6
#include <stdio.h>
main()
{
int a[2][3],b[3][2],c[2][2],i,j,k,sum=0;
printf(“Enter 6 values ”);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
scanf(“%d”,&a[i][j]);
}
printf(“Enter 6 more values ”)
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&b[i][j]);
}
for(i=0;i<2;i++)
{
for(k=0;k<2;k++)
{
for(j=0;j<3;j++)
sum=sum+a[i][j]*b[j][k];
c[i][k]=sum;
sum=0;
}
}
printf(“Multiplication of array ”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf(“%d ”,c[i][j]);
printf(“\n”);
}
}
Output:
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 34
35
Enter 6 values 1 2 3 1 2 3
Enter 6 more values 1 2 1 2 1 2
Multiplication of array 6 12
6 12
#include <stdio.h>
int main()
{
char s[1000];
int i;
printf("Enter a string: ");
scanf("%s", s);
for(i = 0; s[i] != '\0'; ++i);
printf("Length of string: %d", i);
return 0;
}
Output
Length of string: 9
#include <stdio.h>
#include <string.h>
int main()
gets(a);
else
return 0;
Output:
AbccbA
1. Array having more than one subscript variable is called Multi-Dimensional array.
Syntax:
Experiment No. 07
#include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[c]);
scanf("%d", &search);
break;
if (c == n)
return 0;
Output:
Enter no of elements : 5
11 22 33 44 55
#include <stdio.h>
void main()
{
int i, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);
a = number[i];
number[i] = number[j];
number[j] = a;
Output:
Enter the value of N: 6
Enter the numbers
3
78
90
456
780
200
The numbers arranged in ascending order are given below
3
78
90
200
456
780
Experiment No. 08
#include<stdio.h>
int main()
{
int number, Square;
Square = Calculte_Square(number);
return 0;
}
Output:
8.2 WAP to check whether a given number is Armstrong or not using function.
#include <stdio.h>
#include <math.h>
originalNumber = number;
while (originalNumber != 0)
{
originalNumber /= 10;
++n;
}
originalNumber = number;
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 43
44
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += pow(remainder, n);
originalNumber /= 10;
}
return flag;
}
Output:
#include <stdio.h>
int fibo(int);
int main()
{
int num;
int result;
8.4 W.A.P to swap two numbers using call by value & call by reference method.
#include <stdio.h>
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 45
46
temp = num1;
num1 = num2;
num2 = temp;
return 0;
}
Call by reference:
#include <stdio.h>
/**
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 46
47
temp = *num1;
*num1 = *num2;
*num2 = temp;
/*
* &n1 - & evaluate memory address of n1
* &n2 - & evaluate memory address of n2
*/
swap(&n1, &n2);
return 0;
}
Ans: Calling function is the function which calls the another function and the function
which is called is known as called function.
Ans: Actual parameters are those parameters which are passed in functions as
arguments from calling function and formal parameters are those parameters which
are used in function body for the purpose of evaluating return value.
void recursion( ) {
int main( ) {
recursion( );
}
The C programming language supports recursion, i.e., a function to call itself. But
while using recursion, programmers need to be careful to define an exit condition
from the function, otherwise it will go into an infinite loop.
Experiment No. 09
9.1 WAP to store information (Name, Roll and Marks) of a student using structure
and print.
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s;
int main()
{
printf("Enter information:\n");
printf("Displaying Information:\n");
printf("Name: ");
puts(s.name);
return 0;
}
Output
Enter information:
Enter name: Indrajeet
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 49
50
9.2 WAP to read book detail and print them using structure.
#include <stdio.h>
struct Bookinfo
{
char[20] bname;
int pages;
int price;
}book[3];
for(i=0;i<3;i++)
{
printf("\nEnter the Name of Book : ");
gets(book[i].bname);
printf("\nEnter the Number of Pages : ");
scanf("%d",book[i].pages);
printf("\nEnter the Price of Book : ");
scanf("%f",book[i].price);
}
for(i=0;i<3;i++)
{
printf("\nName of Book : %s",book[i].bname);
printf("\nNumber of Pages : %d",book[i].pages);
printf("\nPrice of Book : %f",book[i].price);
}
return 0;
}
#include <stdio.h>
union Job
{
float salary;
int workerNo;
} j;
int main()
{
j.salary = 12.3;
j.workerNo = 100;
Output:
Salary = 0.0
Structure has name mystruct and it contains two variables: an integer named numb
and a character named ch.
Q.3 How can structure variables be declared and it differ from structure type
declarations.
Ans. Structure variables are declared after the structure type declarations. Structure
variables are used to access the structure elements.
Q.4 Define Union. How does a union differ from a structures.
Ans.TO define a union you must use the union statement in the same way as we did
while defining a structure. The union statement define a new data type with more than
one member for your program. Size of structure is the sum of size of all element’s
datatypes while size of union is the maximum ofsize of elements datatype.
Experiment No. 10
#include <stdio.h>
int main(){
int* pc;
int c;
c=22;
printf("Address of c:%u\n",&c);
printf("Value of c:%d\n\n",c);
pc=&c;
printf("Address of pointer pc:%u\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
c=11;
printf("Address of pointer pc:%u\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
*pc=2;
printf("Address of c:%u\n",&c);
printf("Value of c:%d\n\n",c);
return 0;
}
Output
Address of c: 2686784
Value of c: 22
Address of c: 2686784
Value of c: 2
#include <stdio.h>
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 54
55
int main()
{
int m = 5, n = 10, o = 0;
int *p1;
int *p2;
int *p3;
o = *p1+*p2;
printf("*p1+*p2 = %d\n", o);//point 1
p3 = p1-p2;
printf("p1 - p2 = %d\n", p3); //point 2
p1++;
printf("p1++ = %d\n", p1); //point 3
p2--;
printf("p2-- = %d\n", p2); //point 4
return 0;
}
p1 = 2680016
p2 = 2680012
*p1+*p2 = 15
p1-p2 = 1
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 55
56
p1++ = 2680020
p2-- = 2680008
#include <stdio.h>
int main()
{
int data[5], sum=0,i;
printf("Enter elements: ");
return 0;
}
Output
Enter elements: 1
2
3
4
5
Sum=15
Experiment No. 11
11.1 .WAP to write a some of the character (without taking input from the
keyboard) and reading, printing , written characters.
fclose(fp);
return 0;
}
Output:
#include <stdio.h>
#include <stdlib.h> /* For exit() function */
int main()
{
char sentence[1000];
FILE *fptr;
printf("Enter a sentence:\n");
gets(sentence);
fprintf(fptr,"%s", sentence);
fclose(fptr);
return 0;
}
Output:
Enter sentence:
I am awesome and so are files.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char str[80], str1[80];
fp = fopen("data.txt","w");
if(fp == NULL)
{
printf("Cannot open file.\n");
exit(1);
}
printf("Enter string to be written in a file: ");
fscanf(stdin, "%s", str); /*Read from keyboard */
fp = fopen("data.txt","r");
if(fp == NULL) {
printf("Cannot open file.\n");
exit(1);
}
fscanf(fp, "%s", str1); /* read a word from file and copy into
str1 */
fprintf(stdout, "%s", str1); /* print str1 on screen */
return 0;
}
Output:
Q1.What is FILE?and What is file handling in c language? List out the different
modes of opening a file?
Ans: File is permanent storage of data. It is a collection of bytes. It is the process
that sores and retrieves data in files. Different modes of opening a file r, a ,w ,r+, a+,
w+
ANS: -1
Experiment No. 12
DOHAS/LAB MANUAL/1FY3-24/ 2FY3-24 CP LAB
Page | 61
62
#include <stdio.h>
if( argc == 2 ) {
else {
Output:
$./a.out testing