how to print a list of numbers using printf in C++?

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

I am new learning C++ and I have basic questions and basic troubles :(

I want to print a list of numbers that are coming from the next following while condition:

int list=0;
while (list<100){
    list=list+r;
}

I want to use printf instead of cout (because I still don´t know why with cout is not working).

Can anyone help me to give me the analogous printf command to

cout<<list<<"\t";

Thanks a lot!!!

profilepic.png
manpreet 2 years ago

 

Here is a small sample program which counts up to 100 in increments of 10.

I use both std::cout and printf to display the value of list in each increment.

Comments added to hopefully help aid you in learning

#include 
#include dio>

int main()
{
    int r = 10;

    int list=0;
    while (list < 100)
    {
        list += r;                 // this is the same as saying list = list + r, but is more succinct

        std::cout << list << "\t"; // cout is in the std namespace, so you have to prefix with std::

        printf("%d\n", list);      // the printf format specified for int is "%d"
    }
}

Output:

10    10
20    20
30    30
40    40
50    50
60    60
70    70
80    80
90    90
100   100

Please note that I didn't use using namespace std; at the top to import cout into the global namespace. IMHO this is bad practice, so I typically will prefer std::cout etc.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.