Answer:
#include<iostream>
using namespace std;
//main function
int main(){
//initialization
int sum_1=0;
//for loop which run from 1 to 15
for(int a1=1;a1<=15;a1++){
if(a1%2==1){
sum_1 = sum_1 + a1; //take the sum
}
}
//display the result
cout<<"The sum of odd number is:"<<sum_1<<endl;
return 0;
}
Step-by-step explanation:
Include the library iostream for using the input/output instruction.
Create the main function and declare the variable.
Them, take the for loop which runs from 1 to 15 and we also take the if-else conditional statement which checks the condition and if the condition is true, the statement inside the 'if' statement executes.
The condition of odd:
when the odd numbers are divided by 2, they give the remainder 1.
so, in the programming, the operator modules (%) is the only one which gives the output remainder.
suppose, 5%2 it gives the output 1.
so, the condition of an odd number is (number%2==1) we put this condition in the if statement and when the condition is true then, we take the sum of that number and store back in the sum as well.
when all number will check then the loop will terminate and finally, we display the result on the screen.