CP Lab Manual
CP Lab Manual
List of Experiment
Exp. Name of experiment Page
no no.
1 Simple Input output program. 2-6
1.1 WAP to print hello message
1.2 WAP to print value of integer.
1.3 WAP to print addition of two numbers
1.4 WAP to print area of rectangle.
Page | 1
2
10 Pointers 30-33
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.
Page | 2
3
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.
Page | 3
4
EXPERIMENT NO. 1
#include <stdio.h>
int main()
{
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
Output
Hello, World!
#include <stdio.h>
int main()
{
int number;
Output
Enter a integer: 25
You entered: 25
Page | 4
5
#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
Page | 5
6
EXPERIMENT NO. 2
Object: Programs to learn data type, variables, If-else statement.
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
}
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
Page | 6
7
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;
Page | 7
8
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()
{
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
Page | 8
9
{
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:");
scanf(“%d %d %d %d %d”,&sub1, &sub2, &sub3, &sub4, &sub5);
tot= sub1+sub2+sub3+sub4+sub5;
per = tot*100/500;
printf(“Percentage of student is = %d”,per);
if(per>=85)
{
printf(“Grade = A”);
}
else if(per>=70 && per<85)
{
printf(“Grade = B”);
}
else if(per>=55 && per<70)
{
printf(“Grade = C”);
}
else if(per>=40 && per<55)
{
printf(“Grade = D”);
}
else
{
printf(“Fail”);
}
getch();
}
Output:
Enter Marks: 35 45 55 65 75
Percentage of student is = 55
Grade = C
Page | 9
10
EXPERIMENT NO. 3
Object: Programs to understand nested if-else statement and switch statement.
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.
Page | 10
11
# 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;
}
Page | 11
12
Output
Enter an operator (+, -, *,): *
Enter two operands: 1.5
4.5
1.5 * 4.5 = 6
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 :
Page | 12
13
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);
Page | 13
14
}while(number<=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
Page | 14