-5

i need help for coding a program which is read N and will do following:

if n numbers given program will print n, n^2, n^3.

For example if user (or variable) 5; program output will be like this:

n=5

output:
2 4
3 9 27
4 16 64 256
5 25 125 625 3125

note: we should not use pow function.

can anyone help? Thanks.

kadir
  • 29
  • 6
  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Nov 18 '18 at 08:40
  • Your question description does not match your example output. You wrote that you want to print n, n^2, and n^3, but your example output shows i^1, i^2, ..., i^i for 2 <= i <= n. Which do you want? – Rory Daulton Nov 18 '18 at 11:35
  • ok, you are right, i want given output in the example. – kadir Nov 18 '18 at 15:28

3 Answers3

2

You can do given below in c++

#include <bits/stdc++.h>
#define ll long long

using namespace std;

void solve(ll x){
    for(int i = 1;i <= x;i++)
        cout << pow(x,i) << " ";
    cout << endl;
}

int main() {
    ll n;
    cin >> n;
    for(int i = 2;i <= n;i++){
        solve(i);
    }
    return 0;
}
code_cody97
  • 100
  • 9
  • If your code giving timeout for large values then you can use optimized power function..`@kadir` link https://www.geeksforgeeks.org/write-an-iterative-olog-y-function-for-powx-y/ – code_cody97 Nov 18 '18 at 09:10
  • 1
    ok man..only use `scanf,printf` statements instead of `cin,cout` respectively and instead of `#include ` use `#include `.. – code_cody97 Nov 18 '18 at 09:22
1
#include<stdio.h>
int main()
{
    int i,j,n,pro;
    printf("Enter n:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        pro=1;
        for(j=1;j<=i;j++)
        {
            pro=pro*i;
            printf("%d ",pro);
        }
        printf("\n");
   }
}
1

like this:

#include <stdio.h>

int main() {
  int N;
  scanf("%d", &N);

  for (int i = 2; i <= N; ++i) {
    for (int j = 0, k = i; j < i; ++j, k *= i)
      printf("%d ", k);
    printf("\n");
  }

  return 0;
}
Yunbin Liu
  • 1,484
  • 2
  • 11
  • 20