26-36 C Lab
26-36 C Lab
#include <stdio.h>
void main()
{
int x, y, z;
printf("Enter three numbers: ");
scanf("%d %d %d", &x, &y, &z);
Output:
#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>
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);
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 main()
{
int size, target;
printf("Enter size of array: ");
scanf("%d", &size);
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;
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;
#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);
return 0;
}
Output:
#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,