Tuesday, July 14, 2009

In Java C++ 2. Write code that will read two integers and print the number of multiples of 3?

Java and C++ are different languages. What are you trying to ask?

In Java C++ 2. Write code that will read two integers and print the number of multiples of 3?
Code in c++ is as below


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


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


void main()


{


clrscr();


cout%26lt;%26lt;"Enter two integers:';


int a,b,count;


cin%26gt;%26gt;a%26gt;%26gt;b;


if(a%26gt;b)


{


int c=a;


a=b;


b=c;


}


for(int i=a;i%26lt;=b;i++)


{


if(i%3==0)


count++;


}








cout%26lt;%26lt;"Number of multiples of three between"%26lt;%26lt;a%26lt;%26lt;"%26amp;"%26lt;%26lt;b%26lt;%26lt;"are"%26lt;%26lt;count%26lt;%26lt;endl;





getch();


}
Reply:#include %26lt;iostream%26gt;





int main() {


std::cout %26lt;%26lt; "Enter an integer: ";


int first = 0;


std::cin %26gt;%26gt; first;


std::cout %26lt;%26lt; "Enter another integer: ";


int second = 0;


std::cin %26gt;%26gt; second;


int count = 0;





//ensure first is less than second


if(first %26gt; second) {


int temp = first;


first = second;


second = temp;


}





for(int i = first; i %26lt;= second; i++) {


if(i % 3 == 0) {


//i is a multiple of 3


std::cout %26lt;%26lt; i %26lt;%26lt; std::endl;


count++;


}


}





std::cout %26lt;%26lt; "Number of multiplies of 3 between " %26lt;%26lt; first %26lt;%26lt; " and " %26lt;%26lt; " second is " %26lt;%26lt; count %26lt;%26lt; std::endl;


}
Reply:PLZ.... dicide that r u talking about java or c++


No comments:

Post a Comment