Professional Documents
Culture Documents
CA2 Notes Array For Students1
CA2 Notes Array For Students1
CA2 Notes Array For Students1
What is an Array?
C Array is a collection of variables belongings to the same data type. You can store group of
data of same data type in an array.
Types of Array:
There are 2 types of C arrays. They are,
Syntax Example
Character array:
char str[10];
char str[10]={‘H’,‘a’,‘i’};
(or)
char str[0] = ‘H’;
char str[1] = ‘a’;
char str[2] = ‘i;
Example 2:
Write a C program to get five numbers from user. Compute and display the addition of even
numbers and product of odd numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,m=1,i,num[5];
clrscr();
for(i=0;i<5;i++)
{
printf("\n Enter number[%d]:",i+1);
scanf("%d",&num[i]);
}
printf("\n------------------------");
for(i=0;i<5;i++)
{
if(num[i]%2==0)
{
printf("\n Even Number :%d",num[i]);
a=a+num[i];
}
else
{
printf("\n Odd Number :%d",num[i]);
m=m*num[i];
}
}
printf("\n--------------------");
printf("\n Addition of Even Number:%d",a);
printf("\n Product of Odd Number :%d",m);
printf("\n--------------------");
getch();
}
void main()
{
int sum=0,i,num;
clrscr();
printf("Enter 5 numbers : ");
for(i=0; i<5; i++)
{
scanf("%d",&num);
sum=sum+num;
}
printf("Sum of all numbers = %d",sum);
getch();
}