Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Lab Program Solution in PDF

Uploaded by

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

Lab Program Solution in PDF

Uploaded by

anupraj020402
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

PPS LAB PROGRAM(BCS-151P)

PROGRAM NO. 0
WAP to print hello world.

#include<stdio.h>
void main()
{
printf("Hello World!");
}
Output:

Hello World!

PROGRAM NO. 1
WAP that accepts the marks of 5 subjects and finds the sum and percentage marks obtained
by the student.
#include<stdio.h>
void main()
{
int hindi, english, science,math,computer,sum ;
float per;
printf("Enter marks of Hindi=");
scanf("%d",&hindi);
printf("Enter marks of English=");
scanf("%d",&english);
printf("Enter marks of Science=");
scanf("%d",&science);
printf("Enter marks of Math=");
scanf("%d",&math);
printf("Enter marks of Computer=");
scanf("%d",&computer);

sum=hindi+english+science+math+computer;
printf("Sum of marks=%d",sum);

per=(float)sum/5;
printf("Percentage of marks=%f",per);
}
Output:
Enter marks of Hindi=56
Enter marks of English=65
Enter marks of Science=78
Enter marks of Math=86
Enter marks of Computer=56

Sum of marks=341
Percentage of marks=68.199997

PROGRAM NO. 2
WAP that calculates the Simple Interest and Compound Interest. The Principal, Amount, Rate
of Interest and Time are entered through the keyboard.

#include<stdio.h>
#include<math.h>
int main()
{
float p, r, t, a, si, ci;
printf("Enter Principle=");
scanf("%f",&p);
printf("Enter Rate=");
scanf("%f",&r);
printf("Enter Time=");
scanf("%f",&t);

si=(p*r*t)/100;

printf("Simple Interest=%f",si);
a = p*(pow((1 + r / 100), t));
ci = a - p;
printf("\nCompound Interest=%f",ci);
return 0;
}
Output:
Enter Principle=100
Enter Rate=10
Enter Time=3
Simple Interest=30.000000
Compound Interest=33.100010

PROGRAM NO. 3

WAP to calculate the area and circumference of a circle.

#include<stdio.h>
int main()
{
float r,c,a,pi=3.14;

printf("Enter radius=");
scanf("%f",&r);
c=2*pi*r;
a=pi*r*r;
printf("\nCircumference of a circle=%f",c);
printf("\nArea of a circle=%f",a);
return 0;

}
Output:
Enter radius=7

Circumference of a circle=43.960003
Area of a circle=153.860016

PROGRAM NO.4
WAP that accepts the temperature in Centigrade and converts into Fahrenheit using the
formula C/5=(F-32)/9.
#include<stdio.h>
int main()
{
float c,f;
printf("Enter Centigrade=");
scanf("%f",&c);
f=(9*c)/5+32;
printf("Fahrenheit=%f",f);
}
Output:
Enter Centigrade=36
Fahrenheit=96.800003

PROGRAM NO. 5
WAP that swaps values of two variables using a third variable.
#include<stdio.h>
void main()
{
int a,b,temp;
printf("Enter a=");
scanf("%d",&a);
printf("Enter b=");
scanf("%d",&b);

temp=a;
a=b;
b=temp;
printf("\nAfter swapping");
printf("\na=%d",a);
printf("\nb=%d",b);
}
Output:
Enter a=10
Enter b=20

After swapping
a=20
b=10

PROGRAM NO. 6
WAP that checks whether the two numbers entered by the user are equal or not.
#include<stdio.h>
void main()
{
int a,b;
printf("Enter a=");
scanf("%d",&a);
printf("Enter b=");
scanf("%d",&b);

if(a==b)
{
printf("\na and b are equal.");
}
else
{
printf("\na and b are not equal.");
}
}
Output:
Enter a=10
Enter b=12

a and b are not equal.


PROGRAM NO.7
WAP to find the greatest of three numbers.
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter a=");
scanf("%d",&a);
printf("Enter b=");
scanf("%d",&b);
printf("Enter c=");
scanf("%d",&c);

