hi i need some help in this program.. using only 1 for loop..

#include<stdio.h>
void main()
{
int n;
clrscr();
for (n=1; n<=10; n--)
{
printf("\n %d",n);
}
getch();
}

*the output of this program is"
1
2
3
4
5
6
7
8
9
10

but i need to make it like this:
1        2
3        4
5        6
7        8
9        10

Recommended Answers

All 13 Replies

>void main()
Use this as a template:

#include <stdio.h>

int main ( void )
{
  /* Your code here */

  return 0;
}

>clrscr();
Unnecessary, anti-social, and you forgot to include <conio.h>.

>getch();
Unnecessary, non-portable (use getchar if you must), and you forgot to include <conio.h>.

>but i need to make it like this:
How about something like this:

for ( n = 1; n < 10; n += 2 )
  printf ( "%d\t%d\n", n, n + 1 );

thanks..
how about in this format i need the output to be like this

square   cube 
1        1        1 
2        4        8 
3        9        27 
4        16       64 
5        25       125

i think it needs some formula but i dont have any clues about it.. thanks in advance..

>i think it needs some formula
Do you even know how to find the square and cube of a number? It's pretty basic arithmetic, and you can use what you learned from my last post to do the output.

nope i dont know how to find it using C.. can you teach me how to find square and cube in C??

Do you know how to multiply two numbers in C? You can multiply a value with itself, and that's all you need to know to find the square and cube of a value.

haha he really expecting us to do his work. nana ;) you can't do that here my friend. Narue has already shown in here sample code on her previous post. You wouldn't believe its just a 2 line of code.

Narue with your permission i gonna pinch your code.

for ( n = 1; n < 10; n += 2 )  
     printf ( "%d\t%d\n", n, n + 1 );

How would you calculate square n * n --- OK
How would you calculate cube n * n * n --- OK

If you expect more than that, god help you.

ssharish

ssharish thanks for this

How would you calculate square n * n --- OK
How would you calculate cube n * n * n --- OK

and thank you so much narue.. ive figured it now.. thanks thanks thanks thanks

Well done, you did get how to calculate square and cude. You learn new everyday!

ssharish

ohh i need help again in this program.. can you help me again??

oh oooooooooo, what is it now???

ssharish

int main()
{
        int i,limit;

        printf("Enter the Limit: ");
        scanf("%d", &limit);

        printf("%10s%10s%10s", "Number","Square","Cube");

        for( i = 0; i < limit; ++i ){

                printf("%10d%10d%10d\n", i, i*i, i*i*i);
        }

        return 0;
}

you can write like that if you want to be formatted for your output, you should glance C I-O chapter...

thanks sir.. but now i need another help i want a prgram that continuosly adds the number input then displays the sum when 0 is pressed. all i have is this old code for adding the sum.

#include<stdio.h>
#include<conio.h>
main()
{
int sum,n1,n2,n3,n4;
clrscr();
printf("Enter numbers: ");
scanf("%d%d%d%d",&n1,&n2,&n3,&n4);
sum=n1+n2+n3+n4;
while(1)
{
  scanf("%d",&n1);
  sum+=n1;
  if(n1==0) break;
}
 
printf("\n The sum is: %d", sum);
getch();
}

thanks sir.. but now i need another help i want a prgram that continuosly adds the number input then displays the sum when 0 is pressed. all i have is this old code for adding the sum.

Perhaps you are beyond help. You have been shown some good professional programming practices already. None very hard to understand. But still you go and post an aberration of code, showing your level of commitment to do things right. Am I too harsh coming to that conclusion?
Why don't you go ahead and change that code and implement the suggestions given to you, and prove me wrong.

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.