Sei sulla pagina 1di 6

Practice Programs For Array In C | Programmerdouts

programmerdouts.blogspot.com/2019/06/practice-programs-for-array-in-c.html

Practice Programs
Doing Programs gives you the great understanding about what you
have learned.
Doing Programs gives you the Confidence.
So let's Start by doing some programming.

Q:Declare a integer array of 10 element and initialize it by some


integer values and calculate its mean?
Solution:

#include<stdio.h>

void main()
{

int marks[15]={63,65,48,95,45,48,78,65,23,78,78};
int i,sums=0; //initialization of sums variable to zero,
float mean; // because it should not start with any garbage value.
for(i=0;i<11;i++)
{
sums = sums + marks[i]; //at every iteration assigning (current value +
new_element) to new sums variable
}
mean = sums/11;

printf("\nMean of marks array is %f",mean);


}

Output:
Mean of marks array is 62.0000

Q:Declare a integer array and take no of elements and their value


from the user, and only print even numbers ?
Solution:
1/6
#include<stdio.h>

void main()
{
int i,n;
int arr[20];
printf("enter the number of elements:");
scanf("%d",&n);

//taking array element as input from the user.


for(i=0;i<i++)
{ printf("Enter the element:");
scanf("%d",&arr[i]);
}

//printing even numbers


printf("Even numbers are:");
for(i=0;i<i++)
{ //iterating through all values of 'arr' array
if(arr[i] % 2 ==0) //checking array element is even or not
{ //as you know that '%' operator gives remainder
//if remainder is 0 than number is even number otherwise
//number is odd.
printf("%d\t",arr[i]);
}
}
}

Output:

enter the number of elements:6

enter the number of elements:1


enter the number of elements:2
enter the number of elements:3
enter the number of elements:4
enter the number of elements:5
enter the number of elements:6

Even numbers are:2 4 6


Exercise:
Q:Declare a integer array and take no of elements and their value
from the user, and only print odd numbers ?

2/6
Q:Declare a integer array and take no of elements and their value
from the user, and print the maximum value?
Solution:

#include<stdio.h>

void main()
{
int i, n, maxs=0;
int arr[20];
printf("enter the number of elements:");
scanf("%d",&n);

//taking array element as input from the user.


for(i=0;i<i++)
{ printf("Enter the element:");
scanf("%d",&arr[i]);
}

//printing even numbers


for(i=0;i<i++)
{ //iterating through all values of 'arr' array
if(arr[i] > maxs) //checking array element is greater than max variable
{ //if element is greater than max value than,
//we are assigning that value to the max variable.
maxs = arr[i];
}
}
printf("%d is a maximum value.",maxs);
}

Output:

enter the number of elements:6

enter the number of elements:10


enter the number of elements:20
enter the number of elements:30
enter the number of elements:40
enter the number of elements:50
enter the number of elements:60

60 is a maximum value.
Exercise:
3/6
Q:Declare a integer array and take no of elements and their value
from the user,and print the minimum value?

Q:Declare 2-dimensional integer arrays and initialize them with


some value and do their addition and assign that result to another
2-dimensional array?
Solution:

4/6
#include<stdio.h>

void main()
{ int i,j;
int array1[3][3] = {{1,2,3},
{4,5,6}, //decleration and initializing the 2-d array
{7,8,9}};

int array2[3][3] = {{11,12,13},


{14,15,16}, //decleration and initializing the 2-d array
{7,8,9}};
{17,18,19}};

int result[3][3];

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


{
for(j=0;j<3;j++)
{ // at every iteration adding elements of both array of index i_th row
and j_th column
//and assigning to result array at index i_th row and j_th column.
result[i][j] = array1[i][j] + array2[i][j];
}
}

//printing array result.


printf("Addition of two matrix is :")
for(i=0; i<3 ;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",result[i][j]);
}
printf("\n");
}
}

Output:
Addition of two matrix is :
12 14 16
18 20 22
24 26 28

5/6
Exercise:
Q:Declare 2-dimensnional integer arrays and initialize them with
some value and do their Subtraction and assign that result to
another 2-dimensional array?

Further Topics:
Strings In C
What are Strings
Concept of strings
Input And Output Functions
How to take strings As Input
What is gets() functions
What is puts() functions
2D Strings
Concept of 2D Strings
What is Character array
what are various string handling functions
Functions for Characters
Functions from #include<string.h> Header File
what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
strcmp() VS strncmp() VS strcmpi()
Functions from #include<ctype.h> Header File
what are isdigit(), islower() , isupper(),etc
Various Practice Programs on strings

6/6

Potrebbero piacerti anche