if(a>b)
{
if(a>c)
{
printf("\na is greatest.");
}
else
{
printf("\nc is greatest.");
}
}
else
{
if(b>c)
{
printf("\nb is greatest.");
}
else
{
printf("\nc is greatest.");
}
}
}
Output:
Enter a=10
Enter b=15
Enter c=12

b is greatest.
PROGRAM NO. 8
WAP that finds whether a given number is even or odd.
#include<stdio.h>
void main()
{
int n;
printf("Enter a number=");
scanf("%d",&n);

if(n%2==0)
{
printf("\nn is even number.");
}
else
{
printf("\nn is odd number.");
}
}
Output:
Enter a number=15

n is odd number.
PROGRAM NO. 9
WAP that tells whether a given year is a leap year or not.
#include<stdio.h>
void main()
{
int year;
printf("Enter a number=");
scanf("%d",&year);

if((year%4==0||year%400==0)&&year%100!=0)
{
printf("\nYear is leap year.");
}
else
{
printf("\nYear is not leap year.");
}
}
Output:
Enter a number=1600

Year is not leap year.


PROGRAM NO. 10
WAP that accepts marks of five subjects and finds percentage and prints grades according to
the following criteria:
Between 90-100%————–Print „A‟
80-90%—————————-Print „B‟
60-80%—————————Print „C‟
Below 60%———————-Print „D‟

#include<stdio.h>
void main()
{
int hindi, english, science,math,computer,sum ;
float per;
printf("Enter marks of Hindi=");
scanf("%d",&hindi);
printf("Enter marks of English=");
scanf("%d",&english);
printf("Enter marks of Science=");
scanf("%d",&science);
printf("Enter marks of Math=");
scanf("%d",&math);
printf("Enter marks of Computer=");
scanf("%d",&computer);

sum=hindi+english+science+math+computer;
printf("\nSum of marks=%d",sum);

per=(float)sum/5;
printf("\nPercentage of marks=%f",per);

if(per>=90&&per<=100)
{
printf("\nGrade A");
}
else if(per>=80&&per<90)
{
printf("\nGrade B");
}
else if(per>=60&&per<80)
{
printf("\nGrade C");
}
else if(per<60)
{
printf("\nGrade D");
}
}
Output:
Enter marks of Hindi=45
Enter marks of English=65
Enter marks of Science=89
Enter marks of Math=78
Enter marks of Computer=65

Sum of marks=342
Percentage of marks=68.400002
Grade C
PROGRAM NO. 11
WAP that takes two operands and one operator from the user and perform the operation and
prints the result by using Switch statement.
#include<stdio.h>
void main()
{
int choice,a,b;
printf("Select your choice:\n");
printf("1- Add:\n");
printf("2- Sub:\n");
printf("3- Mul:\n");
printf("4- Div:\n");
printf("5- Mod:\n");
printf("Enter number a=");
scanf("%d",&a);
printf("Enter number b=");
scanf("%d",&b);
printf("Enter your choice=");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Add of a and b %d",(a+b));
break;
case 2:
printf("Sub of a and b %d",(a-b));
break;
case 3:
printf("Mul of a and b %d",(a*b));
break;
case 4:
printf("Div of a and b %d",(a/b));
break;
case 5:
printf("Mod of a and b %d",(a%b));
break;
default:
printf("Wronf choice.");

}
}
Output:
Select your choice:
1- Add:
2- Sub:
3- Mul:
4- Div:
5- Mod:
Enter number a=7
Enter number b=5
Enter your choice=3
Mul of a and b 35
PROGRAM NO. 12
WAP to print the sum of all numbers up to a given number.
#include <stdio.h>

int main()
{
int n, sum = 0, c, value;

printf("How many numbers you want to add?\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 1; c <= n; c++)


{
scanf("%d", &value);
sum = sum + value;
}

printf("Sum of the integers = %d\n", sum);

return 0;
}
OUTPUT:
How many numbers you want to add?
4
Enter 4 integers
1
2
3
4
Sum of the integers = 10
PROGRAM NO. 13
WAP to find the factorial of a given number.
#include <stdio.h>
int main()
{
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}

return 0;
}
Output:
Enter an integer: 10
Factorial of 10 = 3628800
PROGRAM NO. 14
WAP to print sum of even and odd numbers from 1 to N numbers.
#include<stdio.h>
void main()
{
int i,n,sumEven=0,sumOdd=0;
printf("Enter number=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
{
sumEven=sumEven+i;
}
else
{
sumOdd=sumOdd+i;
}
}
printf("\nSum of even numbers=%d",sumEven);
printf("\nSum of odd numbers=%d",sumOdd);
}
Output:
Enter number=10

