Q&A Discussions
Hi, I need assistance aka HELP!
I've done this calculation in various ways some of printed nothing but zeros non stop.
Using while loop calculate all numbers 1 to 1000 add numbers to sum = sum + i
int sum = 0;
// use a while loop to calculate the sum of 1 to 1000
While(int sum = 0; sum >= 1000; sum = sum + I){
System.out.println(sum);
}
0 Votes
4 Answers
what have i to do, if i want that the program always writes for example :
0+1
1+2
3+3
6+4
10+5
15+6
...
because now it just writes
1
3
6
10
15
...
and maybe if it works, can i make then
0+1=1
1+2=3
3+3=6
6+4=10
#include <iostream>
using namespace std;
int main(){
int i;
int sum = 0;
for (i = 1; i <= 100; i++){
sum = sum+i;
cout<<sum<<endl;
}
cout<<sum;
return 0;
}
1 Vote
5 Answers#include <iostream>
using namespace std;
class employee
int main()
{
private:
double salary;
public:
void getsalary(double sal)
{
salary=sal;
}
void display()
{
cout<<"the salary:"<<salary;
}
};
void main ()
{
employee programmer;
employee manager;
manager.getsalary (60000.0);
programmer.getsalary(40000.0);
manager.display();
programmer.display();
return 0;
}
1 Vote
5 Answerswhy is it not running?
import java.util.Scanner;
public class Myclass{
public static void main(String [] args){
Scanner in = new Scanner(System.in);
String a;
int b;
System.out.println("enter a word");
a = in.nextLine();
System.out.println("enter repetition")
b = in.nextInt();
for ( int = 0; x < b; x = x++)
System.out.println(a);
}
}
0 Votes
8 AnswersIn the code below the output is 2. This code is used in a challenge and I get an error when I select the answer 2. It accepts as correct the answer 1 which I believe is wrong. If you agree please thumbs up.
If you find any other errors please post them here!
#include <iostream>
using namespace std;
int main()
{
for(int i=1; i>=1; i++) {
if(i<1 || i>1){
cout << i;
break;
}
}
return 0;
}
8 Votes
8 Answerswhy does this code output that there is an error because the else statement doesn't have a corresponding if statement?. How do i correct this?
#include <iostream>
using namespace std;
int main() {
int a;
for (int x =0; x<5;x++){
cin>>a;
if (a<1){
cout<<"a cannot be less than 1\n";
}
else if (a>10){
cout <<"a cannot be greater than 10\n";
}
}
else{
cout<<"code can run now"<<endl;
}
return 0;
}
3 Votes
8 Answerscan someone please explain why this code keeps telling me complication error instead of outputting the value of speed.
#include <iostream>
using namespace std;
class myClass {
public:
void setcarspeed (int a){
speed=a;
}
int getcarspeed(){
return speed;
}
private:
int speed;
};
int main() {
myClass obj;
obj.setcarspeed (40);
cout<<obj.getcarspeed ;
return 0;
}
2 Votes
3 Answershey guys, I've been trying to work out the formula 'v=I*r'. But whenever I run the program and input values for I and r, it doesn't give the right answer.
This is the code
#include <iostream>
using namespace std;
int main() {
int i,r;
int v = i*r;
cout<<"what is the value of i?\n";
cin>>i;
cout<<"what is the value of r?\n";
cin>>r;
cout<<"voltage ="<<v<<endl;
return 0;
}
p/s: I'm new to programming
1 Vote
8 Answers#include <iostream>
using namespace std;
int sum(int a, int b=42) {
int result = a + b;
return (result);
}
int main() {
int x = 24;
int y = 36;
//calling the function with both parameters
int result = sum(x, y);
cout << result << endl;
//calling the function without b
result = sum(x);
cout << result << endl;
return 0;
}
why is da last function cout 66 and not 48 isn't it suppose to add 24+24?
1 Vote
6 Answersif i text 21 it stacks. program says you are a child. you are a teenager. you are adult. how can i delete this "stack".
#include <iostream>
using namespace std;
int main() {
int age;
cin>>age;
if (age>=14){
cout<<"You're a Teenager"<<endl;
}
if (age>=18){
cout<<"You're adult"<<endl;
}
if (age>=0){
cout<<"You're a Child"<<endl;
}
else{
cout<<"Are u stupid ?"<<endl;
}
return 0;
}
0 Votes
4 AnswersWher is mistake?
0 Votes
5 Answersx = 42;
int num = 0;
while (num <3)
Consele.writeline (x);
num++;
int x = 42; is just an int with an asigned value of 42
int num=0; is what initiate the count for the while loop
while (num < 3); means 0<3 true, 1< 3 true, 2<3 true, the loop end at 2 bc 3 <3 is false
consele.writeline (x) will display 42 while num < 3
num++ will execute and then add 1 to check if the while loop still true, but it stop at 2, bc 3 makes it false.
so the consele.write will write 42 three times. x=42, remember the (x)
0 Votes
2 Answers