AlliedPractical_C
AlliedPractical_C
AlliedPractical_C
#include <stdio.h>
#include<conio.h>
void main()
{
int n, i, sum = 0;
clrscr();
printf("Enter a Number: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i)
{
sum =sum+ i;
}
printf("Sum = %d", sum);
getch();
}
OUTPUT
#include <stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
clrscr();
printf("Enter a,b,c: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c)
{
printf("a is Greater than b and c");
}
else if (b > a && b > c)
{
printf("b is Greater than a and c");
}
else if (c > a && c > b)
{
printf("c is Greater than a and b");
}
else
{
printf("all are equal or any two values are equal")
}
getch();
}
OUTPUT
Enter a,b,c: 10 5 7
a is Greater than b and c
3.TO SOLVE QUADRIC EQUATION
# include<stdio.h>
# include<conio.h>
# include<math.h>
void main ()
{
float a,b,c,r1,r2,d;
printf ("Enter the values of a b c: ");
scanf (" %f %f %f", &a, &b, &c);
d= b*b - 4*a*c;
if (d>0)
{
r1 = -b+sqrt (d) / (2*a);
r2 = -b-sqrt (d) / (2*a);
printf ("The real roots = %f %f", r1, r2);
}
else if (d==0)
{
r1 = -b/(2*a);
r2 = -b/(2*a);
printf ("Roots are equal =%f %f", r1, r2);
}
else
printf("Roots are imaginary");
getch();
}
OUTPUT
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float p, r, t;
clrscr();
printf("Enter Principal Amount: ");
scanf("%f", &p);
OUTPUT
#include <stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
if(n == 1)
{
printf("1 is neither prime nor composite.");
getch();
}
int count = 0;
for(int i = 2; i < n; i++)
{
if(n % i == 0)
count++;
}
if(count == 0)
{
printf("%d is a prime number.", n);
}
else
{
printf("%d is not a prime number.", n);
}
getch();
}
OUTPUT
#include <stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,j;
clrscr();
printf("Array size: ");
scanf("%d",&n);
printf("Elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (a[j] > a[i])
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
printf("\n\nAscending : ");
for (int i = 0; i < n; i++)
{
printf(" %d ", a[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (a[j] < a[i])
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
printf("\n\nDescending : ");
for (int i = 0; i < n; i++)
{
printf(" %d ", a[i]);
}
getch();
}
OUTPUT
Array size: 5
Elements: 3 6 2 8 9
Ascending : 2 3 6 8 9
Descending : 9 8 6 3 2
7. TO GENERATE THE FIBONACCI SEQUENCE
#include <stdio.h>
#include<conio.h>
void main()
{
int n, num1 = 0, num2 = 1, nextNum;
clrscr();
printf("Enter the number of Elements: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 1; i <= n; ++i)
{
printf("%d, ", num1);
nextNum = num1 + num2;
num1 = num2;
num2 = nextNum;
}
getch();
}
OUTPUT
#include<math.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
float num[10], deviation, sum, sumsqr, mean, variance, stddev;
sum = 0;
sumsqr =0;
n = 0;
clrscr();
printf("Enter number of elements:\n");
scanf("%d", &n);
printf("Input %d values \n", n);
for(i=0; i<n; i++)
{
scanf("%f", &num[i]);
sum += num[i];
}
mean = sum/(float)n;
printf("Mean is %f\n", mean);
for(i=0;i<n;i++)
{
deviation = num[i] - mean;
sumsqr += deviation * deviation;
}
variance = sumsqr/(float)n;
stddev = sqrt(variance);
printf("Standard Deviation is %f\n", stddev);
}
OUTPUT
#include <stdio.h>
#include<conio.h>
void addMatrix(int rows, int cols, int mat1[10][10], int mat2[10][10], int
result[10][10])
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
void subtractMatrix(int rows, int cols, int mat1[10][10], int mat2[10][10], int
result[10][10])
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result[i][j] = mat1[i][j] - mat2[i][j];
}
}
}
void main()
{
int rows, cols;
clrscr();
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int mat1[10][10], mat2[10][10], resultSum[10][10], resultDiff[10][10];
printf("Enter elements of matrix1:\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements of matrix2:\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
scanf("%d", &mat2[i][j]);
}
}
addMatrix(rows, cols, mat1, mat2, resultSum);
subtractMatrix(rows, cols, mat1, mat2, resultDiff);
printf("\nSum of matrices:\n");
displayMatrix(rows, cols, resultSum);
printf("\nDifference of matrices:\n");
displayMatrix(rows, cols, resultDiff);
getch();
}
OUTPUT
Difference of matrices:
-3 -3 -3
-3 -3 -3
-3 -3 -3
10. TO FIND THE MULTIPLICATION OF TWO MATRICES
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
clrscr();
system("cls");
printf("Enter the number of row=");
scanf("%d",&r);
printf("Enter the number of column=");
scanf("%d",&c);
printf("Enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
OUTPUT