Sum of even numbers=30


Sum of odd numbers=25
PROGRAM NO. 15
WAP to print the Fibonacci series.
#include<stdio.h>
void main()
{
int i,n,a=0,b=1,c;
printf("Enter number=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d\n",a);
c=a+b;
a=b;
b=c;
}
}
Output:
Enter number=10
0
1
1
2
3
5
8
13
21
34
PROGRAM NO. 16
WAP to check whether the entered number is prime or not.
#include<stdio.h>
void main()
{
int i,n,prime=1;
printf("Enter number=");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
prime=0;
break;
}
}
if(prime==1)
{
printf("Prime Number");
}
else
{
printf("Not Prime Number");
}
}
Output:
Enter number=29
Prime Number
PROGRAM NO. 17
WAP to find the sum of digits of the entered number.
#include<stdio.h>
void main()
{
int n,r,sumDigits=0;
printf("Enter number=");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sumDigits=sumDigits+r;
n=n/10;
}
printf("Sum of Digits=%d",sumDigits);
}
Output:
Enter number=12345
Sum of Digits=15
PROGRAM NO. 18
WAP to find the reverse of a number.
#include<stdio.h>
void main()
{
int n,r,rev=0;
printf("Enter number=");
scanf("%d",&n);
while(n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
printf("Reverse number=%d",rev);
}
Output:
Enter number=1234
Reverse number=4321
PROGRAM NO. 19
WAP to print Armstrong numbers from 1 to 1000.
A positive number is called armstrong number if it is equal to the sum of cubes of its digits.
Examples: 153 is Armstrong

(1*1*1)+(5*5*5)+(3*3*3) = 153

#include<stdio.h>
void main()
{
int i=1,r,aNum=0,num;
for(i=1;i<=1000;i++)
{
num=i;
aNum=0;
while(num>0)
{
r=num%10;
aNum=aNum+r*r*r;
num=num/10;
}
if(i==aNum)
{
printf("Armstrong Number=%d\n",i);
}
}
}
Output:
Armstrong Number=1
Armstrong Number=153
Armstrong Number=370
Armstrong Number=371
Armstrong Number=407
PROGRAM NO. 20
WAP to convert binary number into decimal number and vice versa.
#include<stdio.h>
#include<math.h>
void main()
{
int n,r,rev=0,p=0;
printf("Enter binary number=");
scanf("%d",&n);
while(n>0)
{
r=n%10;
if(r!=0)
{
rev= rev+(int)pow(2,p);
}
n=n/10;
p++;
}
printf("Decimal number=%d",rev);
}
Output:
Enter binary number=1011
Decimal number=11
PROGRAM NO. 21
WAP that simply takes elements of the array from the user and finds the sum of these
elements.
#include <stdio.h>

int main()
{
int arr[100];
int i, n, sum=0;

/* Input size of the array */


printf("Enter size of the array: ");
scanf("%d", &n);

/* Input elements in array */


printf("Enter %d elements in the array: ", n);
for(i=0; i<n; i++)
{
printf(“arr[i]=%d”,i);
scanf("%d", &arr[i]);

/*
* Add each array element to sum
*/

sum = sum + arr[i];


}

printf("Sum of all elements of array = %d", sum);

return 0;
}
OUTPUT:
Enter size of the array: 10
Enter 10 elements in the array: 10 20 30 40 50 60 70 80 90 100
Sum of all element of array=55010 20 30 40 50 60 70 80 90 100
Sum of all elements of array = 550

PROGRAM NO. 22
WAP that inputs two arrays and saves sum of corresponding
elements of these arrays in a third array and prints them.
#include<stdio.h>
void main()
{
int i,ar1[10],ar2[10],sum[10];
printf("Enter first array:-\n");
for(i=0;i<=9;i++)
{
printf("ar1[%d]=",i);
scanf("%d",&ar1[i]);
}
printf("Enter second array:-\n");
for(i=0;i<=9;i++)
{
printf("ar2[%d]=",i);
scanf("%d",&ar2[i]);
}

for(i=0;i<=9;i++)
{
sum[i]=ar1[i]+ar2[i];
printf("\nsum[%d]=%d",i,sum[i]);
}

}
Output:
Enter first array:-
ar1[0]=1
ar1[1]=2
ar1[2]=3
ar1[3]=4
ar1[4]=5
ar1[5]=6
ar1[6]=7
ar1[7]=8
ar1[8]=9
ar1[9]=10
Enter second array:-
ar2[0]=11
ar2[1]=22
ar2[2]=33
ar2[3]=44
ar2[4]=55
ar2[5]=66
ar2[6]=77
ar2[7]=88
ar2[8]=99
ar2[9]=100
Sum of arrays:-
sum[0]=12
sum[1]=24
sum[2]=36
sum[3]=48
sum[4]=60
sum[5]=72
sum[6]=84
sum[7]=96
sum[8]=108
sum[9]=110
PROGRAM NO. 23
WAP to find the minimum and maximum element of the array.
#include<stdio.h>
void main()
{
int i,ar[10],min,max;
printf("Enter array:-\n");
for(i=0;i<=9;i++)
{
printf("ar[%d]=",i);
scanf("%d",&ar[i]);
}
min=ar[0];
max=ar[0];
for(i=0;i<=9;i++)
{
if(min > ar[i])
{
min=ar[i];
}
if(max < ar[i])
{
max=ar[i];
}
}
printf("\nMin=%d",min);
printf("\nMax=%d",max);
}
Output:
Enter array:-
ar[0]=4
ar[1]=6
ar[2]=12
ar[3]=21
ar[4]=36
ar[5]=47
ar[6]=5
ar[7]=8
ar[8]=9
ar[9]=2

Min=2
Max=47
PROGRAM NO. 24
WAP to search an element in a array using Linear Search.
#include<stdio.h>
void main()
{
int i,ar[10],n,pos=-1;
printf("Enter array:-\n");
for(i=0;i<=9;i++)
{
printf("ar[%d]=",i);
scanf("%d",&ar[i]);
}
printf("Enter number to be search=");
scanf("%d",&n);
for(i=0;i<=9;i++)
{
if(n==ar[i])
{
pos=i+1;
}
}
if(pos==-1)
{
printf("Number not found.");
}
else
{
printf("Position=%d",pos);
}
}
Output:
Enter array:-
ar[0]=15
ar[1]=24
ar[2]=65
ar[3]=45
ar[4]=78
ar[5]=34
ar[6]=20
ar[7]=74
ar[8]=49
ar[9]=39
Enter number to be search=34
Position=6
PROGRAM NO. 25
WAP to sort the elements of the array in ascending order using Bubble Sort technique.
#include<stdio.h>
void main()
{
int i,j,ar[10],temp;
printf("Enter array:-\n");
for(i=0;i<=9;i++)
{
printf("ar[%d]=",i);
scanf("%d",&ar[i]);
}
for(i=0;i<=9;i++)
{
for(j=0;j<=9-i;j++)
{
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}

}
}
printf("Sorted array:-");
for(i=0;i<=9;i++)
{
printf("\n%d",ar[i]);
}
}
Output:
Enter array:-
ar[0]=2
ar[1]=5
ar[2]=4
ar[3]=12
ar[4]=21
ar[5]=32
ar[6]=45
ar[7]=15
ar[8]=16
ar[9]=34
Sorted array:-
2
4
5
12
15
16
21
32
34
41
PROGRAM NO. 26
WAP to add and multiply two matrices of order nxn.
#include<stdio.h>
void main()
{
int i,j,k,m1[3][3],m2[3][3],mul[3][3];
printf("Enter first matrix:-\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Enter second matrix:-\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&m2[i][j]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
mul[i][j]=0;
for(k=0;k<=2;k++)
{
mul[i][j]=mul[i][j]+(m1[i][k]*m2[k][j]);

}
}
}
printf("Multiplication of metrices:-\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d ",mul[i][j]);
}
printf("\n");
}
}
Output:
Enter first matrix:-
2
3
1
4
5
6
2
3
7
Enter second matrix:-
4
5
6
3
2
8
4
1
3
Multiplication of metrices:-
21 17 39
55 36 82
45 23 57
PROGRAM NO. 27
WAP that finds the sum of diagonal elements of a mxn matrix.
#include<stdio.h>
void main()
{
int i,j,k,matrix[3][3],sum=0;
printf("Enter matrix:-\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&matrix[i][j]);
}
}
printf("Matrix is:-\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d ",matrix[i][j]);
}
printf("\n");
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(i==j)
{
sum = sum + matrix[i][j];
}
}
}
printf("Sum of diagonal is = %d ",sum);

}
Output:
Enter matrix:-
1
2
3
4
5
6
7
8
9
Matrix is:-
1 2 3
4 5 6
7 8 9
Sum of diagonal is = 15
PROGRAM NO. 28
WAP to implement strlen (), strcat (),strcpy () using the concept of Functions.
#include<stdio.h>
#include <string.h>
void main()
{
char st[20]={'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l','d', '\0'};
printf("Length of '%s'=: %d\n",st,strlen(st));

char wd1[10]={'h', 'e', 'l', 'l', 'o', '\0'};


char wd2[10]={'w', 'o', 'r', 'l','d', '\0'};
strcat(wd1,wd2);
printf("\nFirst word is: %s\n",wd1);

char st1[20]={'I', 'n', 'd', 'i', 'a', '\0'};


char st2[20];
strcpy(st2,st1);
printf("\nString2 is: %s",st2);
}
Output:
Length of 'Hello World'=: 11

