array-example
💻Example 4️⃣:
#include <iostream>
using namespace std;
int main() {
int marks[3];
cout<<"Enter number 0: ";
cin>>marks[0];
cout<<"Enter number 1: ";
cin>>marks[1];
cout<<"Enter number 2: ";
cin>>marks[2];
cout<<"Number0 => "<<marks[0]<<endl;
cout<<"Number1 => "<<marks[1]<<endl;
cout<<"Number2 => "<<marks[2]<<endl;
return 0;
}
⚙️ Output :
Enter number 0: 5
Enter number 1: 10
Enter number 2: 15
Number0 => 5
Number1 => 10
Number2 => 15
💻Example 5️⃣:
#include <iostream>
using namespace std;
int main() {
int marks[3];
for(int i=0; i<3; i++)
{
cout<<"Enter number"<<i<<": ";
cin>>marks[i];
}
for(int i=0; i<3; i++){
cout<<"Number"<<i<<" =>"<<marks[i]<<endl;
}
return 0;
}
⚙️ Output :
Enter number 0: 5
Enter number 1: 10
Enter number 2: 15
Number0 => 5
Number1 => 10
Number2 => 15
💻Example 6️⃣:
#include <iostream>
using namespace std;
int main() {
int marks[5];
for(int i=0; i<5; i++)
{
cout<<"Enter number"<<i<<": ";
cin>>marks[i];
}
for(int i=0; i<5; i++){
cout<<"Number"<<i<<" =>"<<marks[i]<<endl;
}
return 0;
}
⚙️ Output :
Enter number 0: 5
Enter number 1: 10
Enter number 2: 15
Enter number 2: 20
Enter number 2: 25
Number0 => 5
Number1 => 10
Number2 => 15
Number3 => 20
Number4 => 25
Array Example
- Write a program to calculate sum of elements present in array.
#include <iostream>
using namespace std;
int main() {
int arr[5] = {5,4,1,9,7};
int sum = 0;
for(int i=0; i<5; i++)
{
sum = sum + arr[i];
}
cout<<"Sum = "<<sum;
return 0;
}
- Write a program to print only even numbers from given array
#include <iostream>
using namespace std;
int main() {
int arr[8] = {5,4,1,9,7,6,12,3};
for(int i=0; i<8; i++)
{
if(arr[i]%2==0)
{
cout<<arr[i]<<" , ";
}
}
return 0;
}
- Write a program to find largest element from array.
#include <iostream>
using namespace std;
int main() {
int arr[5] = {5,7,81,2,23};
int largest = 0;
for(int i=1; i<5; i++)
{
if(arr[i] > largest)
{
largest = arr[i];
}
}
cout<<"Largest Element is = "<<largest;
return 0;
}