0% found this document useful (0 votes)
25 views7 pages

PROGRAMS

The document contains 10 C programs covering various array concepts and algorithms including: 1. Finding the largest number in an array using a for loop. 2. Adding two matrices stored in two-dimensional arrays. 3. Reading and displaying n random numbers input from the user into an array. 4. Displaying a pyramid pattern of stars using nested loops. 5. Searching for an element in an array using linear search. 6. Calculating the factorial of a number using a recursive function. 7. Searching for an element in a sorted array using binary search. 8. Calculating the greatest common divisor of two numbers using a recursive function. 9. Printing the Fibonacci series
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)
25 views7 pages

PROGRAMS

The document contains 10 C programs covering various array concepts and algorithms including: 1. Finding the largest number in an array using a for loop. 2. Adding two matrices stored in two-dimensional arrays. 3. Reading and displaying n random numbers input from the user into an array. 4. Displaying a pyramid pattern of stars using nested loops. 5. Searching for an element in an array using linear search. 6. Calculating the factorial of a number using a recursive function. 7. Searching for an element in a sorted array using binary search. 8. Calculating the greatest common divisor of two numbers using a recursive function. 9. Printing the Fibonacci series
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/ 7

1.

C program to find largest number in array

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

prin?("\n Enter the size of the array: ");


scanf("%d", &n);

//Input array elements

prin?("\n Enter %d elements of the array: \n", n);

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


{
scanf(" %d", &a[i]);
}

//Declaring Largest element as the first element


largest = a[0];

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


{
if (largest < a[i])
largest = a[i];
}

prin?("\n largest element present in the given array is : %d", largest);

2. C program to add two matrices using two dimensional arrays.

#include<stdio.h>
main()
{
int a[10][10],i,j,m,n,b[10][10],c[10][10];
printf("Enter The row & column size\n");
scanf("%d%d",&m,&n);
printf("Enter the Elements to matrix -A\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[ i ][ j ]);
printf("Enter the Elements to matrix -B\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",b[ i ][ j ]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i ][ j ]=a[ i ][ j ]+b[ i ][ j ];

// to print sum matrix


printf("\n Addition of a & b matrices is \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d",c[i][j]);
printf("\n");
}
}

3. C program to read and display n random numbers using arrays.

int main()
{
int i, a[100],n;
printf(“Enter the size of an array “);
scanf(“%d”, &n); // read array size
printf(“Enter %d elements\n”, n);
for(i=0;i<n; i++)
scanf(“%d”, &a[i] ); // to read array elements

printf(“Array elements are \n”);


for(i=0;i<n; i++)
printf(“%d”, a[i]); // to print array elements
}

4. C program to display a pyramid of stars pattern using nested loops.

#include <stdio.h>
int main()
{
int i, j, rows;

printf("Enter number of rows: ");


scanf("%d", &rows);

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


{
for (j = 1; j <= rows - i; j++)
{
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++)
{
printf("*");
}
printf("\n");
}

5. C program to search an element using Linear search

#include<stdio.h>
int main()
{
int a[100],n,i,key,found=0;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the %d elements of array\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key to search\n");
scanf("%d",&key);
for(i=0;i<n-1;i++)
{
if(key==a[i])
{
found=1;
break;
}
}
if(found==1)
{
printf("Item found in position %d\n",i+1);
}
else
{
printf("Item not found\n");
}
}
6. C program to find the factorial of a number using a function.

#include<stdio.h>
int fact(int n)
{
if(n==1) return 1;
return n*fact(n-1);
}
int main()
{
int n, factorial;
prin?("Enter a number\n");
scanf("%d", &n);
factorial=fact(n);
prin?("factorial of num=%d“, factorial);
}

7. C program to search an element using binary search.

#include<stdio.h>
int main()
{
int i, n, a[10],mid, low, high, key, found=0;

printf("\n Enter the number of elements:\n");


scanf("%d", &n);
printf("Enter the array element in the ascending order\n");
for(i=0;i<n; i++)
{
scanf("%d", &a[i]);
}

printf("\n Enter the key element to be searched\n");


scanf("%d", &key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low +high )/2;
if(key==a[mid])
{
found=1;
break;
}
else if(key>a[mid])
low=mid+1;
else
high=mid-1;
}
if(found ==1)
printf(“Item found in position : %d”,mid+1);
else
printf("\n Item not found\n");
}

8. C program to calculate GCD of a number using a function.


#include <stdio.h>
int gcd(int m,int n);
int main()
{
int m,n,rem;
printf("Enter m&n\n");
scanf("%d%d",&m,&n);
rem=gcd(m,n);
printf("GCD of %d and %d =%d\n",m,n,rem);
}

int gcd(int m,int n)


{
if(n==0)
return m;
else
return gcd(n,m%n);
}
9. C program to print the Fibonacci series using recursion.

#include<stdio.h>
int fib(int num)
{

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

int main()
{
int num;
prin?("Enter the number of elements to be in the series : ");
scanf("%d", &num);
int i;
for (i = 0; i < num; i++)
{
prin?("%d, ", fib(i));
}
}

10. C program to implement bubble sort technique.

#include<stdio.h>
int main()
{
int a[100],n, i, j, temp;

printf("Enter the number of elements\n");


scanf("%d",&n);

printf("Enter the %d elements of array\n", n);


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

printf("The Input array is\n");


for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

printf("\nThe sorted array is\n");


for(i=0;i<n;i++)
printf("%d\t",a[i]);

You might also like