PPSC Lab Manual
PPSC Lab Manual
Description:
printf(): The printf() function is used for output. It prints the given statement to the console
scanf():The scanf() function is used for input. It reads the input data from the console.
Source Code:
#include<stdio.h>
int main()
int number;
printf("enter a number:");
scanf("%d",&number);
return 0;
Output:
enter a number:5
cube of number is:125
1. B) Write a C Program to Perform Adding, Subtraction, Multiplication
and Division of two numbers
Description: There are five fundamental arithmetic operators supported by C language, which are
addition(+), subtraction(-), multiplication(*), division(/) and modulus(%) of two numbers. All arithmetic
operators compute the result of specific arithmetic operation and returns its result.
Source Code:
/*
* C Program for Addition, Subtraction, Multiplication, Division
* and Modulus of two numbers
*/
#include <stdio.h>
#include <conio.h>
int main(){
/* Variable declation */
int num1,num2;
int sum, difference, product, modulo;
float quotient;
/* Taking input from user and storing it in num1 and num2*/
printf("Enter First Number: ");
scanf("%d",&num1);
printf("Enter Second Number: ");
scanf("%d",&num2);
/* Adding two numbers */
sum = num1 + num2;
/* Subtracting two numbers */
Difference = num1 - num2;
/* Multiplying two numbers*/
product = num1 * num2;
/* Dividing two numbers by typecasting one operand to float*/
quotient = (float)num1 / num2;
/* returns remainder of after an integer division */
modulo = num1 % num2;
printf("\n Addition = %d", sum);
printf("\n Subtraction = %d", difference);
printf("\n Multiplication = %d", product);
printf("\n Division = %.3f", quotient);
printf("\n Modulus = %d", modulo);
getch();
return 0;
}
Output:
i) v=u+at
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int v,u,a,t;
clrscr();
printf("\n enter the initial velocity");
scanf("%d",&u);
printf("\n enter the accelartion");
scanf("%d",&a);
printf("\n enter the time");
scanf("%d",&t);
v=u+(a*t);
printf("\n the final velocity is%d",v);
getch();
}
OUTPUT:
ii) s=ut+1/2at2
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int s,u,a,t;
clrscr();
printf("\n enter the initial velocity");
scanf("%d",&u);
printf("\n enter the accelartion");
scanf("%d",&a);
printf("\n enter the time");
scanf("%d",&t);
s=(u*t)+(0.5*a*t*t);
printf("\n the speed is%d",s);
getch();
}
OUTPUT:
iii) v2-u2=2as
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int v,u,a,s;
clrscr();
printf("\n enter the initial velocity");
scanf("%d",&u);
printf("\n enter the accelartion");
scanf("%d",&a);
printf("\n enter the speed");
scanf("%d",&s);
v=sqrt((u*u)-(2*a*s));
printf("\n the final velocity is%d",v);
getch();
}
OUTPUT:
2. B) Write a C Program to convert Celsius to Fahrenheit and vice versa
Description:
We are going to perform the c program of temperature conversion from Fahrenheit to Celsius
and vice versa.
We can use the following formulas to do so:
C = (f-32)/1.8
f = 1.8 * C + 32
Source Code:
#include<stdio.h>
void main()
{
floatf,c;
printf("enter the temperature in centigrade\n");
scanf("%f",&c);
f=1.8*c+32;
printf("\n %f centigrade=%f fahrenheat",c,f);
printf("\n enter the temperature in fahrenheat");
scanf("%f",&f);
c=(f-32)/1.8;
printf("\n %f fahrenheat=%f centigrade",f,c);
}
Output:
enter the temperature in centigrade
34.2
34.200001 centigrade=93.559998 fahrenheat
enter the temperature in fahrenheat45.6
45.599998 fahrenheat=7.555555 centigrade
Exercise - 3 Control Flow – I
3 a) Write a C Program to Find Whether the Given Year is a Leap Year or not.
3 b) Write a C Program to Add Digits & Multiplication of a number
Source Code:
void main()
{
int year;
printf("Enter a year \n");
scanf("%d", &year);
if ((year % 400) == 0)
printf("%d is a leap year \n", year);
else if ((year % 100) == 0)
printf("%d is a not leap year \n", year);
else if ((year % 4) == 0)
printf("%d is a leap year \n", year);
else
printf("%d is not a leap year \n", year);
}
Output:
Enter a year
2012
2012 is a leap year
Enter a year
2009
2009 is not a leap year
3 B) Write a C program to add Digits & Multiplication of a number.
Source Code
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s=0,z=1;
printf("Enter a number\n");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
s = s + r; // s is used to calculate sum of the digits of a given number
z = z * r; // t is used to calculate product of the digits of a given number
n = n / 10;
} //closing of while
printf("\n Sum of the digits = %d",s);
printf("\n Product of the digits = %d",z);
getch();
} // closing for main
Output:
Enter a number
234
Sum of the digits = 9
Product of the digits = 24
Exercise - 4 Control Flow – II
4 a) i) Write a C Program to Find Whether the Given Number is Prime Number or Not
ii) Armstrong Number
b) Write a C program to print Floyd Triangle
4 A) i) Write a C program to find whether the given number is Prime number or not
Description:
Prime number is a number that is greater than 1 and divided by 1 or itself. In other words,
prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13,
17, 19, 23....are the prime numbers.
we will take an input from the user and check whether the number is prime or not.
Source Code:
#include <stdio.h>
main() {
int n, i, c = 0;
printf("Enter any number :");
scanf("%d", &n);
//logic
for (i = 1; i<= n; i++)
{
if (n % i == 0)
{
c++;
}
}
if (c == 2)
{
printf("Prime number");
}
else
{
printf("Not a Prime number");
}
return 0;
}
Output
Enter any number n: 7
Primenumber
Description:
Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371
and 407 are the Armstrong numbers.
Let's try to understand why 153 is an Armstrong number.
153 = (1*1*1)+(5*5*5)+(3*3*3)
where:
(1*1*1)=1
(5*5*5)=125
(3*3*3)=27
So:
1+125+27=153
Let's try to understand why 371 is an Armstrong number.
371 = (3*3*3)+(7*7*7)+(1*1*1)
where:
(3*3*3)=27
(7*7*7)=343
(1*1*1)=1
So:
27+343+1=371
Source code
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("Armstrong number ");
else
printf("not an Armstrong number");
return 0;
}
Output:
enter the number=153
Armstrong number
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Exercise – 5 Control Flow – III
5 a) Write a C Program to print Pascal Triangle
5 b) Write a C Program to make a simple Calculator to Add, Subtract, Multiply or Divide Using
Switch…case
Source Code:
#include<stdio.h>
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(int row=1; row<=n; row++)
{
int a=1;
for(int s=1; s<=40-row; s++)
printf(" ");
for(int =1; i<=row; i++)
{
printf("%d ",a);
a = a * (row-i)/i;
}
printf("\n");
}
return 0;
}
Output
Source Code:
void main()
{
int a,b,c,ch;
float d;
printf("\nEnter a b values ");
scanf("%d%d",&a,&b);
printf("\n\n1.ADD 2.SUB 3.MUL 4.DIV 5.EXIT");
printf("\nEnter Your Choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
c=a+b;
printf("\nThe sum is %d",c);
break;
case 2:
c=a-b;
printf("\nThe subtraction is %d",c);
break;
case 3:
printf("\nThe multiplication is %d",c);
break;
case 4:
d=(float)a/b;
printf("\nThe division is %f",d);
break;
case 5: exit(0);
default: printf("\n\tINVALID OPTION\n");
break;
}
}
OUTPUT:
6. a) Write a program in C for multiplication of two square Matrices
,
Source Code:
#include <stdio.h>
void main()
{
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,k,r1,c1,r2,c2,sum=0;
printf("\n\nMultiplication of two Matrices :\n");
printf("----------------------------------\n");
printf("\nInput the rows and columns of first matrix : ");
scanf("%d %d",&r1,&c1);
printf("\nInput the rows and columns of second matrix : ");
scanf("%d %d",&r2,&c2);
if(c1!=r2){
printf("Mutiplication of Matrix is not possible.");
printf("\nColumn of first matrix and row of second matrix must be
same.");
}
else
{
printf("Input elements in the first matrix :\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("Input elements in the second matrix :\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&brr1[i][j]);
}
}
printf("\nThe First matrix is :\n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("%d\t",arr1[i][j]);
}
21 15 12
54 42 33
24 18 15
6. b) Write a program in C to find transpose of a given matrix
Source Code:
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
Entered matrix:
1 3
5 6
Transpose of the matrix:
1 5
3 6
7. a) Write a C Program demonstrating of parameter passing in
Functions and returning values
Source Code:
#include<stdio.h>
float calculate_area(int);
void main()
{
int radius;
float area;
printf("\nEnter the radius of the circle : ");
scanf("%d",&radius);
area = calculate_area(radius);
printf("\nArea of Circle : %f ",area);
}
float calculate_area(int r)
{
float result;
result = 3.14 * r * r;
}
Sample output:
Enter the radius of the circle 10
Area of Circle:314.000
Are of Circle: 314.0000
7. b) Write a C Program illustrating Fibonacci, Factorial with Recursion
without Recursion
Output:
Enter n 5
Factorial is 120
Factorial without recursion:
Source code:
#include<stdio.h>
#include<conio.h>
void main()
{ int n;
void factorial(int);
clrscr();
printf("\nEnter n:");
scanf("%d",&n);
factorial(n);
getch();
}
void factorial(int m)
{
int f=1;
while(m>0)
f*=m--;
printf("\nFactorial=%d",f);
}
Output:
Enter n 5
Factorial is 120
Fibonacci with recursion:
Source code:
#include<stdio.h>
#include<conio.h>
int fibonacci(int);
void main()
{
int n, i = 0, c;
clrscr();
printf("\nEnter the value :");
scanf("%d",&n);
printf("\nFibonacci Series using recursion:\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\t", fibonacci(i));
i++;
}
getch();
}
int fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( fibonacci(n-1) + fibonacci(n-2) );
}
OUTPUT:
Enter the value: 10
Fibonacci series using recursion:
0 1 1 2 3 5 8 13 21 34
Fibonacci without recursion:
Source code:
#include<stdio.h>
#include<conio.h>
main()
{
int n, first = 0, second = 1, next, c;
clrscr();
printf("Fibonacci Series\n");
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;
second = next;
}
printf("%d\n",next);
}
getch();
return 0;
}
OUTPUT:
Fibonacci series
Enter the number of terms
10
First 10 terms of Fibonacci series are:-
0 1 1 2 3 5 8 13 21 34
Exercise – 8Functions
a) Write a program in C to add numbers using call by reference.
b) Write a program in C to swap elements using call by reference
#include <stdio.h>
#include <stdlib.h>
int main()
int res;
return res;
Output:
2 4
Addition of 2 and 4 is 6
8.b) Write a program in C to swap elements using call by reference
SOURCE CODE:
#include <stdio.h>
void swap(int * num1, int * num2);
int main()
{
int num1, num2;
printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);
printf("Before swapping in main n");
printf("Value of num1 = %d \n", num1);
printf("Value of num2 = %d \n\n", num2);
/* Pass the addresses of num1 and num2 */
swap(&num1, &num2);
/* Print the swapped values of num1 and num2 */
printf("After swapping in main n");
printf("Value of num1 = %d \n", num1);
printf("Value of num2 = %d \n\n", num2);
return 0;
}
void swap(int * num1, int * num2)
{
int temp;
temp = *num1;
*num1= *num2;
*num2= temp;
printf("After swapping in swap function n");
printf("Value of num1 = %d \n", *num1);
printf("Value of num2 = %d \n\n", *num2);
}
OUTPUT:
Enter two numbers: 10 20
Before swapping in main
Value of num1 = 10
Value of num2 = 20
After swapping in swap function
Value of num1 = 20
Value of num2 = 10
After swapping in main
Value of num1 = 20
Value of num2 = 10
Exercise – 9Arrays and Pointers
a) Write a C Program to Access Elements of an Array Using Pointer
b) Write a C Program to find the sum of numbers with arrays and pointers.
SOURCE CODE
#include<stdio.h>
Void main()
{
int *p,a[5],i;
p=&a[0];
printf("\nEnter the array values: \n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("\nThe values of array using pointer are: ");
for(i=0;i<5;i++)
{
printf("%d \t",*p);
p++;
}
}
output:
Enter the array values:
12345
The values of array using pointer are: 1 2 3 4 5
9.b) Write a C Program to find the sum of numbers with arrays and
pointers
SOURCE CODE:
#include<stdio.h>
main()
{
int *p,a[5],i,sum=0;;
p=&a[0];
printf("\nEnter the array values: \n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
{
sum = sum + *p;
p++;
}
printf("\nThe sum of array using pointer is: %d",sum);
}
output:
Enter the array values: 1 2 3 4 5
The sum of array using pointer is: 15
Exercise – 10Strings
a) Implementation of string manipulation operations with library function.
i) copy ii) concatenate iii) length iv) compare
b) Implementation of string manipulation operations without library function.
i) copy ii) concatenate iii) length iv) compare
SOURCE CODE
AIM: i) Write a C program to copy one string to another string with
library function.
#include<stdio.h>
#include<string.h>
main()
{
char name1[40],name2[20];
printf("\nEnter the name1: ");
scanf("%s",name1);
printf("\nEnter the changed name: ");
scanf("%s",name2);
strcpy(name1,name2);
printf("\nThe name is: %s\n",name1);
}
output:
Enter the name1: nani
Enter the changed name: mothilal
The name is: mothilal
AIM: ii) Write a C program to concatenate one string to another sting
with library
function.
#include<stdio.h>
#include<string.h>
main()
{
char fname[40],lname[20];
printf("\nEnter first name: ");
scanf("%s",fname);
printf("\nEnter the last name: ");
scanf("%s",lname);
strcat(fname,lname);
printf("\nThe full name is: %s\n",fname);
}
output:
Enter first name: mothi
Enter the last name: lal
The full name is: mothilal
AIM: iii) Write a C program to find length of string with library
function.
#include<stdio.h>
#include<string.h>
main()
{
char name[40],n;
printf("\nEnter name: ");
scanf("%s",name);
n=strlen(name);
printf("\nThe length name is: %d\n",n);
}
output:
Enter name: mothilal
The length name is: 8
AIM: iv) Write a C program compare one string to another string with
library function.
#include<stdio.h>
#include<string.h>
main()
{
char name1[40],name2[20];
printf("\nEnter the name1: ");
scanf("%s",name1);
printf("\nEnter the name2: ");
scanf("%s",name2);
if(strcmp(name1,name2)==0)
printf("\nBoth names are equal\n");
else
printf("\nBoth names are NOT equal\n");
}
output 1:
Enter the name1: mothi
Enter the changed name: mothi
Both names are equal
output 2:
Enter the name1: mothi
Enter the name2: Mothi
Both names are NOT equal
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *ptr, sum = 0;
SOURCED CODE:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) calloc(n, sizeof(int));
if(ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i) {
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
OUTPUT:
Enter number of elements: 5
Enter elements: 1
5
2
4
2
Sum = 14