You are on page 1of 37

DATA STRUCTURES

PRACTICAL FILE

FOR

BACHELOR OF TECHNOLOGY
IN

ARTIFICIAL INTELLIGENT AND MACHINE LEARNING

SUBMITTED TO: SUBMITTED BY:


MS.NEETU NARANG MD IMRAN ALI
NAME OF PROGRAM

Write a C program to find sum and average of three numbers.

Write a C program to find the sum of individual digits of a given positive integer.

Write a C program to generate the first n terms of the Fibonacci sequence.

Write a C program to generate prime numbers between 1 to n.

Write a C program to Check whether given number is Armstrong Number or Not.

Write a C program to evaluate algebraic expression (ax+b)/(ax-b).

Write a C program to check whether given number is perfect number or Not.

Write a C program to check whether given number is strong number or not

Write a C program to find the roots of a quadratic equation.

Write a C prograrm perform arithmetic operations using switch statement.

Write a C program to find factorial of a given integer using non-recursive function.

Write a C program to find both the largest and smallest number in a list of integers.

Write a C Program to Sort the Array in an Ascending Order.

Write a C Program to find whether given matrix is symmetric or not.

Write a C program to perform addition of two matrices.

Write a C program that uses functions to perform Multiplication of Two Matrices.


Q. Write a C program to find sum and average of three numbers.

CODE:-
#include<stdio.h>

int main()
{
float a,b,c,sum;

printf("Enter any 1st number : ");


scanf("%f", &a);

printf("Enter any 1st number : ");


scanf("%f", &b);

printf("Enter any 1st number : ");


scanf("%f", &c);

printf("Sum of the number is : %f\n", a+b+c);

printf("Average is : %f", (a+b+c)/3);


}
Q. Write a C program to find the sum of individual digits of a given
positive integer.
CODE:-

#include<stdio.h>

void main()
{
int n,sum=0;
printf("Enter any +ve number : ");
scanf("%d", &n);

while(n>0){
sum = sum + n %10;
n = n/10;
}

printf("Sum of individual digits : %d", sum);


}
Q. Write a C program to generate the first n terms of the Fibonacci
sequence.

CODE:-

#include<stdio.h>

int main()
{
int a=0,b=1,c=0,n,i;
printf("How many number do you want from fibonacci series : ");
scanf("%d", &n);

printf("Fibonacci series : %d\t%d\t", a,b);

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


{
c = a+b;
a = b;
b = c;
printf("%d\t", c);
}
}
Q. Write a C program to generate prime numbers between 1 to n.

CODE:-

#include<stdio.h>

void main(){
int i,n,num,count;
printf("Enter the range of prime number : ");
scanf("%d", &n);

for(num = 1; num<=n; num++){


count = 0;
for(i=2; i<=num/2; i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!=1)
printf("%d\t", num);
}
}
Q. Write a C program to Check whether given number is Armstrong
Number or Not.

CODE:-

#include <stdio.h>

int main()
{
int num, orgNum, rem, result = 0;
printf("Enter any number to check Armstrong number : ");
scanf("%d", &num);
orgNum = num;

while (orgNum != 0)
{
rem = orgNum % 10;

result += rem * rem * rem;

orgNum /= 10;
}

if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);

return 0;
}
Q. Write a C program to evaluate algebraic expression (ax+b)/(ax-b).

CODE:-

*#include<stdio.h>

void main()
{
int a,b,x;
float exp;
printf("Enter the Values of a,b,x : ");
scanf("%d%d%d",&a,&b,&x);
exp=(a*x+b)/(a*x-b);
printf("The Resultant Value is : %f", exp);

}
Q.Write a C program to check whether given number is perfect
number or Not.

CODE:-
#include <stdio.h>

int main()
{
int num, rem, sum = 0, i;

printf("Enter a Number to check perfect number : ");


scanf("%d", &num);
for (i = 1; i <= (num - 1); i++)
{
rem = num % i;
if (rem == 0)
{
sum = sum + i;
}
}
if (sum == num)
printf("%d is perfect number", num);
else
printf("%d is not a perfect number", num);
return 0;
}
Q. Write a C program to check whether given number is strong
number or not.

CODE:-
#include<stdio.h>

int factorial(int n)
{
int i, fact = 1;
for (i = 1; i <= n; i++)
{
fact = fact * i;
}
return fact;
}

void main()
{
int num, n, sum = 0;
printf("Enter the number: ");
scanf("%d", &num);
int i = n;
while (i != 0)
{
sum = sum + factorial(i % 10);
i = i / 10;
}
if (sum == n)
{
printf("%d is strong number", num);
}
else
{
printf("%d is not a strong number", num);
}
}
Q. Write a C program to find the roots of a quadratic equation.

CODE:-
#include <stdio.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");
}
Q. Write a C program perform arithmetic operations using switch
statement.

CODE:-
#include<stdio.h>

