# Practice questions with solutions on array

1. ### Ask for user inputs and store them in an array

``````#include <stdio.h>

int main(void){
int num1[5];
int i=0;

for(i=0;i<5;i++){
printf("Enter number : ");
scanf("%d",&num1[i]);
}
return 0;
}``````
2. ### Display all elements of an array.

``````#include <stdio.h>

int main(void){
int num1[5]={5,3,4,2,9};
int i=0;

for(i=0;i<5;i++){
printf("Number : %d \n",num1[i]);

}
return 0;
}``````
3. ### Display only even number elements of an array.

``````#include <stdio.h>
int main(void){
int num1[5]={5,3,4,2,9};
int i=0;

for(i=0;i<5;i++){
if(num1[i]%2==0){
printf("Number : %d \n",num1[i]);
}
}

return 0;
}``````
4. ### Display the sum of all elements of the array.

``````#include <stdio.h>
int main(void){
int num1[5]={5,3,4,2,9};
int i=0,sum=0;

for(i=0;i<5;i++){
sum=sum + num1[i];
}
printf("Sum : %d",sum);
return 0;
}``````
5. ### Display the highest number of the array.

``````#include <stdio.h>
int main(void){
int num[5]={12,14,11,15,11,10};
//int num[6]={-30,-32,-33,-28,-31,-35};
int hig_num=num[0];
int i;
for(i=0;i<6;i++){
if(num[i]>hig_num){
hig_num=num[i];
}
}
printf("Highest Number is %d",hig_num);
return 0;
}``````
6. ### Display the highest and lowest number of the array

``````#include <stdio.h>
int main(void){
int num[6]={16,14,9,15,11,10};
//int num[6]={-25,-32,-33,-28,-31,-24};
int hig_num=num[0];
int low_num=num[0];
int i;
for(i=0;i<6;i++){
if(num[i]>hig_num){
hig_num=num[i];
}else if(num[i]<low_num){
low_num=num[i];
}
}
printf("Highest Number is %d ",hig_num);
printf("\nLowest Number is %d ",low_num);
return 0;
}``````
Note : The else if check inside the for loop, it only checks if num[i] is not greater than hig_num.
7. ### Ask for user inputs to create the array and display the array in reverse order

``````#include <stdio.h>
int main(void){
int num[6];
int i;
for(i=0;i<6;i++){
printf("Enter %d number ",i);
scanf("%d",&num[i]);
}
// display in reverse order //
for(i=5;i>=0;i--){
printf("Position %d  is : %d \n",i,num[i]);
}
return 0;
}``````

12. ### Sort the array in descending order

``````#include <stdio.h>
int main(void){
int num[]={0,14,-11,15,11,-20};
//int num[6]={-30,-32,-33,-28,-31,-35};
int i=0,j=0;
int x=0;
for(i=0;i<6;i++){
for(j=i+1;j<6;j++){
if(num[i]<num[j]){ // descending order
//if(num[i]>num[j]){ // ascending order
x=num[i];
num[i]=num[j];
num[j]=x;
}
}
}
//////////
for(i=0;i<6;i++){
printf(" %d \n",num[i]);
}
/////////
return 0;
}``````
13. ### Sort the array in ascending order

Change the commented lines in above code.

## Subscribe

* indicates required
Subscribe to plus2net

plus2net.com

Post your comments , suggestion , error , requirements etc here

We use cookies to improve your browsing experience. . Learn more
 HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer