Josh Interview Question for Software Developers


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
1
of 1 vote

public class PrintTriangle {
    public static void main(String[] args) {
        int max = 4;
        printSequence(1,1,max);
    }

    private static void printSequence(int start, int endOffset, int maxRow) {
        if(maxRow > 0) {
            StringBuilder builder = new StringBuilder();
            for(int i=start; i< start + endOffset; i++) {
                builder.append(i + "*");
            }
            System.out.println(builder.toString().substring(0,builder.toString().length()-1));
            printSequence(start + endOffset, endOffset + 1, maxRow - 1);
            System.out.println(builder.toString().substring(0,builder.toString().length()-1));
        }
    }
}

- newspecies March 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

You can use formula n(n-1)/2 to find starting number for nth row.

startNum = n(n - 1)/2 + 1, where n is the row number

void printTriangles(int n) {
	int i = 1;
	while(i <= n)
		printRow(i++);
        i--;
	while(i > 0)
		printRow(i--);
}

void printRow(int n) {
	int startNum = n * (n - 1) / 2 + 1;
	while(n > 0) {
		cout << startNum++;
		if(n > 1)
			cout << " * ";
	}
}

- blue-j March 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

here's the right code of your logic

void printRow(int n) {
	int startNum = n * (n - 1) / 2 + 1;
	while(n > 0) {
		cout << startNum++;
		if(n > 1)
			cout << " * ";
			n--;
	}
}
void print(int n) {
	int i = 1;
	while(i <= n){
		printRow(i++);
        cout<<"\n";
		}
		i--;
	while(i > 0){
		printRow(i--);
		cout<<"\n";
		}
}

- JerryGoyal April 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

class Main {
    
    public static void Print(int num, int max, int next)
    {
        if (num == max + 1) return;
        
        int tmpNext = next;
        
        for (int i = 0; i < num; i++) {
            System.out.print(++tmpNext + ((i < num - 1) ? "*" : "\n"));
        }
        

        Print(num + 1, max, tmpNext);
        
        for (int i = 0; i < num; i++) {
            System.out.print(++next + ((i < num - 1) ? "*" : "\n"));
        }
    }
    
      public static void main(String[] args)
      {
            Print(1, 4, 0);
     }
}

- brampersad March 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the code which i wrote to print above triangle

int num=1;
   for(int i=1;i<=5;i++)
   {
       for(int j=1;j<=i;j++)
       {
       cout<<num;
       if(i!=j)
       {
       cout<<"*";
       }
       num++;
   }
   cout<<endl;
   }

- deepanshuchg March 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The above solution prints only the above triangle.
i hope the current code snippet below prints both the above and below triangle.

#include<iostream>
using namespace std;


int main()
{
int k=1,i,l;
int n=4;
   for(i=1;i<=n;i++)
   {
       for(int j=1;j<=i;j++)
       {
          cout<<k++<<"*";
          }
   cout<<endl;
   }  
    l=k-i+1;
   for(int i=n;i>=1;i--)
   {
      
       for(int j=1;j<=i;j++)
       {
          cout<<l++<<"*";
          }
     l=(l-1)-2*(i-1);      
   cout<<endl;
   }  
   return 0;
}

- wilky singh April 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

int recur(int n){
    if(n==1) {
        return 1;
    }
    return recur(n-1)+n;
}

void printPiramid(int n){

	int num = recur(n);
	int aux;
	for (int i=4;i>-1;i--){
        aux = num - i + 1;
		for(int j=0;j<i;j++){
            if(j==0){
                cout<<aux;
            }
            else{
                cout<<'*'<<aux;
            }
			aux++;
		}
        num = num - i;
		cout<<endl;
	}
}

int main(){
    printPiramid(4);
}

- This is a brute force solution March 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Totally working solution

void PrintSeq(int n);
int Printfirsthalf(int count, int startelement);
int PrintSecondHalf(int count, int lastelement);


void PrintSeq(int n)
{
if (n == 0)
{
return;
}

int startelement = 1;

for (int i = 1; i <= n; i++)
{
startelement = Printfirsthalf(i, startelement);
}

int lastelement = startelement - 1;

for (int i = n; i >= 1; i--)
{
lastelement = PrintSecondHalf(i, lastelement);
}
}

int Printfirsthalf(int count, int startelement)
{
cout << endl;

int iRet = startelement;

for (int i = 0; i < count; i++)
{
cout << iRet++;

if (i+1 < count)
{
cout<< "*";
}
}

return iRet;
}

int PrintSecondHalf(int count, int lastelement)
{
cout << endl;

int iRet = lastelement;

for (int i = count - 1; i >=0; i--)
{
cout << iRet - i;

if (i > 0)
{
cout << "*";
}
}

return iRet - count;
}

- WOW March 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code is written in C#. Most of the awnsers only complete the top half of the triangle

public static void PrintTriangle(int n){
		int count = 1;
		for(int i=1; i<n+1;i++){
			for(int j=1; j<=i;j++){
				Console.Write(count);
				count++;
			}
			Console.WriteLine("");
		}
		
		for(int k=n;k>0;k--){
			int tempCount = k;
			for(int l=1; l<=k;l++){
				int temp = count - tempCount;
				Console.Write(temp);
				tempCount--;
			}
			count -= k;
			Console.WriteLine("");
		}
	}

- Matt March 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code is in C#. Most of the awnsers only print the top half of the triangle.

public static void PrintTriangle(int n){
		int count = 1;
		for(int i=1; i<n+1;i++){
			for(int j=1; j<=i;j++){
				Console.Write(count);
				count++;
			}
			Console.WriteLine("");
		}
		
		for(int k=n;k>0;k--){
			int tempCount = k;
			for(int l=1; l<=k;l++){
				int temp = count - tempCount;
				Console.Write(temp);
				tempCount--;
			}
			count -= k;
			Console.WriteLine("");
		}

}

- matt March 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code is in C#. Alot of awnsers here are only printing the top half of the triangle instead of the full triangle

public static void PrintTriangle(int n){
		int count = 1;
		for(int i=1; i<n+1;i++){
			for(int j=1; j<=i;j++){
				Console.Write(count);
				count++;
			}
			Console.WriteLine("");
		}
		
		for(int k=n;k>0;k--){
			int tempCount = k;
			for(int l=1; l<=k;l++){
				int temp = count - tempCount;
				Console.Write(temp);
				tempCount--;
			}
			count -= k;
			Console.WriteLine("");
		}
	}

- matt March 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void PrintPattern(int n)
        {
            PrintPatternImpl(n, 1, 1);
        }

        public static void PrintPatternImpl(int n, int current, int startingNumber)
        {
            int result=1;
            if (n >= current)
            {
                for (int i = 1; i <= current; i++, startingNumber++)
                {
                    result *= startingNumber;
                }
                Console.WriteLine(result);
                PrintPatternImpl(n, current + 1, startingNumber);
                Console.WriteLine(result);
            }
        }

- Feldman.Max March 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code written in C

#include <stdio.h>
#include <stdlib.h>

int draw_line(int start,int limit,int flag){
int count=0,copy=start;
while(count<limit-1){
printf("%d*",++start);
count++;
}
printf("%d\n",++start);
if(flag==0)
return start;
else return copy;
}

int rev_print(int i,int n){
if(i<=1)
{
    printf("1");
    return 1;
}
else{
 draw_line(i-n,n,1);
 return rev_print(i-n,n-1);
}
}


int print(int i,int n){
if(i==1){
printf("1\n");
return 1;
}
else{
   int k=print(i-1,n);
   int ret=draw_line(k,i,0);
   if(i==n)
    rev_print(ret,n);
   return ret;
}
}

int main()
{
  int n=4;
 print(n,n);
    return 0;
}

- gowtham kesa March 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void Main(string[] args)
        {
            int n = 8, j;
            for(int i = 1, k = 1; i <= n*2; i++)
            {
                if (i <= n)
                {
                    for (j = k; j < i + k; j++)
                        Console.Write(j + " ");
                    if (i != n) k = j;
                }
                else
                {
                    for (j = k; j < n - (i - n) + 1 + k; j++)
                        Console.Write(j + " ");
                    k = j + 1 - (n - (i - n) + 1)*2;
                }
                Console.WriteLine();
            }
        }

- ulmaxy March 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void PrintIncrementingTriangle(int currentDepth, int endingDepth)
{
	int currentNumber = ((currentDepth * (currentDepth - 1)) / 2) + 1;

	std::stringstream ssOutput;

	// Build string
	for (int numbersToDisplay = 0; numbersToDisplay < currentDepth; numbersToDisplay++)
	{
		if (numbersToDisplay != 0)
		{
			ssOutput << " * ";
		}

		ssOutput << currentNumber;

		currentNumber++;
	}

	ssOutput << endl;

	// Print string
	cout << ssOutput.str();

	// Print next string if necessary
	if (currentDepth < endingDepth)
	{
		PrintIncrementingTriangle(currentDepth + 1, endingDepth);
	}

	// Print bottom half of triangle as we recurse out
	cout << ssOutput.str();
}

- CareerCupDom March 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream.h>
#include<string.h>

int return_j(int n)
{
return(((n*(n+1))/2));
}

int main()
{
int i=0,j=0,n;
cout << " Enter n \n";
cin >> n;

for(i=0;i<n;i++)
{
for(int l=0;l<=i;l++)
{
j++;
cout<<j;
if(!(l==i))
cout<<"*";
}
cout << endl;
}

for(i=n-1;i>=0;i--)
{
j=return_j(i);
for(int l=0;l<=(i);l++)
{
j++;
cout<<j;
if(!(l==i))
cout<<"*";
}
cout << endl;
}

}

- Kunal Bansal March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream.h>
#include<string.h>
int return_j(int n)
{
return(((n*(n+1))/2));
}
int main()
{
int i=0,j=0,n;
cout << " Enter n \n";
cin >> n;
for(i=0;i<n;i++)
{
for(int l=0;l<=i;l++)
{
j++;
cout<<j;
if(!(l==i))
cout<<"*";
}
cout << endl;
}
for(i=n-1;i>=0;i--)
{
j=return_j(i);
for(int l=0;l<=(i);l++)
{
j++;
cout<<j;
if(!(l==i))
cout<<"*";
}
cout << endl;
}
}

- Kunal Bansal March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream.h>
#include<string.h>

int return_j(int n)
{
return(((n*(n+1))/2));
}

int main()
{
int i=0,j=0,n;
cout << " Enter n \n";
cin >> n;

for(i=0;i<n;i++)
{
for(int l=0;l<=i;l++)
{
j++;
cout<<j;
if(!(l==i))
cout<<"*";
}
cout << endl;
}

for(i=n-1;i>=0;i--)
{
j=return_j(i);
for(int l=0;l<=(i);l++)
{
j++;
cout<<j;
if(!(l==i))
cout<<"*";
}
cout << endl;
}

}

- Kunal Bansal March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java Solution using mathematical equation.

public class Careercup {
	
	public static int counter=0;
	public static int temp;
	public static int temp2;
	public static void main(String[] args) {
		
		int n =5;
		temp=n*(n+1)/2;
		temp2=n*(n+1)/2;
		for(int i=1;i<=2*n;i++){
			
			temp=temp2;
			for(int j=1; j<=(i>n?(2*n-i)+1:i); j++){
				
				counter+=1;
				if(counter<=n*(n+1)/2){
				
				System.out.print(counter+"*");
				}
				if(counter>n*(n+1)/2){
					System.out.print(((temp-(i>n?(2*n-i)+1:i))+j)+"*");
					temp2--;
				}
				
			}
			System.out.println();
		}
	}
}

- Mahaveer Jangir March 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// how bout this?

int N=4;
int i(1), c(1);
for(i=1; i<=N; ++i )
{
for( int j = 0; j<i; ++j)
std::cout << c++ << (j==(i-1) ? "" : "*");
std::cout << std::endl;
}

for(c-=N, --i; i>0; --i)
{
int j;
for( j = 0; j<i; ++j)
std::cout << c++ << (j==i-1 ? "" : "*");
c-=i+j-1;
std::cout << std::endl;
}

- randy April 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int N=4;
    int i(1), c(1);
    for(i=1; i<=N; ++i )
    {
        for( int j = 0; j<i; ++j)
            std::cout << c++ << (j==(i-1) ? "" : "*"); 
        std::cout << std::endl;
    }

    for(c-=N, --i; i>0; --i)
    {
        int j;
        for( j = 0; j<i; ++j)
            std::cout << c++ << (j==i-1 ? "" : "*"); 
        c-=i+j-1;
        std::cout << std::endl;
    }

- randy April 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int N=4;
    int i(1), c(1);
    for(i=1; i<=N; ++i )
    {
        for( int j = 0; j<i; ++j)
            std::cout << c++ << (j==(i-1) ? "" : "*"); 
        std::cout << std::endl;
    }

    for(c-=N, --i; i>0; --i)
    {
        int j;
        for( j = 0; j<i; ++j)
            std::cout << c++ << (j==i-1 ? "" : "*"); 
        c-=i+j-1;
        std::cout << std::endl;
    }

- randy April 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printPattern()
{
	int k = 1;
	auto i = 0;
	for(; i <= 4; ++i)
	{	
		int l = 0;
		for(auto j = 0; j <= i; ++j)
		{		
			if(i == j)
			{
				cout << j + k << endl;
				++l;
			}
			else
			{
				cout << j + k << "*";
				++l;
			}
		}
		k = k + l;
	}
	k = k - i;
	for(auto i = 4; i >= 0; --i)
	{	
		int l = 0;
		for(auto j = 0; j <= i; ++j)
		{		
			if(i == j)
			{
				cout << j + k << endl;
				++l;
			}
			else
			{
				cout << j + k << "*";
				++l;
			}
		}
		k = k - l + 1;
	}

}

- qasim.hasnain13 April 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;


int main()
{
int k=1,i,l;
int n=4;
   for(i=1;i<=n;i++)
   {
       for(int j=1;j<=i;j++)
       {
          cout<<k++<<"*";
          }
   cout<<endl;
   }  
    l=k-i+1;
   for(int i=n;i>=1;i--)
   {
      
       for(int j=1;j<=i;j++)
       {
          cout<<l++<<"*";
          }
     l=(l-1)-2*(i-1);      
   cout<<endl;
   }  
   return 0;
}
//

- wilky singh April 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wilky ..your code length is short & seems good...

- Shamin_shweta April 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

{wilky ..your code length is short & seems good...}

- Shamin_shweta April 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Allah k wasta.

- Moin April 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use recursion for below triangle

void print(int k,int s){
	if(s==1){
		return;
	}
	print(k-1,s-1);
	cout<<"*"<<k;
	
}
main()
{
int i,t,n;
int k=10;
int size=4;
while(size!=0){
	
	print(k,size);
	k=k-size+1;
	k--;
	size--;
	cout<<"\n";
}

- JerryGoyal April 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <stdio.h>
using namespace std;

void PrintTriangle(int start,int row,int n)
{
if(row<=n)
{
for(int i=0;i<row;i++)
{
printf("%d\t",start++);
}
row++;
printf("\n");

PrintTriangle(start,row,n);
if(row<=n)
{
for(int i=0;i<row;i++)
{
printf("%d\t",start++);
}
printf("\n");
}
}
}
int main() {
// your code goes here
PrintTriangle(1,1,4);
return 0;
}

- Anonymous April 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printTriangles(int n)
{
int i = 1;
while(i <= n)
printRow(i++);
i--;
while(i > 0)
printRow(i--);
}
void printRow(int n)
{
int startNum = n * (n - 1) / 2 + 1;
int count=0;
while(count < n) {
printf("%d",startNum++);
/*if(n>1)
printf(" * ");*/
if(n-1==count)
break;
else
printf(" * ");
count++;
}
printf("\n");
}

- vinod kande July 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <stdio.h>

using namespace std;

void print_row(int row) {
    int start = (row * (row - 1)/2);
    for (int i = 1; i <= row; i++ ) {
        if (i != 1)
            cout<<"*";
        cout<<++start;
    }
}
void print_upper_triangle(int rows) {
    for (int j=1; j<=rows; j++) {
        print_row(j);
        cout<<endl;
    }
}
void print_lower_triangle(int rows) {
    for (int j=rows; j>=1; j--) {
        print_row(j);
        cout<<endl;
    }
}

int main(int argc, const char * argv[]) {
    print_upper_triangle(4);
    print_lower_triangle(4);
    return 0;
}

- Anonymous August 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <stdio.h>

using namespace std;

void print_row(int row) {
    int start = (row * (row - 1)/2);
    for (int i = 1; i <= row; i++ ) {
        if (i != 1)
            cout<<"*";
        cout<<++start;
    }
}
void print_upper_triangle(int rows) {
    for (int j=1; j<=rows; j++) {
        print_row(j);
        cout<<endl;
    }
}
void print_lower_triangle(int rows) {
    for (int j=rows; j>=1; j--) {
        print_row(j);
        cout<<endl;
    }
}

int main(int argc, const char * argv[]) {
    print_upper_triangle(4);
    print_lower_triangle(4);
    return 0;

}

- Anonymous August 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main(){
int i,j,k=1,n;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++){
for(j=1;j<=i;j++)
{
printf("%d",k);
k++;

if(j<i){
printf("*");
}

}
printf("\n");
}

k=k-n;
for(i=n;i>=1;i--){
for(j=1;j<=i;j++)
{
printf("%d",k);
k++;
if(j<i)
printf("*");
}

k=(k+1)-2*i;
printf("\n");
}
return 0;
}

- Sachin Yadav October 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main(){
int i,j,k=1,n;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++){
for(j=1;j<=i;j++)
{
printf("%d",k);
k++;

if(j<i){
printf("*");
}

}
printf("\n");
}

k=k-n;
for(i=n;i>=1;i--){
for(j=1;j<=i;j++)
{
printf("%d",k);
k++;
if(j<i)
printf("*");
}

k=(k+1)-2*i;
printf("\n");
}
return 0;

}

- Sachin Yadav October 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

main()
{
int i,j,k=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
qDebug()<<k;
k++;
}
qDebug()<<"\n";
}

for(i=n;i>=1;i--)
{
k-=i;
for(j=1;j<=i;j++)
{

qDebug()<<k;
k++;
}
k-=--j;
qDebug()<<"\n";
}
}

- sravani January 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void main()
{
int i,j,k=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
qDebug()<<k;
k++;
}
qDebug()<<"\n";
}

for(i=n;i>=1;i--)
{
k-=i;
for(j=1;j<=i;j++)
{

qDebug()<<k;
k++;
}
k-=--j;
qDebug()<<"\n";
}
}

- sravani January 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
//Function to Print Numbers from A to B.
void rPrintNumber(int, int);
//Function to Print Pattern Recursively.
void rPrintPattern(int,int,int);
//Function to Print Patter Iteratively.
void iPrintPattern(int);
void main(void)
{
int X;
char C;
cout<<"Length :",cin>>X;
cout << "Recursion or Iteration (R/I):", cin >> C;
if (C == 'R')
{
rPrintPattern(1, 1, X);
}
else iPrintPattern(X);
}
void rPrintPattern(int CurrLen,int CurrentDigit,int LastLen)
{
if (CurrLen == LastLen)
rPrintNumber(CurrentDigit, CurrentDigit + LastLen-1);
else
{
rPrintNumber(CurrentDigit, CurrentDigit + CurrLen-1);
rPrintPattern(CurrLen+1, CurrentDigit + CurrLen, LastLen);
}
rPrintNumber(CurrentDigit, CurrentDigit + CurrLen-1);
}
void rPrintNumber(int Curr, int Last)
{
if (Curr == Last)
cout << Curr<<endl;
else
{
cout << Curr<<'*';
rPrintNumber(Curr + 1, Last);
}
}
void iPrintPattern(int X)
{
int CurrentDigit = 1;
//Upper Triangle.
for (int CurrentLen = 1 ;CurrentLen <= X; ++CurrentLen)
{
for (int V = 0; V < CurrentLen; ++V)
{
if (V == CurrentLen - 1)
cout << CurrentDigit;
else cout << CurrentDigit << "*";
++CurrentDigit;
}
cout << endl;
}
//Lower Triangle.
for (int CurrentLen = X; CurrentLen > 0; --CurrentLen)
{
for (int V = 0; V < CurrentLen; ++V)
{
if (V == CurrentLen - 1)
cout << (CurrentDigit - (CurrentLen - V ));
else cout << (CurrentDigit - (CurrentLen - V ))<<"*";
}
CurrentDigit -= CurrentLen;
cout << endl;
}
}

- Anonymous January 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
//Function to Print Numbers from A to B.
void rPrintNumber(int, int);
//Function to Print Pattern Recursively.
void rPrintPattern(int,int,int);
//Function to Print Patter Iteratively.
void iPrintPattern(int);
void main(void)
{
int X;
char C;
   cout<<"Length :",cin>>X;
   cout << "Recursion or Iteration (R/I):", cin >> C;
   if (C == 'R')
   {
	   rPrintPattern(1, 1, X);
   }
   else iPrintPattern(X); 
}
void rPrintPattern(int CurrLen,int CurrentDigit,int LastLen)
{
	if (CurrLen == LastLen)
		rPrintNumber(CurrentDigit, CurrentDigit + LastLen-1);
	else
	{
	  rPrintNumber(CurrentDigit, CurrentDigit + CurrLen-1);
	  rPrintPattern(CurrLen+1, CurrentDigit + CurrLen, LastLen);
	}
	rPrintNumber(CurrentDigit, CurrentDigit + CurrLen-1);
}
void rPrintNumber(int Curr, int Last)
{
	if (Curr == Last)
		cout << Curr<<endl;
	else
	{
		cout << Curr<<'*';
		rPrintNumber(Curr + 1, Last);
	}
}
void iPrintPattern(int X)
{
int CurrentDigit = 1;
   //Upper Triangle.
	for (int CurrentLen  = 1 ;CurrentLen <= X; ++CurrentLen)
	{
		for (int V = 0; V < CurrentLen; ++V)
		{
			if (V == CurrentLen - 1)
				cout << CurrentDigit;
			else cout << CurrentDigit << "*";
			++CurrentDigit;
		}
		cout << endl;
	}
	//Lower Triangle.
	for (int CurrentLen = X; CurrentLen > 0; --CurrentLen)
	{
		for (int V = 0; V < CurrentLen; ++V)
		{
			if (V == CurrentLen - 1)
				cout << (CurrentDigit - (CurrentLen - V ));
			else  cout << (CurrentDigit - (CurrentLen - V ))<<"*";
		}
		CurrentDigit -= CurrentLen;
		cout << endl;
	}
}

- Atif Farooq January 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{ int n;
cout<<"\nInput n"<<endl;
cin>>n;
int num = 1;
//upper triangle
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= (2*i - 1); j++) {
if(j%2) cout<<num++;
else cout<<'*';
}cout<<endl;
}
//lower triangle
for(int i = n; i >= 1; i--) {
num = num - i; //reset the value to be printed
for(int j=1; j <= (2*i - 1); j++) {
if(j%2) cout<<num++;
else cout<<'*';
}cout<<endl;
num = num - i;
}
return 0;
}

- rashmi rai February 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
int n;
cout<<"\nInput n"<<endl;
cin>>n;
int num = 1;
//upper triangle
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= (2*i - 1); j++) {
if(j%2) cout<<num++;
else cout<<'*';
}cout<<endl;
}
//lower triangle
for(int i = n; i >= 1; i--) {
num = num - i; //reset the value to be printed
for(int j=1; j <= (2*i - 1); j++) {
if(j%2) cout<<num++;
else cout<<'*';
}cout<<endl;
num = num - i;
}
return 0;
}

- rashmi rai February 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

int main() 
{
	int n, i, j, count = 0, counter;
	cin>>n;
	for(i = 0 ; i < n ; i++)
	{
		for(j = 0 ; j <= i ; j++)
		{
			if(j != i)
				cout<<++count<<"*";
			else
				cout<<++count;
		}
		cout<<"\n";
	}
	for(i = n ; i > 0 ; i--)
	{
		count -= i;
		counter = count;
		for(j = i ; j > 0 ; j--)
		{
			if(j != 1)
				cout<<++counter<<"*";
			else
				cout<<++counter;
		}
		cout<<"\n";
	}
	
	return 0;
}

- Sarthak March 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int k = 1; int i = 0;
for (; i <= 6; i = i + 2)
{
int j = 0;
while (j <= i)
{
if (j % 2 == 1)
{
Console.Write("*");

}
else
{
Console.Write(k);
k++;
}
j++;
}

Console.WriteLine();
}

k = i-1;
i = i - 2;
for (; i >= 0; i = i - 2)
{
int j = 0;
while (j <= i)
{
if (j % 2 == 1)
{
Console.Write("*");

}
else
{

Console.Write(k);
k++;

}
j++;
}

k = k - j;
Console.WriteLine();

}

- s2rocks April 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int k = 1; int i = 0;
for (; i <= 6; i = i + 2)
{ int j = 0;
while (j <= i)
{
if (j % 2 == 1)
{
Console.Write("*");
}
else
{
Console.Write(k);
k++;
}
j++;
}
Console.WriteLine();
}
k = i-1;
i = i - 2;
for (; i >= 0; i = i - 2)
{
int j = 0;
while (j <= i)
{
if (j % 2 == 1)
{
Console.Write("*");
}
else
{
Console.Write(k);
k++;
}
j++;
}
k = k - j;
Console.WriteLine();
}

