Problem #1
Write a program in Java which accept 2 integer start & repeat and print the pattern such that starting and ending number of pattern is - start and total numbers or rows in the pattern is equal to (repeat*2). Number in the pattern should be increase by 1 in each rows till the middle of the rows and then decrease by 1 till end of the rows.
For example- If start = 4 and repeat = 4 then
For example- If start = 4 and repeat = 4 then
- start = 4 -> Starting number of pattern,
- (repeat * 2) = (4*2)= 8 -> number of rows to print in the pattern
4
55
666
7777
7777
666
55
4
Possible Solution
void printPattern(int start, int repeat) {
int k = repeat;
for (int i = 0; i < 2 * repeat; i++) { // loop 1 start
for (int j = 0; (j <= i && j < k); j++) { // loop 2
System.out.print(start);
} // end loop 2
if (i < repeat - 1) {
start = start + 1;
}
if (i > repeat - 1) {
start = start - 1;
k = k - 1;
}
System.out.println();
} // end loop 1
}
Output:
Test 1: (Start = 5 , repeat = 6)
5
66
777
8888
99999
101010101010
101010101010
99999
8888
777
66
5
Test 2: (Start = 3 , repeat = 4)
3
44
555
6666
6666
555
44
3
this question was askend in mindtree 2018
ReplyDeletethis pattern printing can also be done in java the code is..
import java.util.Scanner;
class Add{
public static void main(String[] args){
Scanner ob =new Scanner(System.in);
System.out.println("enter the starting point ");
int n=ob.nextInt();
System.out.print("enter the number of rows ");
int r=ob.nextInt();
r=r/2;
for(int i=1;i<=r;i++){
for(int j=1;j<=i;j++){
System.out.print(n);
}
n++;
System.out.println("");
}n=n-1;
for(int i=1;i<=r;i++){
for(int j=i;j<=r;j++){
System.out.print(n);
}
n--;
System.out.println("");
}
}
}
#include
ReplyDeleteint main()
{
int i,j,n,s,p;
scanf("%d%d",&s,&n);
for(i=0;i<n;i++){
for(j=1;j<=i+1;j++){
p=s+i;
printf("%d",p);
}
printf("\n");
}
for(i=0;i<n;i++){
for(j=1;j<=n-i;j++){
printf("%d",p-i);
}
printf("\n");
}
return 0;
}
pattern 1010101010
ReplyDelete8888
666
44
2 can any one send the program
5
ReplyDelete66
777
8888
8888
777
66
5
#include
int main() {
//code
int a[10],b[10],i,j,k=5,c;
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
if(j<=i)
{
printf("%d",k);
}
else
printf(" ");
}
printf("\n");k++;
}
c=k-1;
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
if(i>=1&&j<=5-i)
{
printf("%d",c);
}
else
printf(" ");
}
printf("\n");c--;
}
}