Write a program that prompts the user to input two numbers, First and diff. The program then creates a one dimensional array of 10 elements and initializes them with an arithmetic sequence. The first number of the sequence is the First value and the next number is generated by adding the diff to the number preceding it. This formula is repeated for the rest of the sequence. E.g. if First =11 and diff = 4, then the arithmetic sequence will be 11, 15, 19, 23, 27, 31 ... and so on.
#include <iostream>
using namespace std;
int main(){
int first, diff;
cout<<"Enter First: ";
cin>>first;
cout<<"Enter diff: ";
cin>>diff;
int array[10];
array[0] = first;
for(int i = 1; i < 10; i++){
array[i] = array[i - 1] + diff;
}
cout<<"The arithmetic sequence is\n";
for(int i = 0; i < 10; i++){
cout<<array[i];
if(i != 9){
cout<<", ";
}
}
cout<<endl;
return 0;
}
Comments
Leave a comment