Skip to main content

C++ = Code to find sum, product and average of particular numbers

Q2: Create a C++ program that does the following
a. Declare an array of integers with a size of 10 and initialize it with some values
b. Find and display the sum of all even numbers in the array.
c. Find and display the product of all odd numbers in the array.
d. Determine and display the average of all numbers in the array.

Code:

#include <iostream>

using namespace std;

int main()

{

int array[]={1,2,3,4,5,6,7,8,9,10},seven=0,sodd=1,saverage=0;

for (int i=0;i<10;i++)

{

if (array[i]%2==0)

{

seven+=array[i];

}

else

{

sodd*=array[i];

}

saverage+=array[i];

}

float average;

average=saverage/10;


cout<<"Sum of even numbers in array is "<<seven<<endl;

    cout<<"Product of odd numbers in array is "<<sodd<<endl;

    cout<<"The average of these numbers is "<<average<<endl;

}

Result:



Comments

Popular posts from this blog

C++ = Multiplication of 2 matrices of order m*n by taking input by user.

Code: #include <iostream> using namespace std; int main() { int rows1,columns1,rows2,columns2; cout<<"Enter the rows of first column : "; cin>>rows1;     cout<<"Enter the columns of first column : ";     cin>>columns1;          cout<<"Enter the rows of second column : ";     cin>>rows2;     cout<<"Enter the columns of second column : ";     cin>>columns2;     int matrix1[rows1][columns1],matrix2[rows2][columns2];          if (rows2==columns1)     {     cout<<"Enter the elements of first matrix : ";     for (int i=0;i<rows1;i++)     {     for (int j=0;j<columns1;j++)     {     cin>>matrix1[i][j]; } }          cout<<"Enter the elements of second matrix : ";     for (int i=0;i<...

C++ = Write a program that will ask user with four possible answers. The question should be asked 5 times. After the input is gathered, the program should output the number of times each question was selected.

Write a program that will ask user with four possible answers. The question should be asked 5 times. After the input is gathered, the program should output the number of times each question was selected. Code: #include <iostream> using namespace std; int main() {      int ques1=0,ques2=0,ques3=0,ques4=0,num=0;   cout<<"Enter the no: of desired question "<<endl;   cout<<"Press 1 for question 1 "<<endl;   cout<<"Press 2 for question 2 "<<endl;   cout<<"Press 3 for question 3 "<<endl;   cout<<"Press 4 for question 4 "<<endl;      cout<<endl;   for (int i=1;i<=5;i++)   {   cout<<"Enter no: of Question = ";   cin>>num;   if (num==1)   {   ques1=ques1+1;   }   else if (num==2)   {   ques2+=1;   }     else if (num==3)     {     ques3+=1;...

C++ = Code for checking any digit palindrome number

Code no: 1 = Using only main function to perform task. #include <iostream> using namespace std; int main() { int number,original,digit,reverse=0; cout<<"Enter any number : "; cin>>number; original=number; while (number!=0) { digit=number%10; reverse=(reverse*10)+digit; number=number/10; } if (original==reverse) { cout<<"This "<<original<<" is Palindrome number."; } else { cout<<"The "<<original<<" is not Palindrome number."; } } Result: Code no: 2 = Using two functions to perform same task. #include <iostream> using namespace std; int isprime(int num) { int digit=0,reverse=0; while (num!=0) { digit=num%10; reverse=(reverse*10)+digit;  num/=10; } return reverse; } int main() { int num; cout<<"Enter any digit to find its sum = "; cin>>num;          int original=num;          if (i...