0% found this document useful (0 votes)
2 views6 pages

CSharp Console Programs-1

The document contains C# console application programs that demonstrate various functionalities, including calculating the area of a square, counting digits and alphabets in a string, incrementing characters and toggling their case, performing string manipulations, demonstrating pre-increment and post-increment assignments, implementing a login system with attempts, and finding the position of a digit in a number. Each program includes code snippets and sample outputs for user interaction. These examples serve as practical exercises for learning C# programming concepts.

Uploaded by

batmanavx661
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

CSharp Console Programs-1

The document contains C# console application programs that demonstrate various functionalities, including calculating the area of a square, counting digits and alphabets in a string, incrementing characters and toggling their case, performing string manipulations, demonstrating pre-increment and post-increment assignments, implementing a login system with attempts, and finding the position of a digit in a number. Each program includes code snippets and sample outputs for user interaction. These examples serve as practical exercises for learning C# programming concepts.

Uploaded by

batmanavx661
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C# Console Application Programs

1. Area of a Square

using System;

class Program
{
static void Main(string[] args)
{
[Link]("Enter the side of the square: ");
double side = [Link]([Link]());

double area = side * side;

[Link]("Area of the square = " + area);


}
}

Sample Output:

Enter the side of the square: 5


Area of the square = 25

2. Count Digits and Alphabets in a String

using System;

class Program
{
static void Main(string[] args)
{
[Link]("Enter a string: ");
string input = [Link]();

int digitCount = 0;
int alphabetCount = 0;

foreach (char c in input)


{
if ([Link](c))
digitCount++;
else if ([Link](c))
alphabetCount++;
}

[Link]("Number of digits: " + digitCount);


[Link]("Number of alphabets: " + alphabetCount);
}
}
Sample Output:

Enter a string: Hello123World


Number of digits: 3
Number of alphabets: 10

3. Increment Each Character by 1 and Toggle Case

using System;
using [Link];

class Program
{
static void Main(string[] args)
{
[Link]("Enter a string: ");
string input = [Link]();

StringBuilder incremented = new StringBuilder();


foreach (char c in input)
{
[Link]((char)(c + 1));
}

StringBuilder toggled = new StringBuilder();


foreach (char c in [Link]())
{
if ([Link](c))
[Link]([Link](c));
else if ([Link](c))
[Link]([Link](c));
else
[Link](c);
}

[Link]("Incremented string: " + [Link]());


[Link]("Toggled case string: " + [Link]());
}
}

Sample Output:

Enter a string: abcXYZ


Incremented string: bcdYZ[
Toggled case string: BCDyz[

4. String Manipulations (Reverse, Substring, Replace, Copy)


using System;

class Program
{
static void Main(string[] args)
{
[Link]("Enter a string: ");
string input = [Link]();

// 1) Print the string in reverse order


char[] charArray = [Link]();
[Link](charArray);
string reversed = new string(charArray);
[Link]("Reversed string: " + reversed);

// 2) Extract part of the string from 2nd position till the end
string extracted = [Link] >= 2 ? [Link](1) : "";
[Link]("Substring from 2nd position: " + extracted);

// 3) Replace any given character by '$' and print the new string
[Link]("Enter character to replace: ");
char toReplace = [Link]([Link]());
string replaced = [Link](toReplace, '$');
[Link]("String after replacement: " + replaced);

// 4) Copy the string to another variable, modify it, print both


string copyString = input;
copyString = copyString + " (modified)";
[Link]("Original string: " + input);
[Link]("Copied and modified string: " + copyString);
}
}

Sample Output:

Enter a string: hello


Reversed string: olleh
Substring from 2nd position: ello
Enter character to replace: l
String after replacement: he$$o
Original string: hello
Copied and modified string: hello (modified)

5. Pre-Increment vs Post-Increment Assignment and Swap

using System;

class Program
{
static void Main(string[] args)
{
[Link]("Enter num1: ");
int num1 = Convert.ToInt32([Link]());
[Link]("Enter num2: ");
int num2 = Convert.ToInt32([Link]());

// 1) Assign num1 to num2 by pre-incrementing num1


int a = num1;
int b = num2;
b = ++a;
[Link]("Pre-increment: num1 = " + a + ", num2 = " + b);

// 2) Assign num1 to num2 by post-incrementing num1


int c = num1;
int d = num2;
d = c++;
[Link]("Post-increment: num1 = " + c + ", num2 = " + d);

// 3) Swap both values


int x = num1;
int y = num2;
int temp = x;
x = y;
y = temp;
[Link]("After swap: num1 = " + x + ", num2 = " + y);
}
}

Sample Output:

Enter num1: 5
Enter num2: 10
Pre-increment: num1 = 6, num2 = 6
Post-increment: num1 = 6, num2 = 5
After swap: num1 = 10, num2 = 5

6. Login with 3 Attempts

using System;

class Program
{
static void Main(string[] args)
{
string correctLogin = "admin";
string correctPassword = "pass123";

int attempts = 0;
bool accessGranted = false;

while (attempts < 3)


{
[Link]("Enter login: ");
string login = [Link]();
[Link]("Enter password: ");
string password = [Link]();

if (login == correctLogin && password == correctPassword)


{
accessGranted = true;
break;
}
else
{
attempts++;
[Link]("Incorrect login or password. Attempts
left: " + (3 - attempts));
}
}

if (accessGranted)
[Link]("Login successful. Welcome!");
else
[Link]("User rejected after 3 wrong attempts.");
}
}

Sample Output:

Enter login: admin


Enter password: wrongpass
Incorrect login or password. Attempts left: 2
Enter login: admin
Enter password: pass123
Login successful. Welcome!

7. Position of 2nd Number's Digit in 1st Number

using System;

class Program
{
static void Main(string[] args)
{
[Link]("Enter the first number: ");
int num1 = Convert.ToInt32([Link]());
[Link]("Enter the second number (single digit): ");
int num2 = Convert.ToInt32([Link]());

int position = 0;
int temp = num1;
bool found = false;

while (temp > 0)


{
position++;
int digit = temp % 10;

if (digit == num2)
{
found = true;
string placeName = position switch
{
1 => "unit's",
2 => "ten's",
3 => "hundred's",
4 => "thousand's",
_ => position + "th"
};
[Link](num2 + " is in the " + placeName + " place
of " + num1);
}

temp = temp / 10;


}

if (!found)
[Link](num2 + " is not present in " + num1);
}
}

Sample Output:

Enter the first number: 4523


Enter the second number (single digit): 5
5 is in the hundred's place of 4523

You might also like