- 8
Write a program in C++, if n =3 it will print 333 313 323 333, if n= 4 it will print 4444 4414 4424 4434 4444
Pattern programming
6 Respuestas
+ 1
#include<iostream>
using namespace std;
int main() {
int i, j, c = 1, n, mid;
cout << "enter size";
cin >> n;
for (i = 1; i < n + 2; i++) {
mid = 1 + n / 2;
for (j = 1; j < mid; j++) {
cout << n;
}
for (j = mid; j <= mid; j++) {
if (i == 1)
cout << n;
else {
cout << c;
c++;
}
}
for (j = mid; j < n; j++) {
cout << n;
}
cout << endl;
}
return 0;
}
0
Yerucham your logic is totally wrong...
0
what is the pattern for n=4??
0
C++
//for N=3
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 1;i <= 1;i++)
{
cout << n << n << n <<"\n";
break;
}
for (int i = 1;i <= n;i++)
{
cout << n << i << n << "\n";
}
return 0;
}
//For N=5
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 1;i <= 1;i++)
{
cout << n << n <<n << n << n <<"\n";
break;
}
for (int i = 1;i <= n;i++)
{
cout << n << n << i << n << n << "\n";
}
return 0;
}
- 1
If you input number of column as num=3,then the pattern will be look like this type-
333
313
323
333
If you input number of column as num=5,then the pattern will be look like this type-
55555
55155
55255
55355
55455
55555
The simple c code is-
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,num,n=1;
printf("Enter the number of column:");
scanf("%d",&num);
for(i=1;i<=num+1;i++)
{
for(j=1;j<=num;j++)
{
if(i>=2 && i<=num && j==(num+1)/2)
{
printf("%d",n);
n++;
}
else
printf("%d",num);
}
printf("\n");
}
getch();
return 0;
}
o/p:
Enter the number of column:9
999999999
999919999
999929999
999939999
999949999
999959999
999969999
999979999
999989999
999999999