Monday, May 24, 2010

What would this look like in C++ code?

a program that reads ten numbers (in an array), and counts the number of elements in the array that are divisible by 3.

What would this look like in C++ code?
#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


clrscr();


int arr[10];int ctr=0;


cout%26lt;%26lt;"Enter 10 numbers";


for(int i=0,i%26lt;10,i++)


{


cin%26gt;%26gt;arr[i];


if((arr[i]%3)==0)


ctr++;


}


cout%26lt;%26lt;"The no of elements divisible by 3 are"%26lt;%26lt;ctr;


}
Reply:Assuming you already have the numbers in an array called "numbers":





int count = 0;





for(int i = 0 ; i%26lt;10 ; i++){


if(numbers[i] % 3 ==0){


count++;


}


}





The variable "count" will have what you need. Just stick that into a function.


No comments:

Post a Comment