- s2rocks April 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int k = 1; int i = 0;
            for (; i <= 6; i = i + 2)
            {                int j = 0;
                while (j <= i)
                {
                    if (j % 2 == 1)
                    {
                        Console.Write("*");
                    }
                    else
                    {
                        Console.Write(k);
                        k++;
                    }
                    j++; 
                }
                Console.WriteLine();
             }
                     k = i-1;
            i = i - 2;
            for (; i >= 0; i = i - 2)
            {
                int j = 0;
                while (j <= i)
                {
                    if (j % 2 == 1)
                    {
                        Console.Write("*");
                    }
                    else
                   {
                        Console.Write(k);
                       k++;
                    }
                    j++;
                }
                k = k - j;
                Console.WriteLine();

}

- Anonymous April 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Loop through rows, printing each row and storing each row in a vector, then loop through vector using a reverse iterator.

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

void printPyramid( int size )
{
    int currNum = 0;
    std::vector<std::string> rows;
    int row = 0;
    
    for( ; row <= size; ++row )
    {
        std::ostringstream oss;
        for( int numbers = 0; numbers < row; ++numbers )
        {
            ++currNum;
            std::cout << "\t" << row << ":" << numbers << ":" << currNum << std::endl;
            oss << currNum;
            if( (numbers + 1) != row )
            {
                oss << "*";
            }
            else
            {
                std::cout << oss.str() << std::endl;
                rows.push_back(oss.str());
            }
        }
    }

    for( std::vector<std::string>::const_reverse_iterator rIter = rows.rbegin(); 
         rIter != rows.rend(); 
         ++rIter )
    {
        std::cout << *rIter << std::endl;
    }
}

int main()
{
    printPyramid(4);    
    return 0;
}

- merlinme April 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main()
{
int num=1,k,row=4,i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",num);
if(i!=j)
{
printf("*");
}
num++;
}
printf("\n");
}
num=num-row;
k=(row*2)+1;
for(i=row;i>=1;i--)
{
k=k-2;
for(j=1;j<=i;j++)
{
printf("%d",num);
if(i!=j)
{
printf("*");
}
num++;
}
printf("\n");
num=num-k;
}
return 0;
}
I hope this will do the job

- Lareb Zafar April 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main()
{
int num=1,k,row=4,i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",num);
if(i!=j)
{
printf("*");
}
num++;
}
printf("\n");
}
num=num-row;
k=(row*2)+1;
for(i=row;i>=1;i--)
{
k=k-2;
for(j=1;j<=i;j++)
{
printf("%d",num);
if(i!=j)
{
printf("*");
}
num++;
}
printf("\n");
num=num-k;
}
return 0;
}

- Lareb Zafar April 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;


int main()
{
int k=1,i,l;
int n=4;
for(i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{ if(i==j)
cout<<k++;
else
cout<<k++<<"*";
}
cout<<endl;
}
l=k-i+1;
for(int i=n;i>=1;i--)
{

for(int j=1;j<=i;j++)
{
if(i==i)
cout<<l++;
else
cout<<l++<<"*";
}
l=(l-1)-2*(i-1);
cout<<endl;
}
return 0;
}

- anuj pachauri May 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int j,b;
b=(i*(i-1))/2+1;
j=i;
while(j>0)
{
printf("%d",b);
b=b+1;
j--;
if(j!=0)
printf("*");
}
printf("\n");
}
for(int i=n;i>=1;i--)
{
int j,b;
b=(i*(i-1))/2+1;
j=i;
while(j>0)
{
printf("%d",b);
b=b+1;
j--;
if(j!=0)
printf("*");
}
printf("\n");
}
return 0;
}

- MCawesome September 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int j,b;
b=(i*(i-1))/2+1;
j=i;
while(j>0)
{
printf("%d",b);
b=b+1;
j--;
if(j!=0)
printf("*");
}
printf("\n");
}
for(int i=n;i>=1;i--)
{
int j,b;
b=(i*(i-1))/2+1;
j=i;
while(j>0)
{
printf("%d",b);
b=b+1;
j--;
if(j!=0)
printf("*");
}
printf("\n");
}
return 0;
}