void main()
{
float a,b;
int opt;
printf("Enter any two numbers : ");
scanf("%f %f",&a,&b);
printf(" Enter 1 for addition\n Enter 2 for Subtraction\n Enter
3 for Multiplication\n Enter 4 for Division\n");
printf("Enter your Choice : ");
scanf("%d",&opt);
switch(opt)
{
case 1 :
printf("Addition of %f and %f is : %f",a,b,a+b);
break;
case 2 :
printf("Subtraction of %f and %f is : %f",a,b,a-b);
break;
case 3 :
printf("Multiplication of %f and %f is : %f",a,b,a*b);
break;
case 4 :
printf("Division of two numbers is %f : ",a/b);
break;
default :
printf(" You enter wrong number. \n Enter correct
choice.");
break;
}
}
Q . a) Write a C program to find factorial of a given integer using non-
recursive function.
CODE:-
#include <stdio.h>
int factorial(int);
void main()
{
int n,fact;
printf(“Enter a positive integer: “);
scanf(“%d”, &n);
fact= factorial(n);
printf(“Factorial of %d = %d”,n, fact);
getch();

}
int factorial(int num)
{
int i=1,f=1;
while(i<=num)

{
f=f*i;
i++;
}
return f;
}
Q . Write a C program to find both the largest and smallest number in
a list of integers.
CODE:-
#include<stdio.h>
int main(){
int min,max,p,q,r,s;
printf("enter any four numbers:");
scanf("%d%d%d%d",&p,&q,&r,&s);
min=p;
max=p;
if(min>q) //checking 1st and 2nd number
min=q;
else if(max<q)
max=q;
if(min>r) //checking 1st and 3rd number
min=r;
else if(max<r)
max=r;
if(min>s) //checking 1st and 4th number
min=s;
else if(max<s)
max=s;
printf("Largest number from the given 4 numbers is:%d
",max);
printf("Smallest numbers from the given 4 numbers is:%d",min);
return 0;
}
Q . Write a C Program to Sort the Array in an Ascending Order?
CODE:-
#include <stdio.h>
void main (){
int num[20];
int i, j, a, n;
printf("enter number of elements in an array
");
scanf("%d", &n);
printf("Enter the elements
");
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
for (i = 0; i < n; ++i){
for (j = i + 1; j < n; ++j){
if (num[i] > num[j]){
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf("The numbers in ascending order is:
");
for (i = 0; i < n; ++i){
printf("%d
", num[i]);
}
}
Q . Write a C Program to find whether given matrix is symmetric or
not?
CODE:-
#include<stdio.h>

int main()
{
int i, j, row, col, count = 0;

printf("Please Enter Number of rows and columns: ");


scanf("%d %d", &i, &j);

if(i!=j)
{
printf("Rows not equal to columns. Therefore Non-Symmetric Matrix.");
exit(0);
}

//Initialize 2d-array of size i,j.


int a[i][j], b[i][j];

printf("\nEnter the Matrix Elements \n");


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

//Transpose of matrix
for(row = 0; row < i; row++)
{
for(col = 0;col < j; col++)
{
b[col][row] = a[row][col];
}
}

//Check if matrix a equals to matrix b or not.


for(row = 0; row < i; row++)
{
for(col = 0; col < j; col++)
{
if(a[row][col] != b[row][col])
{
count++;
break;
}
}
}
if(count == 0)
{
printf("\nThe given Matrix is a Symmetric Matrix ");
}
else
{
printf("\nThe given Matrix is Not a Symmetric Matrix ");
}

return 0;
}
Q . Write a C program to perform addition of two matrices?
CODE:-
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}

// adding two matrices


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}

// printing the result


printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

return 0;
}
Q . Write a C program that uses functions to perform Multiplication
of Two Matrices.?

CODE:-

#include <stdio.h>

#define r1 2
#define c1 3
#define r2 3
#define c2 2

void Matrix_mul (int mat1[][c1], int mat2[][c2])


{
int mul[r1][c2];
printf ("Multiplication of given two matrices is:\n");
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c2; j++)
{
mul[i][j] = 0;
for (int k = 0; k < r2; k++)
{
mul[i][j] += mat1[i][k] * mat2[k][j];
}
printf ("%d\t", mul[i][j]);
}
printf ("\n");
}
}

int main ()
{
int mat1[r1][c1] = { {0, 1, 2}, {3, 4, 5} };
int mat2[r2][c2] = { {1, 2}, {3, 4}, {5, 6} };
int mul[r1][c2], i, j, k;

printf ("matrix 1 is :\n");


for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
printf ("%d ", mat1[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}

printf ("matrix 2 is :\n");


for (i = 0; i < r2; i++)
{
for (j = 0; j < c2; j++)
{
printf ("%d ", mat2[i][j]);
if (j == 2 - 1)
{
printf ("\n\n");
}
}
}

Matrix_mul (mat1, mat2);


return 0;
}

You might also like