I need to code a 1 22 333 4444 pattern in python with while loops
This is the idiomatic way to do it in Python, using the fact that a string can be "multiplied":
def EXwhile6 ():
a = input("Write how many lines you want to print: ")
a = int(a)
K = 1
while K <= a:
print(str(K) * K)
K += 1
Now, if you want to code it by hand you'd need a nested loop; the above is equivalent to:
def EXwhile6 ():
a = input("Write how many lines you want to print: ")
a = int(a)
K = 1
while K <= a:
J = 1
while J <= K:
print(K, end="")
J += 1
print()
K += 1
Python | addition of pattern 1 22 333 4444 55555 ....?
You generate a sequence of elements of the form str(i)*i
, which you cast into an integer, then sum
that sequence.
def create_sequence(n):
return sum(int(str(i)*i) for i in range(1, n+1))
create_sequence(int(input('Enter the number till which you want the sequence: ')))
How to create the pattern like 55555,4444,333,22,1 using loop structures in R
You can do :
n <- 5
for(x in n:1){
cat(rep(x, x), '\n')
}
#5 5 5 5 5
#4 4 4 4
#3 3 3
#2 2
#1
Another version without a loop :
n1 <- n:1
cat(paste0(strrep(n1, n1), collapse = '\n'))
How to print numbers in the following pattern with .NET
The problem is in the MakeALine
method. You actually concat the number with itself, so for input 1
you actually get "1" + "1"
.
Instead you should repeat the string representation of your number k
times. For this you can use Enumerable.Repeat
in the following way:
static string MakeALine(int k)
{
return String.Concat(Enumerable.Repeat(k.ToString(),k));
}
Creating number pattern with minimum for loops
here you are:
for (int i = 1, j = 1 ; i < 10 ; i++, j = (i <= 5) ? (j*10 + 1) : (j/10))
System.out.println(i * j);
How to make this numbers triangle using nested while loops? (python 2.7)
Do you require nested loops?
>>> n=5
>>> for i in range(1, n+1):
... print("{:>{width}}".format(str(i)*i, width=n))
1
22
333
4444
55555
But to fix your code - you are missing the multiplier on your number:
n=5
m=1
while n>=1:
while m<=5:
print " "*(n), str(m)*m
n=n-1
m=m+1
how to generate pattern 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
Beginners should help beginners :-)
Here a slightly different approach:
for (int i=0; i <= 5; i++)
{
for (int j=0; j<5 ; j++)
{
if (j >= i)
printf("%1d ", 5-i);
else
printf(" ");
}
printf("\n");
}
Related Topics
Make Alternate Letters Capital
How to Limit Iterations of a Loop in Python
Python Sockets Multiple Messages on Same Connection
Regular Expression for Double and Integer Validation
How to Populate New Column Based on Values in Other Columns
How to Expand Input Buffer Size of Pyserial
How to Get String Objects Instead of Unicode from Json
Executing Multiple Functions Simultaneously
How to Pivot on Multiple Columns in Spark SQL
Opencv Typeerror: Expected Cv::Umat for Argument 'Src' - What Is This
How to Smooth a Curve in the Right Way
How to Extract Data from Dictionary in the List
How to Use and Print the Pandas Dataframe Name