Computer Science, asked by sweetmehak751, 10 months ago

Write 3 programs using arrays

Answers

Answered by saranganand2018
0

Answer:

Explanation:

1 program

#include <stdio.h>

int main()

{

   int avg = 0;

   int sum =0;

   int x=0;

   /* Array- declaration – length 4*/

   int num[4];

   /* We are using a for loop to traverse through the array

    * while storing the entered values in the array

    */

   for (x=0; x<4;x++)

   {

       printf("Enter number %d \n", (x+1));

       scanf("%d", &num[x]);

   }

   for (x=0; x<4;x++)

   {

       sum = sum+num[x];

   }

   avg = sum/4;

   printf("Average of entered number is: %d", avg);

   return 0;

}

Output:

Enter number 1  

10

Enter number 2  

10

Enter number 3  

20

Enter number 4  

40

Average of entered number is: 20

2 program

// Program to take 5 values from the user and store them in an array

// Print the elements stored in the array

#include <stdio.h>

int main() {

 int values[5];

 printf("Enter 5 integers: ");

 // taking input and storing it in an array

 for(int i = 0; i < 5; ++i) {

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

 }

 printf("Displaying integers: ");

 // printing elements of an array

 for(int i = 0; i < 5; ++i) {

    printf("%d\n", values[i]);

 }

 return 0;

}

Output

Enter 5 integers: 1

-3

34

0

3

Displaying integers: 1

-3

34

0

3

3 program

// Program to find the average of n numbers using arrays

#include <stdio.h>

int main()

{

    int marks[10], i, n, sum = 0, average;

    printf("Enter number of elements: ");

    scanf("%d", &n);

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

    {

         printf("Enter number%d: ",i+1);

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

         

         // adding integers entered by the user to the sum variable

         sum += marks[i];

    }

    average = sum/n;

    printf("Average = %d", average);

    return 0;

}

Output

Enter n: 5

Enter number1: 45

Enter number2: 35

Enter number3: 38

Enter number4: 31

Enter number5: 49

Average = 39

pls mark this as a brainliest answer

took me a lot of effort to do

Answered by Ankit02
0

class ankit

{

   public static void main()

   {

       int a[]= {1,2,3,4,6,7,8,9,5,20,19,18,17,16,15,14,13,12,11,10};

       int length = a.length;

       int temp,sum=0;

       for(int i=0; i<length; i++)

       {

           for(int j= 0; j<length-1-i; j++)

           {

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

               {

                   temp= a[j];

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

                   a[j+1]= temp;

               }

           }

       }

       System.out.println("LARGEST NUMBER = " + a[length-1]);

       System.out.println("Smallest NUMBER = " + a[0]);

       for(int i=0; i<length ; i++)

       {

           sum+=a[i];

       }

       System.out.println("SUM = " + sum);

   }

}

       

Similar questions