0% found this document useful (0 votes)
109 views15 pages

PST LAB Mannual

Uploaded by

sagarkoushik509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views15 pages

PST LAB Mannual

Uploaded by

sagarkoushik509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1.

Write a program to read radius of a circle and to find area and


circumference

#include<stdio.h>

int main()
{

int rad;
float PI = 3.14, area, ci;

printf("\nEnter radius of circle: ");


scanf("%d", &rad);

area = PI * rad * rad;


printf("\nArea of circle : %f ", area);

ci = 2 * PI * rad;
printf("\nCircumference : %f ", ci);

return (0);
}
2. Write a program to read three numbers and find the biggest of three.

#include <stdio.h>
void main()
{
int n1, n2, n3;
printf("Enter three numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);

if (n1 >= n2)


{
if (n1 >= n3)
printf("%d is the largest number.", n1);
else
printf("%d is the largest number.", n3);
}
else
{
if (n2 >= n3)
printf("%d is the largest number.", n2);
else
printf("%d is the largest number.", n3);
}
}
3. Write a program to check whether the number is prime or not
#include <stdio.h>

int main()
{
int num;
printf("Enter the number\n");
scanf("%d",&num);
int count=0;
for(int i=2;i<num;i++) //Check for factors
{
if(num%i==0)
count++;
}
if(count!=0) //Check whether Prime or not
{
printf("Not a prime number\n");
}
else
{
printf("Prime number\n");
}
return 0;
}
4. Write a program to read a number, find the sum of the digits, reverse the
number and check it for palindrome

# include<stdio.h>
# include<conio.h>
void main()
{
intnum,x;
int sum=0,rev=0,rem;
clrscr();
printf("\n Enter a number : ");
scanf("%d",&num);
x=num;
while(num>0)
{
rem=num%10;
sum=sum+rem;
rev=rev*10+rem;
num=num/10;
}
printf("\n Sum of digits = %d",sum);
printf("\n Reversed number = %d",rev);
if(x==rev)
printf("\n Number is a Palindrome");
else
printf("\n Number is not a Palindrome");

getch();
}

5. Write a program to read numbers from keyboard continuously till the


user presses 999 and to find the sum of only positive numbers
#include<stdio.h>
int main()
{
intx,sum=0;
printf(“Enter the numbers both positive and negative numbers\n”);
printf(“Enter 999 at the end \n”);
while(1)
{
scanf(“%d”,&x);
if(x==999)
break;
if(x>0)
sum=sum+x;
}
printf(“\n Sum of the positive numbers entered is : %d”,sum);
}
6. Write a program to read percentage of marks and display
appropriate message(else if ladder)

#include<stdio.h>
int main()
{
float percentage;
printf(“Enter the percentage of the students \n”);
scanf(“%f”,&percentage);
if(percentage>=75)
printf(“Scored First class with distinction”);
else
if(percentage>=50)
printf(“Scored Second class”);
else
if(percentage>=35)
printf(“Scored Third class”);
else
printf(“Sorry, failed, you need to improve”);
}
7. Write a c program to find the roots of a quadratic equation

#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%1f %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0)
{
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
// condition for real and equal roots
else if (discriminant == 0)
{
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else
{
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart,
realPart, imagPart);
}
return 0;
}
8.Writea program to read marks scored by n students and find
the average of marks
#include <stdio.h>
int main()
{
int n, i;
floatmarks[30], sum = 0, avg;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("%d. Enter number: ", i + 1);
scanf("%f", &marks[i]);
sum=sum+ marks[i];
}
avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}
9.write a C program to to remove Duplicate Element in a single
dimensional Array
#include <stdio.h>
int main ()
{

int a[50], i, j, k, n;
printf (" Define the number of elements in an array: ");
scanf (" %d", &n);
printf (" \n Enter %d elements of an array: \n ", n);

// use for loop to enter the elements one by one in an array


for ( i = 0; i<n; i++)
{
scanf (" %d", &a[i]);
}

printf (" \n Enter elements are”);


for ( i = 0; i<n; i++)
{
printf (" %d", a[i]);
}

// use nested for loop to find the duplicate elements in array


for ( i = 0; i<n; i ++)
{
for ( j = i + 1; j <n; j++)
{
// use if statement to check duplicate element
if ( a[i] == a[j])
{
// delete the current position of the duplicate element
for ( k = j; k < n - 1; k++)
{
a[k] = a [k + 1];
}
// decrease the size of array after removing duplicate element
n--;

// if the position of the elements is changes, don't increase the index j


j--;
}
}
}
/* display an array after deletion or removing of the duplicate
elements */
printf (" \n Array elements after deletion of the duplicate elements: ");
// for loop to print the array
for ( i = 0; i< n; i++)
{
printf (" %d \t", a[i]);
}
return 0;
}
10. Write a program to perform addition and subtraction of
matrices.
#include<stdio.h>
int main()
{
int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];
printf("\nEnter the number of rows and columns of the first matrix
\n\n");
scanf("%d%d", &m, &n);
printf("\nEnter the %d elements of the first matrix \n\n", m*n);
for(c = 0; c < m; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &first[c][d]);
printf("\nEnter the %d elements of the second matrix \n\n", m*n);
for(c = 0; c < m; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &second[c][d]);

/* printing the first matrix */


printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", first[c][d]);
}
printf("\n");
}

/* printing the second matrix */


printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", second[c][d]);
}
printf("\n");
}

/*
finding the SUM of the two matrices
and storing in another matrix sum of the same size
*/
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d];

// printing the elements of the sum matrix


printf("\n\nThe sum of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", sum[c][d]);
}
printf("\n");
}

/*
finding the DIFFERENCE of the two matrices
and storing in another matrix difference of the same size
*/
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
diff[c][d] = first[c][d] - second[c][d];

// printing the elements of the diff matrix


printf("\n\nThe difference(subtraction) of the two entered matrices is:
\n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", diff[c][d]);
}
printf("\n");
}
return 0;
}

11. Write a program to find the factorial of a number


#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++)
{
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}

You might also like