Number Pattern Programs in C# - c#

I want to print something like this
54321
5432
543
54
5
4
3
2
1
int n = 5;
Console.WriteLine();
for (int i = n; i >= 0; i--)
{
for (int j = 1; j <= i; j++)
Console.Write(i.ToString());
Console.WriteLine();
}
Console.WriteLine();
Console.ReadLine();

I would solve this with while-loops
int i = 5, input = 0;
while (i > 0)
{
input *= 10;
input += i--;
}
while (input > 10)
{
Console.WriteLine(input);
input /= 10;
}
while (input > 0)
{
Console.WriteLine(input--);
}
https://dotnetfiddle.net/1L6j6A

Related

Magic Square Code Single Even number in C#

can you help me to create a logic for magic square metric. In given example, I have created a code for generate Magic Square for odd numbers like 3x3, 5x5, 7x7 metric and double even numbers like 4×4 , 8×8 but unable to found a proper solution for create single even value magic square metric like 6x6, 10x10 etc.
In current implementation anyone can enter a number (n) in input and it will create a nxn magic square metric. But not working fine with single even numbers
class Program
{
public static void Main(string [] args )
{
Console.WriteLine("Please enter a number:");
int n1 = int.Parse(Console.ReadLine());
// int[,] matrix = new int[n1, n1];
if (n1 <= 0)
{
Negativ();
}
else if (n1 == 2)
{
Zwei();
}
else if ((n1 != 2) && !(n1 < 0) && (n1 % 2 != 0))
{
Odd (n1 );
}
else if ((n1 != 2) && !(n1 < 0) && ((n1 - 2) % 4 == 0))
{//singl Even
SingleEven(n1);
}
else if ((n1 != 2) && !(n1 < 0) && (n1 % 4 == 0))
{
DoubleEven (n1);
}
}
private static void Negativ(){
Console.WriteLine("Sorry, the number must be positive and greater than 3 ");
Console.ReadLine();
}
public static void Zwei(){
Console.WriteLine("Sorry,there is no magic square of 2x2 and the number must be and greater than 3 ");
Console.ReadLine();
}
public static void Odd ( int n)// odd method
{
int[,] magicSquareOdd = new int[n, n];
int i;
int j;
// Initialize position for 1
i = n / 2;
j = n - 1;
// One by one put all values in magic square
for (int num = 1; num <= n * n; )
{
if (i == -1 && j == n) //3rd condition
{
j = n - 2;
i = 0;
}
else
{
//1st condition helper if next number
// goes to out of square's right side
if (j == n)
j = 0;
//1st condition helper if next number is
// goes to out of square's upper side
if (i < 0)
i = n - 1;
}
//2nd condition
if (magicSquareOdd[i, j] != 0)
{
j -= 2;
i++;
continue;
}
else
{
//set number
magicSquareOdd[i, j] = num++;
//1st condition
j++; i--;
}
}
// print magic square
Console.WriteLine("The Magic Square for " + n + " is : ");
Console.ReadLine();
for ( i = 0; i < n; i++)
{
for ( j = 0; j < n; j++)
Console.Write(" " + magicSquareOdd[i, j] + " ");
Console.WriteLine();
Console.ReadLine();
}
Console.WriteLine(" The sum of each row or column is : " + n * (n * n + 1) / 2 + "");
Console.ReadLine();
}
public static void SingleEven(int n )
{
// int n = magic .Length ;
int[,] magicSquareSingleEven = new int[n, n];
int halfN = n / 2;
int k = (n - 2) / 4;
int temp;
int[] swapcol = new int[n];
int index = 0;
int[,] minimagic = new int[halfN, halfN];
*Odd(minimagic) ;* // here is the problem
for (int i = 0; i < halfN; i++)
for (int j = 0; j < halfN; j++)
{
magicSquareSingleEven[i, j] = minimagic[i, j];
magicSquareSingleEven[i+ halfN , j+halfN ] = minimagic[i, j]+ halfN *halfN ;
magicSquareSingleEven[i, j + halfN] = minimagic[i, j] +2* halfN * halfN;
magicSquareSingleEven[i + halfN, j] = minimagic[i, j] +3* halfN * halfN;
}
for (int i =1; i< k ;i ++)
swapcol [index ++]=i ;
for (int i = n-k+2; i <= n ; i++)
swapcol[index++] = i;
for (int i =1; i<=halfN ;i ++)
for (int j = 1; j<= index ; j ++)
{
temp = magicSquareSingleEven[i - 1, swapcol[j - 1] - 1];
magicSquareSingleEven[i-1,swapcol[j-1]-1]=magicSquareSingleEven[i +halfN-1,swapcol[j-1]-1];
magicSquareSingleEven[i+halfN-1,swapcol[j-1]-1]=temp;
}
//swaping noses
temp=magicSquareSingleEven[k,0];
magicSquareSingleEven[k,0]=magicSquareSingleEven[k+halfN,0];
magicSquareSingleEven[k+halfN,0]=temp;
temp=magicSquareSingleEven[k+halfN,k];
magicSquareSingleEven[k+halfN,k]=magicSquareSingleEven[k,k];
magicSquareSingleEven[k,k]=temp;}
//end of swaping
// print magic square
Console.WriteLine("The Magic Square for " + n + " is : ");
Console.ReadLine();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
Console.Write(" " + magicSquareSingleEven[i, j] + " ");
Console.WriteLine();
Console.ReadLine();
}
Console.WriteLine(" The sum of each row or column is : " + n * (n * n + 1) / 2 + "");
Console.ReadLine();
}

Need help fixing placement of drawn paterns in console application

