i am new in c++ and i quite need help in writing the code for this peoblem.This program needs to list the numbers from 0 to 25, their squares, and the fourth power. The output should be in the neat 3-column format.

Number Square 4th power
0 0 0
1 1 1
: : :
25 625 :

Recommended Answers

All 15 Replies

you must post your code so we can help you... did you read the announcements?

Member Avatar for iamthwee

1.You don't need printf.
2.Where is your attempt?

Even using printf, it should be an elementary solution. Just try understand how a for loop works (should be simple for this problem), and read up on printf and it's various options. The maths of this solution is also very simple. A bit of trial and error is a good way to learn also.

Member Avatar for iamthwee

There is no need to read up on printf. The <iomanip> library provides exact functionality as printf.

There is no need to read up on printf. The <iomanip> library provides exact functionality as printf.

Boy is that a debatable statement! :icon_wink:

commented: He he +11

OH... C++, ok well I was thinking C here. I have noticed most C++ references don't use printf in their code. Anyway I was just saying printf, as it was stated in the thread title, and I guess it could be a requirement of the assignment?

Member Avatar for iamthwee

>I guess it could be a requirement of the assignment?

It could be yes, but if it isn't there is no reason to use printf to format your output, as it boils down to familiarity more than anything else. Similar arguments could be made for using char arrays instead of std::strings...

you must post your code so we can help you... did you read the announcements?

i'm sorry..i didnt quite read the announcement that has been posted..
this sis the code i have written for problem #1..(write a program that will display on th screen the following using loops)..i'm not sure if my codewiould work out right..im using microsoft visual c++ that is why my code would seem quite different..
#include<iostream.h>
main()
{
int n,x,y;
cout<<"n=";
for (x=1; x<=n; x++)
{
for (y=3; y<=8; y++)
cout<<y;
}
return 0;
}
***************
and this was supposed to be the output for that problem..
345678345678345678
i don't know if my code was correct..so pls do help me because i really want to learn..

Member Avatar for iamthwee

For starters reader this and if you still want to use c this

I hate quoting narue's snippets, I feel so dirty. :(

#include<iostream.h>
main()
{
   int n,x,y;
cout<<"n=";
for (x=1; x<=n; x++)
{
   for (y=3; y<=8; y++)
   cout<<y;
 }
   return 0;
}

***************
and this was supposed to be the output for that problem..
345678345678345678
i don't know if my code was correct..so pls do help me because i really want to learn..

Unless you're using a very old (pre-standard) version of MSVC++, then no, it's incorrect (Certainly doesn't compile on any of the recent versions)

On modern compilers, the correct header is <iostream> (without the .h) - and you need to obtain cout from the std namespace


Regardless of the compiler version, in your for loop, You use the variable 'n' without previously defining what 'n' is, and you're missing a return type from main() ( main() should return an int )

For starters reader this and if you still want to use c this

I hate quoting narue's snippets, I feel so dirty. :(

Any particular reason for this? both articles seem perfectly good to me.

Unless you're using a very old (pre-standard) version of MSVC++, then no, it's incorrect (Certainly doesn't compile on any of the recent versions)

On modern compilers, the correct header is <iostream> (without the .h) - and you need to obtain cout from the std namespace


Regardless of the compiler version, in your for loop, You use the variable 'n' without previously defining what 'n' is, and you're missing a return type from main() ( main() should return an int )


Any particular reason for this? both articles seem perfectly good to me.

so what could be the correct syntax for that sample problem?..do i have to exclude .h from iostream?

Syntax changes from your snippet are highlighted in bold red

#include [B]<iostream>[/B]
[B]int[/B] main()
{
   int n,x,y;
[B]std::[/B]cout<<"n=";
for (x=1; x<=n; x++)
{
   for (y=3; y<=8; y++)
   [B]std::[/B]cout<<y;
 }
   return 0;
}

Note that your code still does not specify a value for 'n' anywhere (Perhaps you intended to retrieve it from the user?)

Just to re-iterate what I said in my last post - If you're using an old, pre-standard, compiler, then this won't compile - and you'd be well advised to download a modern compiler.

perhaps that std::cout<<"n="; was meant to be std::cin>>n; ...

BTW, there are some compilers that still use the deprecated headers, such as <stdlib.h> , or <iostream.h> ... for example, Borland's Turbo C++, which i am forced to use @ the university (which i hate)...

perhaps that std::cout<<"n="; was meant to be std::cin>>n; ...

Or, more likely, that cout needs to be followed by that cin

BTW, there are some compilers that still use the deprecated headers, such as <stdlib.h> , or <iostream.h> ... for example, Borland's Turbo C++, which i am forced to use @ the university (which i hate)...

stdlib.h is not deprecated. It is a C header. Only the C++ headers with the .h are deprecated.

actually, it makes sense... though, if i write it in DEV it tells me it's deprecated...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.