- Even or odd program in C#
Description:
Here in this program, The conditional statement (If statement)is used with the condition (i.e) The remainder value is zero or non zero when dividing the given value by 2, Conslole.WriteLine is used to display the given value is even or odd and Console.ReadLine is used to get the input value.
Sample Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace codeforevenorodd
{
class Program
{
static void Main (string[] args)
{
string g;
int j = 0;
int i;
Console.WriteLine(" Enter the value: ");
g = Console.ReadLine();
i = Convert.ToInt32(g);
j = (i % 2);
if(j == 0)
{
Console.WriteLine("The given value is even ");
}
else
{
Console.WriteLine("The given value is odd ");
}
}
}
}
Sample Input_1:
Enter the value:
88
Sample Output: The given value is even
Sample Input_2:
Enter the value:
23
Sample Output: The given value is odd
A sample program in C# is used to find the given value is palindrome or not both for string value and numbers.
Description:
In this program, either string or number is taken as a input for checking palindrome.
Palindrome is a specific name given for the word or number which shows the same name or number when reversed from right to left or vice-versa. While loop is used to check the given condition is palindrome or not. Conslole.WriteLine is used to display the given value is palindrome or not and Console.ReadLine is used to get the input value.
Sample Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace codeforpalindrome
{
class Program
{
static void Main (string[] args)
{
string s1;
int j = 0;
int i = 0;
Console.WriteLine(" Enter the value ");
s1 = Console.ReadLine();
j = s1.Length;
while (i != (j - 1))
{
if (s1[i] == s1[j - 1])
{
i++;
j = j - 1;
}
else
{
Console.WriteLine(" The given value is not a palindrome");
break;
}
}
if (i == (j-1))
{
Console.WriteLine("The given value is a plaindrome");
}
}
}
}
Sample Input_1:
Enter the value:
malayalam
Sample Output: The given value is a palindrome.
Sample Input_2:
Enter the value:
sir
Sample Output: The given value is not a palindrome.
Sample Input_3:
Enter the value:
12321
Sample Output: The given value is a palindrome.
Sample Input_4:
Enter the value:
12345
Sample Output: The given value is not a palindrome.
Matrix representation –program in C#
The matrix representation program is used to display the given number of rows and columns in a matrix format.
Description:
In this program, number of rows and columns are taken as input. For loop is uesd to diplay the output in a matrix format with each values in a rows and columns as a specific character(*). Try-catch block (Exception) is used to handle errors.
Sample Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace codeformatrix
{
class Program
{
static void Main (string[] args)
{
try
{
string i;
string j;
int m;
int n;
int no_of_rows = 0;
int no_of_columns = 0;
Console.WriteLine("Enter the number of rows ");
i = Console.ReadLine();
no_of_rows = Convert.ToInt32(i);
Console.WriteLine("Enter the number of columns ");
j = Console.ReadLine();
no_of_columns = Convert.ToInt32(j);
for (n = 0; n < no_of_rows; n++)
{
for (m = 0; m < no_of_columns; m++)
{
Console.Write(" * ");
}
Console.WriteLine(" \n " );
}
}
catch (Exception e)
{
Console.WriteLine("Exception occured: " + e.Message);
}
}
}
}
Sample Input :
Enter the number of rows: 3
Enter the number of columns: 3
Sample Output:
The matrix format for given number of rows and columns is
* * *
* * *
* * *
Triangle representation –program in C#
A sample program in C# is uesd to display the given charcter in a trianglular format.
Description:
This triangle representation program is used to diplay the given character in a triangular format. In this program, no of rows is taken as input and For loop is used to represent the given character in a triangle format. Conslole.WriteLine is used to display the given character and Console.ReadLine is used to get the input (no_of_rows) value.
Sample code:
using System;
using System.Collections.Generic;
using System.Text;
namespace codefortriangleformat
{
class Program
{
static void Main (string[] args)
{
string j;
int k = 0;
int m = 1;
int i = 1;
int y = 0;
int a = 0;
int no_of_rows = 0;
Console.WriteLine(" Enter the number of rows ");
j = Console.ReadLine();
no_of_rows = Convert.ToInt32(j);
a = (2*no_of_rows) - 1;
y = (a-1)/2;
for (i = 1; i <= no_of_rows; i++)
{
for (a = 0; a < y; a++)
{
Console.Write(" ");
Console.Write(" ");
}
for (k = 0; k < m; k++)
{
Console.Write(" * ");
}
Console.WriteLine(" \n");
m = m + 2;
y--;
}
}
}
}
Sample Input :
Enter the number of rows: 5
Sample Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
No comments:
Post a Comment
Please do not enter any Spam link in the Comment box