Arrays and Functions

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

ARRAYS AND FUNCTIONS

Arrays:
1. Find an Element in an Array
#include <stdio.h>

int main()

int arraySize,i;

// printf("Enter size of array : ");

scanf("%d",&arraySize);

int a[arraySize];

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

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

int n;

// printf("Enter element to search : ");

scanf("%d", &n);

for( i =0 ; i < arraySize && a[i] != n ; i++){

(a[i] == n)?printf("***element %d ount at index %d ***",a[i] , i) : printf("!!! Element not Found


!!!");

return 0;

}
ARRAYS AND FUNCTIONS

2. Sum and Average of an array


#include <stdio.h>

int main()

int arraySize,i;

// printf("Enter size of array : ");

scanf("%d",&arraySize);

int a[arraySize];

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

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

int sum = 0 , average;

// printf("Enter element to search : ");


ARRAYS AND FUNCTIONS

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

sum += (a[i]

);

printf("Sum of all Elements in array is %d \nAverage of all Elements in array is %d", sum ,
sum / arraySize);

return 0;

3. Transpose Given matrix

#include <stdio.h>

int main()
{
int r,c,i;
// printf("Enter size of array : ");
ARRAYS AND FUNCTIONS

scanf("%d %d",&r , &c);

int a[r][c];

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


for(int j =0 ; j<c ; j++)
scanf("%d",&a[i][j]);
}
// int sum = 0 , average;

printf("given Matrix : \n");

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

for(int j =0 ; j< c ; j++)


printf("%d " , a[i][j]);
printf("\n");
}

printf("\ntranspose matrix : \n");

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

for(int j =0 ; j< r ; j++)


printf("%d " , a[j][i]);
ARRAYS AND FUNCTIONS

printf("\n");
}

return 0;
}

4. Find largest number in Array :


ARRAYS AND FUNCTIONS

#include <stdio.h>

int main()

int r = 0,c,i,n;

printf("Enter size of array : ");

scanf("%d ",&n);

int a[n];

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

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

if( r < a[i])

r = a[i];

}
ARRAYS AND FUNCTIONS

printf("The largest number is %d \n", r);

return 0;

5. Array Sorting (Ascending):

#include <stdio.h>

int main()

{
ARRAYS AND FUNCTIONS

int r = 0,c,i,n;

printf("Sorted Array : \n");

scanf("%d ",&n);

int a[n];

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

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

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

for(int j =i ; j< n-1 ; j++ ){

if(a[j] > a[j+1]){

int t = a[j];

a[j] = a[j+1];

a[j+1] = t;

}
ARRAYS AND FUNCTIONS

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

printf("%d ", a[i]);

return 0;

6. Array Sorting (Descending) :


ARRAYS AND FUNCTIONS

#include <stdio.h>

int main()

int r = 0,c,i,n;

printf("Sorted Array : \n");

scanf("%d ",&n);

int a[n];

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

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

}
ARRAYS AND FUNCTIONS

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

int temp = a[i];

int j = i -1;

for(; j>=0 && temp >= a[j] ; j-- ){

a[j+1] = a[j];

a[j+1] = temp;

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

printf("%d ", a[i]);

return 0;

}
ARRAYS AND FUNCTIONS

7. Count even numbers compute its sum:

#include <stdio.h>

int main()

int sum = 0,i,count = 0;

int a[20];
ARRAYS AND FUNCTIONS

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

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

if(a[i] % 2 ==0 ){

sum+=a[i];

count++;

printf("the sum of %d even numbers of the array is %d ", count ,sum);

return 0;

8. Smallest positive integer not a sum of other numbers:


#include <stdio.h>

int main() {

int i,n = 5;

int a[n];

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

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

printf("%d ", a[i]);


ARRAYS AND FUNCTIONS

printf("\n");

// Sorting of array

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

int temp = a[i];

int j = i -1;

for(; j>=0 && temp <= a[j] ; j-- ){

a[j+1] = a[j];

a[j+1] = temp;

// since the lowest value in the array can’t be formed by other elemrnts.

printf("Smallest number in array %d ", a[0]);

return 0;
ARRAYS AND FUNCTIONS

Functions:
1. Prime numbers between intervals:
#include <stdio.h>

#include <math.h>

int isPrime(int a){

if(a == 2)

return 2;

if (a % 2 == 0)
ARRAYS AND FUNCTIONS

return -1;

for(int i = 3 ; i< a/2 ; i=i+2 ){

if(a%i == 0)

return -1;

return a;

int checkPrime(int a, int b){

for(int i =a ;i <= b ; i++){

if(isPrime(i) != -1){

printf("%d ",i);

return 0;

int main() {

int i,n ,a,b;

scanf("%d %d",&a,&b);

n = b - a;
ARRAYS AND FUNCTIONS

checkPrime(a,b);

return 0;

}
ARRAYS AND FUNCTIONS

2. Sum of prime numbers

#include <stdio.h>

#include <math.h>

int isPrime(int a){

for(int i = b ; i< a/2 ; i=i+2 ){

if(a%i == 0)

return -1;

return a;

int primenum( int b){


ARRAYS AND FUNCTIONS

for(int i =3 ;i <= b/2 ; i+=2){

if(isPrime(i) != -1){

if(isPrime(b-i) != -1){

printf("%d + %d \n", i , b-i);

return 0;

int main() {

int i,n ,a,b;

scanf("%d ",&b);

// n = b - a;

primenum(b);

return 0;

}
ARRAYS AND FUNCTIONS

3. Factorial
#include <stdio.h>
int factorial(int a){
if(a==2)
return 2;
else
return a * factorial(a-1) ;
}

int main() {
int b;
scanf("%d ",&b);
printf("%d",factorial(b));
return 0;
}
ARRAYS AND FUNCTIONS

You might also like