Question

In: Computer Science

Establish a variable that will serve as a counter. You may use a single letter, such...

  1. 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
  2. Write a while loop that will execute while the value of your counter is less than or equal to 10.
  3. Inside the loop, print the value of the counter
  4. Make sure to increment your counter as the last step inside the loop or the loop will never stop executing.

Solutions

Expert Solution

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)


Related Solutions

Craft a letter to one of your legislators. You may choose to write a letter to...
Craft a letter to one of your legislators. You may choose to write a letter to your State Senator or House Representative for your district. Keep your letters to legislators that are elected on the Federal level and serve you in Washington, DC. If you do not know who your local senator or representative is, check the Supplemental Resources for the legislator search link. In your letter tell your legislator what you think may be effective mechanisms for controlling health...
Use if statements to write a Java program that inputs a single letter and prints out...
Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way: 2 = ABC    3 = DEF   4 = GHI    5 = JKL 6 = MNO   7 = PRS   8 = TUV 9 = WXY No digit corresponds to either Q or Z. For these 2 letters your program should print a message indicating that they are not...
From the table below, use a simple linear regression analysis to establish the relationship that may...
From the table below, use a simple linear regression analysis to establish the relationship that may exist between a) number of confirmed cases and deaths; b) number of confirmed cases and number of tests performed; c) number of confirmed cases and number of recoveries; and d) number of deaths and number of recoveries. Briefly discuss these relations. Date Total confirmed Death Recoveries Test 1-Apr 195 5 3 12046 2-Apr 204 5 3 12046 3-Apr 205 5 3 12046 4-Apr 214...
For the prelab assignment, you may not use the if statement nor may you use the...
For the prelab assignment, you may not use the if statement nor may you use the if/else statement. You are to write a program that calculates factorials from 1 up to and including a maximum number given by the user. Because factorials get large quickly, use long unsigned int as the type for the number entered by the user as well as the factorials you are calculating. If the user enters a 0, print an error message. Then ask if...
Define a class called Counter whose internal "count" variable is a basic integer counter. This class...
Define a class called Counter whose internal "count" variable is a basic integer counter. This class track that count from its instantiation in the constructor (can be set to any positive integer, or 0). The count should never be allowed to be negative. Include methods that will set the counter to 0, increase the count by 1, and decrease the count by 1. Include an accessor method that returns the current count value (getCount()). There should be no input /...
You may not use the java.util.Stack class. You may not use the java.util.Queue class. Write a...
You may not use the java.util.Stack class. You may not use the java.util.Queue class. Write a method public static Object removeSecond (): It removes and returns the element just behind the front element. Precondition: The queue has at least two elements. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // Queue.java // demonstrates queue // to run this program: C>java QueueApp //////////////////////////////////////////////////////////////// class Queue { private int maxSize; private long[] queArray; private int front; private int rear; private int nItems; //-------------------------------------------------------------- public Queue(int s) // constructor {...
You may not use the java.util.Stack class. You may not use the java.util.Queue class. Write a...
You may not use the java.util.Stack class. You may not use the java.util.Queue class. Write a method public static void removeDownTo (long n): It pops all values off the stack down to but not including the first element it sees that is equal to the second parameter. If none are equal, leave the stack empty. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // stack.java // demonstrates stacks //////////////////////////////////////////////////////////////// class StackX { private int maxSize; // size of stack array private long[] stackArray; private int top; //...
you may not use the Python ord() or chr() functions you may not use the Python...
you may not use the Python ord() or chr() functions you may not use the Python ord() or chr() functions you may not use the Python ord() or chr() functions You will write a total of four functions, each of which will take two inputs and return a string: c_encrypt() c_decrypt() vig_encrypt() vig_decrypt() The first argument will be a string containing the plaintext (or clear text) message to be encrypted for the two encrypt functions, and a string containing a...
Four hospitals located in one city are cooperating to establish a centralized blood-bank facility to serve...
Four hospitals located in one city are cooperating to establish a centralized blood-bank facility to serve them all. On an xy coordinate grid of the city, the hospitals are found at the following locations: Hospital 1 = (x = 5, y = 10), Hospital 2 = (7, 6), Hospital 3 = (4, 2), and Hospital 4 = (16, 3). The expected number of deliveries per month from the blood bank to each hospital is estimated at 450, 1200, 300, and...
The multiple regressions serve to explain the behavior of one variable (dependent variable) though a set...
The multiple regressions serve to explain the behavior of one variable (dependent variable) though a set of some explanatory variables for which we can find a logical/theoretically founded relationship with the dependent variable. Please discuss three business situations (either real or a business situation) with proposed set of 5 explanatory variable. Could you define the expected sign (positive or negative) of these selected explanatory variables? As e have discussed the usage of the dummy variables propose at least in one...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT