177k views
3 votes
Please use C programming to write a code segment as an answer. Instead of using an while-loop like the following code, please implement a code segment by using a do-while loop. What is the output of the code?

#include
void main()
{
int i = 0;
while (i < 5);
{
printf("%d ", ++i);
}
}

User Jackhab
by
4.8k points

2 Answers

1 vote

Answer:

#include <stdio.h>

void main()

{

int i = 0;

do{

printf("%d ", ++i);

}

while (i < 5);

i=i+1;

}

}

Explanation

The #include needs to include something and that's the standard input and output which is coded has stdio.h

In the do while loop what it does is print the value of I while I is less than 5 and you increment I value so as to prevent infinite looping

User Juan David
by
4.7k points
6 votes

Answer:

The program using do-while loop defined as follows:

Program:

#include <stdio.h> //include header file

int main() //defining main method

{

int i = 0; //defining integer variable i and assign value.

//defining do-while loop

do

{

printf("%d", ++i); //print value

}while (i<5); //check condition

return 0;

}

Output:

12345

Explanation:

Output of given program:

In the given program, it will not print any value because in while loop semi colon is used which is not valid.

Program Explanation:

In the above C language program header file is included, which provides features to use basic function then the main method is defined inside this method an integer variable "i" declare, that holds a value which is "0". In this method, the do-while loop is defined. In the do section, we print value and in the while block checks the condition which is i is less than 5.

User Mnield
by
4.6k points