KEMBAR78
PSPC Important Program For Internal I | PDF | Mathematical Analysis | Matrix Theory
0% found this document useful (0 votes)
44 views16 pages

PSPC Important Program For Internal I

The document contains programs and outputs for various algorithms including: 1) A program to find prime numbers between two numbers using a for loop and modulo operator. 2) A program to find the sum and average of 5 numbers using a for loop, initializing sum to 0, and calculating average. 3) Programs to solve quadratic equations, perform matrix addition and multiplication, find the transpose of a matrix, and sort arrays using selection sort. 4) A program to find an element in an array using linear and binary search, and perform insertion sort. 5) A program to calculate mean, median, mode and range of numbers in an array.

Uploaded by

rishipandian2006
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)
44 views16 pages

PSPC Important Program For Internal I

The document contains programs and outputs for various algorithms including: 1) A program to find prime numbers between two numbers using a for loop and modulo operator. 2) A program to find the sum and average of 5 numbers using a for loop, initializing sum to 0, and calculating average. 3) Programs to solve quadratic equations, perform matrix addition and multiplication, find the transpose of a matrix, and sort arrays using selection sort. 4) A program to find an element in an array using linear and binary search, and perform insertion sort. 5) A program to calculate mean, median, mode and range of numbers in an array.

Uploaded by

rishipandian2006
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/ 16

Write a C program to print all prime numbers between two given numbers?

PROGRAM:
#include<stdio.h>
int main()
{
int i,n,n1,n2,isprime;
printf("\n Enter first number: ");
scanf("%d",&n1);
printf("\n Enter second number: ");
scanf("%d",&n2);

printf("\n The Prime numbers between two given numbers are:\n");


for(n=n1;n<=n2;n++)
{
isprime=1;
for(i=2;i<n;i++)
{
if((n%i)==0)
{
isprime=0;
break;
}
}

if(isprime)
printf("%d\t",n);
}
return 0;
}
OUTPUT:
Enter first number: 10
Enter second number: 30
The Prime numbers between two given numbers are:
11 13 17 19 23 29
Write algorithm, flowchart, and C program for to find sum and average of five numbers?
ALGORITHM:
Step 1: START
Step 2: READ a[0..4]
Step 3: INITIALIZE sum:=0
Step 4: FOR I := 0 TO 4 DO
SET sum := sum + a[i]
NEXT
Step 5: COMPUTE average := sum / 5
Step 6: PRINT “SUM=”, sum, “, AVERAGE=”,average
Step 7: STOP
FLOWCHART:

1
PROGRAM:
#include<stdio.h>
int main()
{
unsigned int a[5],i,sum=0;
float average;
for(i=0;i<5;i++)
{
printf("\n Enter number %d: ",i+1);
scanf("%u",&a[i]);
sum+=a[i];
}
printf("Sum=%u",sum);
average=sum/5.0;
printf("\nAverage=%f",average);
return 0;
}

OUTPUT:
Enter number 1: 10
Enter number 2: 20
Enter number 3: 30
Enter number 4: 40
Enter number 5: 50
Sum=150
Average=30,000000

2
Write a C program to solve quadratic equation ax^2+bx+c to find root?
PROGRAM:
#include <math.h>
#include <stdio.h>
int main() {
float a, b, c, d, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c:\n");
scanf("%f %f %f", &a, &b, &c);

d = b * b - 4 * a * c;

// condition for real roots


if (d > 0)
{
root1 = (-b + sqrt(d)) / (2 * a);
root2 = (-b - sqrt(d)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}

// condition for real and equal roots


else if (d == 0)
{
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf", root1);
}
// if roots are imaginary
else
{
realPart = -b / (2 * a);
imagPart = sqrt(-d) / (2 * a);
printf("root1 = %.2f+%.2fi", realPart, imagPart);
printf("\nroot2 = %.2f-%.2fi", realPart, imagPart);
}

return 0;
}
OUTPUT:
Enter coefficients a, b and c:
1 5 6
root1 = -2.00 and root2 = -3.00
===rerun========
Enter coefficients a, b and c:
1 2 3
root1 = -1.00+1.41i
root2 = -1.00-1.41i
===rerun======
Enter coefficients a, b and c:
2 4 2

3
root1 = root2 = -1.00
1. Write a C program to perform matrix addition:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10],r1,c1,r2,c2,i,j,k;
printf("enter row size of first matrix?\n");
scanf("%d",&r1);
printf("enter column size of first matrix?\n");
scanf("%d",&c1);
printf("enter row size of second matrix?\n");
scanf("%d",&r2);
printf("enter column size of second matrix?\n");
scanf("%d",&c2);
if(r1 != r2 || c1 != c2 )
{
printf("\nmatrix addition not possible");
return 1;
}

printf("\nEnter first matrix(%d X %d):\n",r1,c1);


for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);

printf("\nEnter second matrix(%d X %d):\n",r2,c2);


for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);

for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
c[i][j]=a[i][j]+b[i][j];

printf("Resultant matrix:\n");

for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",c[i][j]);

4
printf("\n");
}
return 0;
}

Output:

enter row size of first matrix?


3
enter column size of first matrix?
3
enter row size of second matrix?
3
enter column size of second matrix?
3

Enter first matrix(3 X 3):


1 2 3
4 5 6
7 8 9

Enter second matrix(3 X 3):


9 8 7
6 5 4
3 2 1
Resultant matrix:
10 10 10
10 10 10
10 10 10

5
2. Write a C program to perform matrix multiplication:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10],r1,c1,r2,c2,i,j,k;
printf("enter row size of first matrix?\n");
scanf("%d",&r1);
printf("enter column size of first matrix?\n");
scanf("%d",&c1);
printf("enter row size of second matrix?\n");
scanf("%d",&r2);
printf("enter column size of second matrix?\n");
scanf("%d",&c2);
if(c1!=r2)
{
printf("\nmatrix multiplication not possible");
printf("\nReason:column size of 1st matrix != row size of 2nd matrix");
return 1;
}

printf("\nEnter first matrix(%d X %d):\n",r1,c1);


for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);

printf("\nEnter second matrix(%d X %d):\n",r2,c2);


for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);

for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}

6
printf("Resultant matrix:\n");

for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
return 0;
}

Output:
enter row size of first matrix?
2
enter column size of first matrix?
3
enter row size of second matrix?
3
enter column size of second matrix?
2

Enter first matrix(2 X 3):


1 2 3
4 5 6

Enter second matrix(3 X 2):


1 2
3 4
5 6
Resultant matrix:
22 28
49 64

7
3. C program to find transpose of the given matrix:
#include<stdio.h>
int main()
{
int a[10][10],t[10][10],i,j,r,c;
printf("Enter row size?\n");
scanf("%d",&r);
printf("enter column size?\n");
scanf("%d",&c);

printf("\nEnter the matrix(%d X %d):\n",r,c);


for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
t[j][i]=a[i][j];
}
printf("transpose of given matrix is:");
printf("\n====================\n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
printf("%6d",t[i][j]);
printf("\n");
}
return 0;
}
Output:
Enter row size?
2
enter column size?
3

Enter the matrix(2 X 3):


1 2 3
4 5 6
transpose of given matrix is:
=======================
1 4

8
2 5
3 6
C program to sort elements in the array using selection sort

output
enter the size of the array?7
enter 7 array elements in unsorted order:
100 4 1 43 32 21 67

9
The sorted array:
1 4 21 32 43 67 100
Write a C program to find element in the given array using linear search

Enter the size of the array?


7
Enter 7 array elements:
54 5 1 76 34 7 11
Enter the element to search:
100
100 not found in the array
=====rerun============
Enter the size of the array?
6
Enter 6 array elements:
87 4 56 34 21 1
Enter the element to search:
34
34 found in the array at index 3
Write a c program to find element in the given array using binary search

10
Insertion Sort Program

11
Enter Size of Array: 7
Enter 7 array elements:
100 2 23 45 31 72 89
after sorting array elements:
2 23 31 45 72 89 100
4. Write a C program to Compute Mean, Median and Mode of given N numbers using
one dimensional array
#include<stdio.h>
void findfreq_element(int arr[],int n)
{
int maxcount = 0, freq_element,count,i,j;
for (i = 0; i < n; i++)
{
count = 0;
for (j = 0; j < n; j++)

12
{
if (arr[i] == arr[j])
count++;
}

if (count > maxcount) {


maxcount = count;
freq_element = arr[i];
}
}

printf("\nMode=%d",freq_element);
}

int main()
{
int a[50],n,range,i;
float mean,median,sum=0;
printf("Enter the size of the arrar:");
scanf("%d",&n);
printf("Enter %d array elements in ascending order:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
printf("Mean=%.2f",(sum/n));

if(n%2!=0)
median=a[n/2];
else
median=(a[n/2-1]+a[n/2]) / 2.0;

printf("\nMedian=%.2f",median);
printf("\nRange=%d",a[n-1]-a[0]);
findfreq_element(a,n);
return 0;
}
Output:

13
Enter the size of the arrar: 5
Enter 5 array elements in ascending order:
1 3 3 3 6
Mean=3.20
Median=3
Range=5
Mode=3

program to find whether given string is palindrome?

5. C Program to print first n natural numbers

14
Output
How many natural number do you want to print?
7
The first 7 natural number are:
1 2 3 4 5 6 7
compute factorial of given number using user defined function

15
Output:
Enter the number to find factorial?
5
5!=120

16

You might also like