C++programming please i need all codes in this homework . Homework 1) Use a loop
ID: 3583971 • Letter: C
Question
C++programming
please i need all codes in this homework
. Homework
1) Use a loop to read 5 numbers and display them.
Enter 5 numbers
1 3 5 2 4
1 3 5 2 4
2) Use a loop to read 10 numbers and display as followings.
Enter 10 numbers
1 3 12 99 88 .......
0-th number: 1
1-th number: 3
2-th number: 12
............
3) Read 10 numbers and display the sum of them.
Enter 10 numbers
1 3 12 99 88 77 44 12 80 13
sum:429
4) Read 10 numbers and display those that are greater than 20.
Enter 10 numbers
1 3 12 99 88 77 44 12 80 13
99 88 77 44 80
5) Read 10 numbers and display the sum of those that are greater than 20.
Enter 10 numbers
1 3 12 99 88 77 44 12 80 13
sum:388
6) Read 10 numbers and show how many 20’s are there.
Enter 10 numbers
1 3 20 20 77 99 23 20 12 1 2
The number of 20: 3
7) Read 10 numbers and display the sum of numbers that are not 20.
Enter 10 numbers
1 3 20 20 77 99 23 20 12 1 2
sum:216
Explanation / Answer
#include <iostream>
using namespace std;
int main(){
int array1[5];
//part-1
for(int i=0; i<5; i++){
cin>>array1[i];
}
for(int i=0; i<5; i++){
cout<<array1[i]<<" ";
}
//part-2
int array2[10];
for(int i=0; i<10; i++){
cin>>array2[i];
}
for(int i=0; i<10; i++){
cout<<i<<"-th number: "<<array2[i]<<endl;
}
//part-3
int sum=0;
int n;
for(int i=0; i<10; i++){
cin>>n;
sum+=n;
}
cout<<"sum:"<<sum;
//part-4
int array4[10];
for(int i=0; i<10; i++){
cin>>array4[i];
}
for(int i=0; i<20; i++){
if(array4[i]>20){
cout<<array4[i]<<" ";
}
}
//part-5
int array5[10];
for(int i=0; i<10; i++){
cin>>array4[i];
}
int sum2=0;
for(int i=0; i<20; i++){
if(array5[i]>20){
sum2+=array5[i];
}
}
cout<<"sum: "<<sum2;
//part-6
int x;
int count=0;
for(int i=0; i<10; i++){
cin>>x;
if(x==20){
count++;
}
}
cout<<"The number of 20: "<<count<<endl;
//part-7
int sum3=0;
int y;
for(int i=0; i<20; i++){
cin>>y;
if(y!=20){
sum3+=y;
}
}
cout<<"sum: "<<sum3;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.