100% found this document useful (2 votes)
59 views

26-36 C Lab

The program takes input of a start and end range of numbers. It then checks each number between that range to identify prime numbers. This is done by checking if the number is perfectly divisible by any number between 2 and its half. Numbers meeting this criteria are printed as prime.

Uploaded by

Aryan
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
100% found this document useful (2 votes)
59 views

26-36 C Lab

The program takes input of a start and end range of numbers. It then checks each number between that range to identify prime numbers. This is done by checking if the number is perfectly divisible by any number between 2 and its half. Numbers meeting this criteria are printed as prime.

Uploaded by

Aryan
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/ 12

26. Write a program to Find GCD of Three Numbers.

#include <stdio.h>

int gcdOfNumbers(int x, int y)


{
int gcd = 1;

for (int i = 2; (i <= x && i <= y); i++)


{
if (x % i == 0 && y % i == 0) {
gcd = i;
}
}
return gcd;
}

void main()
{
int x, y, z;
printf("Enter three numbers: ");
scanf("%d %d %d", &x, &y, &z);

int gcd = gcdOfNumbers(gcdOfNumbers(x, y), z);

printf("GCD of %d, %d, %d = %d", x, y, z, gcd);


}

Output:

Enter three numbers: 10 5 50


GCD of 10, 5, 50 = 5
27. Write a program to Find Factorial of a Number using Functions.

#include <stdio.h>

void factorial(int n) {
int fact = 1;
for(int i=1; i<=n; i++){
fact *= i;
}
printf("Factorial of %d = %d", n, fact);
}

void main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
factorial(n);
}

Output:

Enter a number: 6
Factorial of 6 = 720
28. Write a program to find fibonacci series using recursion.

#include <stdio.h>

int fibonacci(int num)


{
if (num == 0) {
return 0;
}
else if (num == 1){
return 1;
}
else{
return fibonacci(num - 1) + fibonacci(num - 2);
}
}

void main()
{
int num;
printf("Enter a number : ");
scanf("%d", &num);
for (int i = 0; i < num; i++)
{
printf("%d, ", fibonacci(i));
}
}

Output:
Enter a number : 10
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
29. Write a program to Find Sum and Average of an array.

#include <stdio.h>

void main()
{
int size, sum = 0;
printf("Enter size of array: ");
scanf("%d", &size);

printf("Enter %d elements of array:", size);


int arr[size];
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
sum = sum + arr[i];
}
int average = sum / size;
printf("Sum of array: %d \n", sum);
printf("Average of array: %d", average);
}

Output:
Enter size of array: 5
Enter 5 elements of array:10 10 10 10 10
Sum of array: 50
Average of array: 10
30. Write a program to perform Linear Search.

#include <stdio.h>

void linearSearch(int arr[], int size, int target)


{
for (int i = 0; i < size; i++)
{
if (arr[i] == target){
printf("Target found at index %d", i);
return;
}
}
printf("Target not found");
}

void main()
{
int size, target;
printf("Enter size of array: ");
scanf("%d", &size);

printf("Enter %d elements of array:", size);


int arr[size];
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}

printf("Enter target: ");


scanf("%d", &target);

linearSearch(arr, size, target); // function calling


}

Output:
Enter size of array: 5
Enter 5 elements of array:44 33 55 99 66
Enter target: 55
Target found at index 2
31. Write a program to find sum of each row in a Matrix.

#include<stdio.h>

int main()
{
int i, j, rows, columns, a[10][10], Sum;

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


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

printf("Enter Elements of the Matrix: \n");


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

for(rows = 0; rows < i; rows++)


{
Sum = 0;
for(columns = 0; columns < j; columns++)
{
Sum = Sum + a[rows][columns];
}
printf("The Sum of Elements of %d Rows = %d \n",rows, Sum );
}

return 0;
}

Output:
Please Enter Number of rows and columns: 3 3
Enter Elements of the Matrix:
123
222
311
The Sum of Elements of 0 Rows = 6
The Sum of Elements of 1 Rows = 6
The Sum of Elements of 2 Rows = 5
32. Write a program to find the product of the two Matrix.

#include<stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10], n, i, j, k;

printf("Enter the matrix rows/column: ");


scanf("%d", & n);
printf("Enter the elements of Matrix-A: \n");

for (i = 0; i < n; i++) {


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

printf("Enter the elements of Matrix-B: \n");

for (i = 0; i < n; i++) {


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

for (i = 0; i < n; i++) {


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

printf("The product of the two matrices is: \n");


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

Enter the matrix rows/column: 3


Enter the elements of Matrix-A:
111
222
333
Enter the elements of Matrix-B:
123
123
123
The product of the two matrices is:
3 6 9
6 12 18
9 18 27

33. Write a program to find the sum of two matrix.

#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: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
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;
}

Output:

Enter the number of rows: 3


Enter the number of columns: 3

Enter elements of 1st matrix:


222
111
333
Enter elements of 2nd matrix:
111
222
333

Sum of two matrices:


3 3 3
3 3 3
6 6 6
Write a program To sort the array.

#include<stdio.h>
int main()
{
int arr[5],i,j,temp;
printf("Enter the element of array:\n");
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
{
if(arr[i]>arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("Sorted array is\n");
for(i=0;i<5;i++)
printf("%d\t",arr[i]);
}

OUTPUT:
Enter the element of array:
2121
121
2212
1
1121
Sorted array is
1 121 1121 2121 2212
Write a program To Print the Gcd of two number using Recursion.

#include<stdio.h>
int gcd(int a,int b)
{
if(a==0)
return b;
else if(b==0)
return a;
else
{
if(a>b){
a=a-b;
}
else{
b=b-a;
}
return gcd(a,b);
}
}
int main()
{
int a,b;
printf("Enter the value of a and b : ");
scanf("%d %d",&a,&b);
int ans;
ans = gcd(a,b);
printf("Gcd of %d and %d is %d",a,b,ans);
getch();
}

OUTPUT:
Enter the value of a and b : 53 6
Gcd of 53 and 6 is 1
Enter the value of a and b : 12 4
Gcd of 12 and 4 is 4
Write a program to print prime no between m to n:

#include <stdio.h>
int main()
{
int start,end,flag=0,primeNo;
printf("Enter start and end range\n");
scanf("%d %d",&start,&end);
printf("Prime numbers from %d to %d are\n",start,end);
for(int i=start;i<=end;i++){
if(i==0 || i==1){
flag=1;
}
primeNo=i;
for(int j=2;j<=i/2;j++){
if(primeNo%j==0){
flag=1;
break;
}
}
if(flag==0){
printf("%d, ",primeNo);
}
flag=0;
}
}

Output:
Enter start and end range
2
7
Prime numbers from 2 to 7 are
2, 3, 5, 7,

You might also like