In: Computer Science
I have given you the basic explanation in C language in below steps ,and also given you the code with comments in 4 different programming languages-Python,Java,C,C++
Steps-
a.Establish a variable that will serve as a counter. You may use a single letter, such as “x” or you may use a variable name that is more meaningful like “counter”. Set the initial value of this variable to 0
answer set a variable counter=0
i.e int counter=0; //setting value of counter
b.Write a while loop that will execute while the value of your counter is less than or equal to 10.
answer //while loop with condition counter<=10
int counter=0;
while(counter<=10){
}
c.Inside the loop, print the value of the counter
answer int counter=0;
while(counter<=10){
//printing the value of
counter \n means moves to new line
printf("%d \n",counter);
}
d.Make sure to increment your counter as the last step inside the loop or the loop will never stop executing.
answer int counter=0;
while(counter<=10){
//printing the value of counter \n means moves to new line
printf("%d \n",counter);
//incrementing
counter
counter=counter+1;
}
Below are the codes-
Code with comments in C language-
//header file
#include <stdio.h>
//main function
int main()
{
//declaring counter=0
int counter=0;
//while loop with condition counter<=10
while(counter<=10){
//printing the value of counter \n means moves to new line
printf("%d \n",counter);
//incrementing counter
counter=counter+1;
}
//exit from the program
return 0;
}
Code with comments in C++ language-
//header file
#include <iostream>
using namespace std;
//main function
int main()
{
//declaring counter=0
int counter=0;
//while loop with condition counter<=10
while(counter<=10){
//printing the value of counter endl means moves to new line
cout<<counter<<endl;
//incrementing counter
counter=counter+1;
}
//exit from the program
return 0;
}
Code with comments in Java language-
//input output class
import java.io.*;
import java.util.*;
//main function
public class Main
{
//main method
public static void main(String[] args) {
//scanner class
Scanner my=new Scanner(System.in);
//declaring counter=0
int counter=0;
//while loop with condition counter<=10
while(counter<=10){
//printing the value of counter
System.out.println(counter);
//incrementing the counter
counter=counter+1;
}
}
}
Code with comments in Python language-
#declaring counter=0
counter=0
#while loop with condition counter<=10
while(counter<=10):
#printing the value of counter
print(counter)
#incrementing the counter
counter=counter+1
Below are the outputs with code in four different languages C,C++,Java,Python respectively-
Output with code in C language-
Output with code in C++ language-
Output with code in Java
language-
Output with code in Python
language-
(Hope you like the solution please give Thumbs up,If you have any doubt please comment,I will definately help you)