First word is: helloworld

String2 is: India


PROGRAM NO. 30
WAP to swap two elements using the concept of pointers.
#include<stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
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);
}
Output:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10

PROGRAM NO. 31
WAP to compare the contents of two files and determine whether they are same or not.
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *fp1 ;
FILE *fp2 ;

int cnt1 = 0;
int cnt2 = 0;
int flg = 0;

if( argc < 3 )


{
printf("Insufficient Arguments!!!\n");
printf("Please use \"program-name file-name1 file-name2\" format.\n");
return -1;
}

fp1 = fopen(argv[1],"r");
if( fp1 == NULL )
{
printf("\n%s File can not be opened : \n",argv[1]);
return -1;
}

// move file pointer to end and get total number of bytes


fseek(fp1,0,SEEK_END);
cnt1 = ftell(fp1);

fp2 = fopen(argv[2],"r");
if( fp2 == NULL )
{
printf("\n%s File can not be opened : \n",argv[2]);
return -1;
}

// move file pointer to end and get total number of bytes


fseek(fp2,0,SEEK_END);
cnt2 = ftell(fp2);

fseek(fp1,0,SEEK_SET);
fseek(fp2,0,SEEK_SET);

// check for the total number of bytes


if( cnt1 != cnt2 ){
printf("\nFile contents are not same\n");
}
else
{
while( ! feof(fp1) )
{
if( fgetc(fp1) != fgetc(fp2) )
{
flg = 1;
break;
}
}

if( flg ) printf("\nFile contents are not same.\n");


else printf("\nFile contents are same.\n");
}

fclose(fp1);
fclose(fp2);

return 0;
}
Output

Terminal command: ./compPrg file1.txt file2.txt


File contents are not same.
PROGRAM NO. 32
WAP to check whether a given word exists in a file or not. If yes then find the number of
times it occurs.
File contain lines:

how are you.


this is a boy
i am good
#include<stdio.h>
void main()
{

FILE* filePointer;
int wordExist=0;
int bufferLength = 255;
char search[100];
printf("Enter word to be search=");
scanf("%s",search);
char line[bufferLength];
filePointer = fopen("D:\\file.txt", "r");
while(fgets(line, bufferLength, filePointer))
{
char *ptr = strstr(line, search);
if (ptr != NULL)
{
wordExist=1;
break;
}
}
fclose(filePointer);
if (wordExist==1)
{
printf("Word exists.");
}
else
{
printf("Word doesn't exist.");
}
}
Output:
Enter word to be search=is
Word exists.

You might also like