- MCawesome September 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream.h>
using namespace std;
int main()
{
int k=1,i,l;
int n=4;
for(i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
cout<<k;
while(i>j)
{
if(k>j)
{
cout<<"*";
break;
}
}
k++;
}
cout<<endl;
}
l=k-i+1;
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
cout<<l;
if(i>j)
cout<<"*";
l++;
}
l=(l-1)-2*(i-1);
cout<<endl;
}
return 0;
}

- Anonymous September 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream.h>
using namespace std;
int main()
{
int k=1,i,l;
int n=4;
for(i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
cout<<k;
while(i>j)
{
if(k>j)
{
cout<<"*";
break;
}
}
k++;
}
cout<<endl;
}
l=k-i+1;
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
cout<<l;
if(i>j)
cout<<"*";
l++;
}
l=(l-1)-2*(i-1);
cout<<endl;
}
return 0;
}

- Anonymous September 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;
int main()
{
    int i,j,rows;
    cout<<"Enter the number of rows: ";
    cin>>rows;
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           cout<<j<<" ";
        }
        cout<<"\n";
    }
    for(i=rows-1;i>=1;--i)
    {
        for(j=1;j<=i;++j)
        {
           cout<<j<<" ";
        }
        cout<<"\n";
    }
    return 0;

}

- Nidhi Sharma March 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;
int main()
{
    int i,j,rows;
    cout<<"Enter the number of rows: ";
    cin>>rows;
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           cout<<j<<" ";
        }
        cout<<"\n";
    }
    for(i=rows-1;i>=1;--i)
    {
        for(j=1;j<=i;++j)
        {
           cout<<j<<" ";
        }
        cout<<"\n";
    }
    return 0;

}

- Nidhi Sharma March 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>

using namespace std;

int main() {
int i,j,x=0,l;
for(i=0;i<4;i++)
{
for(j=0;j<=i;j++)
{
x=x+1;
if(j!=i)
{cout<<x<<'*';}
else{cout<<x;}

}
cout<<endl;
}

l=x-4;
for(i=4;i>0;i--)
{
for(j=1;j<=i;j++)
{
l=l+1;

if(j!=i)
{cout<<l<<'*';}
else{cout<<l;}
}
l=(l-1)-2*(i-1);
cout<<endl;

}
}

- Abhishek Sharma November 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{#include <iostream>

using namespace std;

int main() {
int i,j,x=0,l;
for(i=0;i<4;i++)
{
for(j=0;j<=i;j++)
{
x=x+1;
if(j!=i)
{cout<<x<<'*';}
else{cout<<x;}

}
cout<<endl;
}

l=x-4;
for(i=4;i>0;i--)
{
for(j=1;j<=i;j++)
{
l=l+1;

if(j!=i)
{cout<<l<<'*';}
else{cout<<l;}
}
l=(l-1)-2*(i-1);
cout<<endl;
}
}
}

- Abhishek Sharma November 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Solution by Uddesh S. Sonawane
#include <iostream>

using namespace std;

int main()
{
for(int i=1,k=1;i<=4;i++)
{
for(int j=0;j<i;j++)
{
cout<<k;
if(i!=(j+1))
cout<<"*";
k++;
}
cout<<"\n";
}

int l=10;
for(int i=4,k=l;i>=1;i--)
{
k=l-i;
for(int j=0;j<i;j++)
{
k++;
cout<<k;
if(i!=(j+1))
cout<<"*";
l--;
}
cout<<"\n";
}
return 0;
}

/*OUTPUT

1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1

*/

- Uddesh Sonawane July 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Solution by Uddesh S. Sonawane
#include <iostream>

using namespace std;

int main()
{
    for(int i=1,k=1;i<=4;i++)
    {
        for(int j=0;j<i;j++)
        {
            cout<<k;
            if(i!=(j+1))
                cout<<"*";
            k++;
        }
        cout<<"\n";
    }
    
    int l=10;      
    for(int i=4,k=l;i>=1;i--)
    {
        k=l-i;
        for(int j=0;j<i;j++)
        {
            k++;
            cout<<k;
            if(i!=(j+1))
                cout<<"*";
            l--;
        }
        cout<<"\n";
    }
    return 0;
}

/*OUTPUT

1                                                                                                                                
2*3                                                                                                                              
4*5*6                                                                                                                            
7*8*9*10                                                                                                                         
7*8*9*10                                                                                                                         
4*5*6                                                                                                                            
2*3                                                                                                                              
1   

*/

- Uddesh Sonawane July 26, 2019 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More