how to make a program which the out put is:
enter n=5

1 2 3 4 5 4 3 2 1         
      2 3 4 5 4 3 2
         3 4 5 4 3
            4 5 4
               5

this is the code i use but an error occur

#include <iostream.h> 
 
void main()
{ 
 
int a=10; 
int n,i,j=0,k; 
 
cout<<"Enter the value for n: "; 
cin>>n; 
k=n; 
 
for (i=0 ;i < 10; i++) 
a[I]=i + 1; [/I]
 
[I]while(k>0){ [/I]
[I]for(i=(0+j);i<n;i++) [/I]
[I]cout<<a[I]<<" "; [/I]
[I]for(i=n-2;i>=(0+j);i--) [/I]
[I]cout<<a[I]<<" "; [/I]
[I]cout<<endl; [/I]
[I]j++; [/I]
[I]for(i=0;i<j;i++) [/I]
[I]cout<<" "; [/I]
[I]k--; [/I]
[I]} [/I]
[/I][/I]

another i really need to fin the proper code for factorial using successive addition

this is the code i use

int main()
{
int num, fact = 6,temp =1,i ,j;
cout<<"Enter a factorial ";
cin>>num;
 
for(i=2;i<=num;i++)
fact=0;
{for(j=1;j<=1;j++)-------said code dosnt have effect
fact=fact+temp;
temp=fact;
}
}

please someone help me...thanks

>>this is the code i use but an error occur
what is (are) the error(s) ?

in the first code actually i cant say its an erro but error in the codebecause when i try to run it
the output is

1 1 1 1 1 1 1 1 1
   1 1 1 1 1 1 1
      1 1 1 1 1
         1 1 1
            1 

why is the result display that one

second code

{for(j=1;j<=1;j++)-------said code doesnt have effect ?????

please help

I suspect your code compiles but provides unexpeceted output. I think it helps if you write out what you want to do first, then try to write code. I've attached a number of comments to your code indicating what I think your code is doing. If you write out what needs to get done to display the pattern you want I think you will find several logic errors.

Also when posting code to this board please use code tags to maintain indentation and spacing, which I hope you use in your code, as it makes it much more readable and much easier to pick up a number of types of errors (none of which I can find in this code).

#include <iostream.h> 

//use int main(), not void main()
void main()
{ 
  int a = 10; 
  int n, i, j=0, k; 

  cout << "Enter the value for n: "; 
  cin >> n; 
  k=n; 

  //overwrite the value of a 10 times
  for (i = 0; i < 10; i++) 
     a = i + 1; 

  /*a now equals 10.  Since a never changes for the rest of the program it will be 10 for the rest of the program.*/

  while(k > 0) //control number of lines
  { 
     //General flow:
     //each line will have three sections
        //initially is a series of spaces
        //in the middle is a series of numbers
        //int the end is a series of numbers

     //zero plus any number is always the number so drop the zeros

     //on the middle part of the line display a and a space n - j times  
     for(i = (0 + j); i < n; i++) 
        cout << a << " "; 

     //at the end of the line display a plus a space n - 2 - j times
     for(i = n - 2; i >= (0 + j); i--) 
        cout << a << " "; 

     //start a new line
     cout<<endl; 

     j++;
 
     //display j spaces initially on the new line
     for(i = 0; i < j; i++) 
        cout << " "; 

     k--; 
}

>>for(j=1;j<=1;j++)
It will never loop more than 1 time therefore you might as well delete that line. There is no point in writing a loop that only loops one time.

Dont use void at the main part.

Dont use void at the main part.

Even I had trouble trying to figure out what this meant :icon_wink:

What he means is this

>> really need to fin the proper code for factorial using successive addition

I think the factorial code has a combination of logic and syntax errors. Here's my logic:

factorial is successive multiplication:

3! = 3 * 2 * 1

mutiplication can be viewed as serial addition

3 * 2 = 3 + 3

Note that that means 3! is same as 3 * 2! and 4! is

4 * 3 * 2 = 4 * (3 + 3) = 4 * 6 = 4 + 4 + 4 + 4 + 4 + 4

or 4! = 4 * 3!;

So by extension, n! = n * (n - 1)!

This means you should be able to start with the lower factorials and work your way up to the desired factorial. Which is what I think your code is trying to do, but explaining your logic with comments is very important. And here's the piece of the logic that I think you have wrong. Each new factorial on the way up is the current number added to itself by the prior factorial number of times.

And here's my comments on your code using my logic on your code and commenting on the errors I see.

int main()
{
  int num, fact = 6, temp =1, i , j;  //initialize fact to 1.
  cout<<"Enter a factorial ";
  cin>>num;

for(i = 2; i <= num; i++)
  fact=0;  /*don't reset fact to zero every time.  fact is ever increasing each time through the inner loop.  It should be the same as the current number, not zero each time the inner loop starts.  You can figure out what variable represents the current number*/

//here's one of those errors from not indenting properly.
{
  for(j = 1; j <= 1; j++)-------said code doesnt   have effect---because the { should be right after the first for loop, not where it is.

  /*this loop controls how many times the current number is added to itself to get the current factorial.  the terminating condition is wrong as written, you need the prior factorial repeats.  I'll let you figure out which variable in your code represents the prior factorial.*/
  

  
     fact=fact+temp; //you're adding the wrong value each time through the loop.  

     temp=fact; //this should be outside the inner loop
  }
}

//This is my code to solve your question.Want a go?

#include <iostream>
using namespace std;
int main()
{
    int num;
    cout<<"Enter a factorial ";
    cin>>num;
    for(int line=1;line<=num;line++){
            int list=line;
        for(int temp1=1;temp1<list;temp1++){
                cout<<' ';
        }
        for(int temp2=list;temp2<num;temp2++)
        cout<<temp2;
        for(int temp3=num;temp3>=list;temp3--)
        cout<<temp3;
        
        cout<<endl;
                
    }
}

//I am sorry for that I hanvn't notice the char ' ' in your program. Now I rewrite my code in the following:

#include <iostream>
using namespace std;
int main()
{
    int num;
    cout<<"Enter a factorial ";
    cin>>num;

    for(int line=1;line<=num;line++){
            int list=line;
        for(int temp1=1;temp1<list;temp1++)
                cout<<' '<<' '<<' ';
        for(int temp2=list;temp2<num;temp2++)
        cout<<temp2<<' ';
        for(int temp3=num;temp3>=list;temp3--)
        cout<<temp3<<' ';

        cout<<endl;         
    }
    cin>>num;
}
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.