So the user has a choice of 3 possible outputs:
- Draw a triangle
- Draw a rectangle
- Draw a house
I can draw all three but the output is not quiet right.
As you see in the code it draws a triangle but I need it to move more to the right.
if (keuze == 1)
{
int n = 4;
int i, j, k = 0;
for (i = 1; i <= n; i++)
{
for (j = i; j < n; j++)
{
Console.Write(" ");
}
while (k != (2 * i - 1))
{
if (k == 0 || k == 2 * i - 2)
Console.Write("*");
else
Console.Write(" ");
k++;
;
}
k = 0;
Console.WriteLine();
}
for (i = 0; i < 2 * n - 1; i++)
{
Console.Write("*");
}
Console.WriteLine();
}
Try This:
if (true)
{
int n = 4;
int i, j, k = 0;
for (i = 1; i <= n; i++)
{
for (j = i; j < n; j++)
{
Console.Write(" ");
}
while (k != (2 * i - 1))
{
if (k == 0) Console.Write(" "); //Added
if (k == 0 || k == 2 * i - 2)
Console.Write("*");
else
Console.Write(" ");
k++;
}
k = 0;
Console.WriteLine();
}
Console.Write(" "); //Added
for (i = 0; i < 2 * n - 1; i++)
{
Console.Write("*");
}
Console.WriteLine();
}

C# Sum of digits of a number if the sum equals 9

i want all the numbers with 3 digits that have the sum of 9 to be write in the console.
This is what i came up so far and it doesnt work:
class Program
{
static void Main(string[] args)
{
int sum = 0;
for (int n = 100; n < 1000; n++)
{
while (n <1000)
{
sum += n % 10;
n /= 10;
if (sum == 9)
Console.WriteLine(sum);
}
}
}
}
I'd use three loops, one for every digit:
for (int i1 = 1; i1 < 10; i1++)
for (int i2 = 0; i2 < 10; i2++)
for (int i3 = 0; i3 < 10; i3++)
{
if (i1 + i2 + i3 == 9)
Console.WriteLine("{0}{1}{2}", i1, i2, i3);
}
You're not resetting sum after every loop iteration. sum should equal zero at the start of every iteration like. Also, the while loop is wrong. Try this:
for(int n=100;n<1000;n++)
{
sum=0;
int i = n;
while(i!=0) {
sum += i % 10;
i /= 10;
}
if (sum == 9)
Console.WriteLine("Number {0} has digit sum of {1}", n, sum);
}
Why soo commplicated?
for (int n = 100; n < 1000; n++)
{
var s1 = n/100 % 10;
var s2 = n/10 % 10;
var s3 = n/1 % 10;
var sum = s1+s2+s3;
if (sum == 9)
Console.WriteLine(n);
}
For people, who dont like easy way :D
Enumerable.Range(0, 1000).Select(x => x.ToString())
.Where(x => x.Length == 3).Select(x => new {x, sum=x.ToCharArray()
.Select(c=>int.Parse(c.ToString())).Sum()}).Where(x=>x.sum == 9)
.Select(x=>x.x).ToList().ForEach(Console.WriteLine);
Oki, tried to create non-generic but fastest solution.
for (var i = 1; i <= 10; i++)
for (var j = 0; j < 10; j++)
{
if ((i>1 && j == 0) || i < j)
{
Console.WriteLine(i * 90 + j * 9);
}
}
I think you are looking for this:
class Program
{
static void Main(string[] args)
{
for (int n = 100; n < 1000; n++)
{
int sum = 0;
int num = n;
while (num != 0)
{
int r = num % 10;
sum += r;
num /= 10;
}
if (sum == 9)
Console.WriteLine(n);//.....Number whose all digit sum ==9
}
}
}

Nested For Loop To Print Below Mentioned Pattern

I wanting to print
1
23
456
78910
in C# Console application, Can any one help in doing it for me
I am using this code which works fine for me but I want not to use if statement just want to show my result in nested loop
for (int i = 1; i <= 10; i++)
{
Console.Write(i.ToString());
if (i==1 || i == 3 || i == 6)
{
Console.WriteLine();
}
}
Try this This should print nummber in format as you mentioned
int len = 0;
int count = 1;
for (int i = 1; i < 11; i++)
{
count++;
if (count >= len)
{
len++;
count = 0;
Console.WriteLine("");
}
Console.Write(i);
}
int len = 1;
int count = 0;
for(int i = 0; i < 11 ; i++)
{
count++;
if(count > len)
{
len++;
count = 0;
Console.WriteLine("");
}
Console.Write(i);
}

infinite loop in C#

This simple program take an input of integers and print them , but stop printing if it sees 60
string input = string.Empty;
int intValue = 0;
int[] numbers = new int[5];
for (int i = 0; i < 4; i++)
{
input = Console.ReadLine();
if (int.TryParse(input, out intValue))
numbers[i] = intValue;
}
for (int i = 0; i < numbers.Length; i++)
{
while (numbers[i] != 60)
{
Console.WriteLine(intValue);
}
}
the program go on an infinite loop after the 4th input
like that
Input:
1
2
3
4
4
4
4
4
4 ........ and so on
and i don't know the reason .... ^_^
while (numbers[i] != 60)
{
Console.WriteLine(intValue);
}
should be:
if (numbers[i] != 60)
{
Console.WriteLine(intValue);
}
string input = string.Empty;
int intValue = 0;
int[] numbers = new int[5];
for (int i = 0; i < 4; i++)
{
input = Console.ReadLine();
if (int.TryParse(input, out intValue))
numbers[i] = intValue;
}
for (int i = 0; i < numbers.Length; i++)
{
/*while (numbers[i] != 60)*/
if (numbers[i] != 60) // it should be if condition, while statement made it infinite
{
Console.WriteLine(intValue);
}
}

Categories