Problem with producing output
Mar 4, 2014 at 4:13am UTC
i want to make this kind of output using "for" operation
AA
ABBA
ABCCBA
ABCDDCBA
ABCDEEDCBA
but instead i got different output...im a noob so please help me
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <stdio.h>
int main()
{
char i,j;
for (i='E' ;i>='A' ;i--)
{
for (j=i;j<='E' ;j++)
printf(" " );
for (j='A' ;j<=i;j++)
printf("%c%c" ,i,j);
printf("\n" );
}
}
Last edited on Mar 4, 2014 at 4:14am UTC
Mar 4, 2014 at 4:53am UTC
This may be helpful for you.
But I wrote this in cpp.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include "iostream"
#include "conio.h"
using namespace std;
int main()
{
char ch = 'A' ;
char j;
for (char i='A' ;i<'F' ;i++)
{
for (j='A' ;j<=i;j++)
cout<<j;
j--;
for (char k=j;k>='A' ;k--)
cout<<k;
cout<<"\n" ;
}
getch();
return 0;
}
Mar 4, 2014 at 4:56am UTC
This is more like you in C (using printf() instead of cout) :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include "stdio.h"
#include "conio.h"
int main()
{
char ch = 'A' ;
char j;
for (char i='A' ;i<'F' ;i++)
{
for (j='A' ;j<=i;j++)
printf("%c" ,j);
j--;
for (char k=j;k>='A' ;k--)
printf("%c" ,k);
printf("\n" );
}
getch();
return 0;
}
Mar 5, 2014 at 5:07am UTC
thanks for the reply but this is the output i actually want...sorry for the misunderstanding
AA
ABBA
ABCCBA
ABCDDCBA
ABCDEEDCBA
it become pyramid shape
thanks :)
Mar 5, 2014 at 5:38am UTC
Just pad with spaces: here is an example:
1 2 3 4 5 6 7
for (int i = 0; i < 5; ++i) {
// print a certain width
printf("%*c" , 4 - i, ' ' );
// now print the characters
...
}
Last edited on Mar 5, 2014 at 5:38am UTC
Mar 5, 2014 at 5:54am UTC
it wont work...can you alter a full source code that produce that kind of output...really apreciated it...thank you so much
Mar 5, 2014 at 7:51am UTC
It simple , you should try this:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include "iostream"
#include "conio.h"
using namespace std;
int main()
{
char ch = 'A' ;
char j;
int h=0;
for (char i='A' ;i<'F' ;i++)
{
printf("%*c" , 5 - h, ' ' );
for (j='A' ;j<=i;j++)
printf("%c" ,j);
j--;
for (char k=j;k>='A' ;k--)
printf("%c" ,k);
cout<<"\n" ;
h++;
}
getch();
return 0;
}
Mar 5, 2014 at 1:48pm UTC
i get the output i want...thank you so much
wow...you really good in this...but i just a noob...i dont understand fully how this work...can you explain...if you have time...what is the function of 'h' and 'k'?
Topic archived. No new replies allowed.