0
How to use variableas
?
3 odpowiedzi
+ 6
// declaring variables
int x;
int num = 1;
string some_str = "cookies";
// using variables
x = num; // value of x is now 1
cout << x + num; // outputs 2
cout << some_str; // outputs "cookies"
+ 2
class AddItAll_Using_For_Loop_WhileLoop_And_DoWhile_Loop
{
public static void main(String[] args) {
int sum_of_all=0;
int till=100;
int j=0;
//For Loop
for(j=0;j<=till;j++)
{
sum_of_all+=j;
}
System.out.println("Using For Loop | Sum of all natural numbers till "+till+" is => "+sum_of_all+"\n");
//While Loop
while(j<=till)
{
sum_of_all+=j;
j++;
}
System.out.println("Using While Loop | Sum of all natural numbers till "+till+" is => "+sum_of_all+"\n");
//Do...While
do
{
sum_of_all+=j;
j++;
}while(j<=till);
System.out.println("Using Do...While Loop | Sum of all natural numbers till "+till+" is => "+sum_of_all+"\n");
//with all loops value will be same i did just for explanation of all loops.
}
}
why the value is increased in do while loop when running the code
0
EVERYTHING THAT IS AFTER //, MEANS INFO... NO COMMANDS
Variables:
I try to tell you that way, so you will know what i mean.
int a; // Variable "a"
int a = 10; // Variable "a" got "10" in itself.
So we can make calculator for example:
int a = 10; // First number is "10"
int b = 5; // Second number is "5"
int sum = a - b; // Summary is "a" - "b" = "10" - "5"
We can make a blank variable and fill it later, example:
int a; // Blank variable (Variable don't have any numbers in it)
cout << "Select one number" << endl; // This command tell our user to type one number
cin >> a; // Number that user typed was stored to variable "a" .
cout << "You typed number << a; // Tell our user that his number is: What he typed.
My first C++ calculator was doing by variables, I get some help from Google , but I don't copied it, I writed it by myself. If you wan't you can get inspired. (If you wanna check it out: https://code.sololearn.com/cyqTln6peslH/# ).
SORRY FOR MY BAD ENGLISH.