0% found this document useful (0 votes)
3 views

CP Lab Manual

Uploaded by

vikashydv.751
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CP Lab Manual

Uploaded by

vikashydv.751
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1

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.

2 Data type, variables & conditional statements (if, if-else,ladder-if) 7-10


WAP to calculate simple interest.
WAP to swap two numbers without using third variable.
WAP to check whether a given number is even or odd.
WAP to calculate percentage and defines its grades using if
else if ladder statement.

3 Nested if-else & switch statements. 11-14

WAP to print largest of three numbers


WAP to implement a simple calculator using switch.

4 Iterative Statements(while & do-while) 15-18


WAP to print natural numbers using while loop.
WAP to print odd numbers using do-while loop.
WAP to print sum of the digits of a number.

5 For loop & nested for loop 19-20


5.1 WAP to print factorial of a number using for loop.
5.2 WAP to print pyramid pattern.

6 Array and string operations 21-27


6.1 WAP to add elements of one dimensional array.
6.2 WAP to print largest number of an array.
6.3 WAP to print addition/multiplication of two dimensional
arrays.
6.4 WAP to print length of a string.
6.5 WAP to check whether a given string is palindrome or not.

7 Sorting & searching 28-29

Page | 1
2

7.1 WAP to search a particular number in array & print location.


7.2 WAP to sort an array in ascending order.

8 Functions & recursion 30-33


8.1 WAP to print square of a number using user defined functions.
8.2 WAP to check whether a given number is Armstrong or not
using function.
8.3 WAP to print Fibonacci series using recursion.
8.4 WAP to swap two numbers using call by value & call by
reference method.
9 Structure & Union 28-29
9.1 WAP to print details of a student using structure.
9.2 WAP to print details of 10 books using structure.
9.3 WAP to implement union.

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.

11 File handling functions 28-29


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().

12 Command line arguments 30-33


12.1 WAP to implement simple program of command line.

Page | 2
3

DO’S AND DONT’S

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

Object:- To learn about the C Library, Preprocessor directive, Input-output


statement.

Introduction:- Before a C program is compiled in a compiler, source code is processed


by a program called preprocessor. This process is called preprocessing.
Commands used in preprocessor are called preprocessor directives and they begin
with “#” symbol.

1.1 Program to Display "Hello, World!"

#include <stdio.h>
int main()
{
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}

Output
Hello, World!

1.2 Program to Print an Integer

#include <stdio.h>
int main()
{
int number;

// printf() dislpays the formatted output


printf("Enter an integer: ");

// scanf() reads the formatted input and stores them


scanf("%d", &number);

// printf() displays the formatted output


printf("You entered: %d", number);
return 0;
}

Output
Enter a integer: 25
You entered: 25

Page | 4
5

1.3 Program to Add Two Integers

#include <stdio.h>
int main()
{
int firstNumber, secondNumber, sumOfTwoNumbers;

printf("Enter two integers: ");

// Two integers entered by user is stored using scanf() function


scanf("%d %d", &firstNumber, &secondNumber);

// sum of two numbers in stored in variable sumOfTwoNumbers


sumOfTwoNumbers = firstNumber + secondNumber;

// Displays sum
printf("%d + %d = %d", firstNumber, secondNumber, sumOfTwoNumbers);

return 0;
}

Output
Enter two integers: 12
11
12 + 11 = 23

1.4 Program to calculate area of rectangle

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

int main() {
int length, breadth, area;

printf("\nEnter the Length of Rectangle : ");


scanf("%d", &length);

printf("\nEnter the Breadth of Rectangle : ");


scanf("%d", &breadth);

area = length * breadth;


printf("\nArea of Rectangle : %d", 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
}

2.1 WAP to calculate simple Interest.

#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);

printf("\nEnter Period of Time : ");


scanf("%d", &time);

si = (amount * rate * time) / 100;


printf("\nSimple Interest : %d", si);

return(0);
}
Output :
Enter Principal Amount : 500
Enter Rate of interest : 5
Enter Period of Time : 2
Simple Interest : 50

Page | 6
7

2.2 WAP to Swap 2 numbers using third variable.

Swapping: This technique in C programming, mean the ability to either:

• Swap two values or


• Swap two references.

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

Without using third variable-

#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

2.3 WAP to check whether the no is Even or Odd.

#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

3.1 WAP to print largest of three numbers.

#include <stdio.h>
int main()
{
double n1, n2, n3;

printf("Enter three numbers: ");


scanf("%lf %lf %lf", &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

3.2 WAP to implement a simple calculator using switch case.

// Performs addition, subtraction, multiplication or division depending the input from


user

# include <stdio.h>

int main() {

char operator;
double firstNumber,secondNumber;

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


scanf("%c", &operator);

printf("Enter two operands: ");


scanf("%lf %lf",&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;

// operator doesn't match any case constant (+, -, *, /)


default:
printf("Error! operator is not correct");
}

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.

4.1 WAP to print natural numbers using while loop.

#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

4.2 WAP to print odd numbers using do while loop.

#include <stdio.h>

int main()
{
//loop counter declaration
int number;
//variable to store limit /N
int n;

//assign initial value


//from where we want to print the numbers
number=1;

//input value of N
printf("Enter the value of N: ");
scanf("%d",&n);

//print statement
printf("Odd Numbers from 1 to %d:\n",n);

//do-while loop, that will print numbers


do
{
//Here is the condition to check ODD number
if(number%2 != 0)
printf("%d ",number);

// increasing loop counter by 1


number++;

Page | 13
14

}while(number<=n);

return 0;
}
Output:
Enter the value of N: 10
Odd Numbers from 1 to 10:
13579

4.3 WAP to print sum of the digits of a number

#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

You might also like