C /C++ Program to enter elements of integer array & display the sum, C or C++ Program to display numbers with the help of array.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[5],sum=0;
clrscr();
printf("Enter the 5 numbers:\n");
for(i=0;i<5;i++)
{
scanf("%d", &a[i]);
sum=sum+a[i];
}
printf("\n");
for(i=0;i<5;i++)
{
printf("a[%d]=%d\n",i+1,a[i]);
}
printf("\nsum=%d\n",sum);
getch();
}
Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h"
2.) for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
3.) don't use "% "symbol and "&" sign in "cin>>" statement.
Click here to check the output
No comments:
Post a Comment