using for loops to generate both figures and asterisks

plyz cud some1 help me out with displaying
1*****
12****
123***
1234**
12345*
123456
using for loop statement
Try with this :

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include <iostream>
#define  M   6

using namespace std;

int main()
{

  for(int i(1) ; i <= M ; i++)
  {
   for(int j(1) ; j <= M ; j++)
   {
     if (j <= i)
      cout << j;
     else
      cout << '*';
   }
   cout << endl;
  }
  return 0;
}
。。。。。。。。。。。。。。。。。。。。。。。
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

void fadeaway(const std::string& a, const std::string& b, std::size_t start_pos=1)
{
    std::size_t size = (a.size() < b.size() ? a.size() : b.size()) + 1;

    for (std::size_t pos = start_pos; pos < size; ++pos)
        std::cout << a.substr(0, pos) << b.substr(pos) << '\n';
}

int main()
{
    fadeaway("123456", "******");
}
Last edited on
i tried the above question but could not find y my loop is running jus once,help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
    char str[]="******";
    int len=strlen(str);
    char num[1];
    int j;
    for(int i=0;i<len;i++)
    {
        j=i+1;
        sprintf(num,"%d",j);
        str[i]= num[0];
        cout<<str;
    }

}
mehak wrote:
i tried the above question but could not find y my loop is running jus once,help!

Read the documentation for sprintf. Note any requirements for the buffer that is written to.
Topic archived. No new replies allowed.