View: 3309|Reply: 11
|
How to solve this question? using C language..
[Copy link]
|
|
Write a simple program where the input is a letter of the alphabet and the output is an upside down triangle as shown below:
input : E
output : ABCDE
ABCD
ABC
AB
A
|
|
|
|
|
|
|
|
takde yg boleh jawab ke? |
|
|
|
|
|
|
|
ok boleh... sy dah wat tp tak jadi...
ni cth yg saya wat, tp tak jadi..
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main ()
{
char E;
printf ( "Enter capital 'E': " );
scanf ( "%c", &E );
for (int c = 5; c > 0; c -- )
{
for ( int d = 1; d <= c; d ++ )
{
printf ("%c", ++E );
}
printf ( "\n" );
}
printf ( "\n\n" );
system ( "pause" );
return 0;
} |
|
|
|
|
|
|
|
OK siap...
- #include <iostream>
- using namespace std;
- int main()
- {
- char ch;
- int ord;
- cout << "Please enter 'E': ";
- cin >> ch;
- for (int j=0; j<=4; j++)
- {
- for (int i=4; i>=j; i--)
- {
- ord = static_cast<int>(ch) - i;
- cout << static_cast<char>(ord);
- }
- cout << endl;
- }
- return 0;
- }
Copy the Code |
Rate
-
1
View Rating Log
-
|
|
|
|
|
|
|
Reply 5# juwaini
thanks juwaini ...
your answer has give me this idea...
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main ()
{
char E;
printf ("Enter capital 'E': ");
scanf ("%c", &E );
for (int c = 0; c < 5; c ++ )
{
for (int d = 4; d >= c; d -- )
{
printf ("%c", E - d );
}
printf ("\n\n" );
}
system ("pause" );
return 0;
}
am just improvise it... |
|
|
|
|
|
|
|
Sintaks tak penting. Yang penting paham dia punya logik je...
BTW, kalau ko pure C programmer, line "using namespace std" tu tak perlu lagi dah... Yang tu cuma untuk C++ je. |
|
|
|
|
|
|
|
Post Last Edit by alien3d at 25-7-2010 13:21
jawapan tu tak lengkap.
kalau php la
<?php
- <?php
- $request='N';
- $continue=0;
- foreach (range('Z', 'A') as $letter) {
- //echo $letter;
- if($letter == $request) {
- $continue=1;
- }
- if($continue==1) {
- $str.=$letter;
- }
- }
- $str_array =str_split($str);
- $str_array = array_reverse($str_array);
- echo implode('',$str_array);
- ?>
- ?>
Copy the Code |
Rate
-
1
View Rating Log
-
|
|
|
|
|
|
|
way 1
1. create an array contain A to Z
2. loop the array until X
3. break the loop .
4. print reverse order of the val
way 2
1.create an array contain Z to A
2. loop the array until X
3. Create a variable which can catch value after found event
4. print value
** maaf tak sesuai aku tulis dalam bahasa melayu. |
|
|
|
|
|
|
|
Reply 9# alien3d
ni soalan nak kiteorg jwb ke? |
|
|
|
|
|
|
|
Reply 10# LanzSlumber
itu namanya pseudo code.... bukan soalan.Sorry wa no degree people. |
|
|
|
|
|
|
|
Reply 5# juwaini
ada bug.
kalo enter huruf selain e dia x dpt nak buat triangle... |
|
|
|
|
|
|
| |
|