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;
}
-
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;
}
-
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;
}
-
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;
}
-
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;
}
-
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.
-
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;
}
-
Find out all duplicate elements of an array
-
Find out the number of duplicate elements of an array
-
Find out all unique elements of an array
-
Copy one array to other
-
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;
}
-
Sort the array in ascending order
Change the commented lines in above code.
-
Find out the frequency of occurrance of the elements in an array