0% found this document useful (0 votes)
12 views27 pages

C# Programming Basics Tutorial

The document provides 12 coding tutorials with examples of C# programs to print "Hello World", create a profile page by taking user input, determine if a number is odd or even, calculate factorials, add three numbers by calling a method, convert a string to uppercase, toggle case of a string, mask the last 5 digits of a phone number, get user input for name and gender and print with honorific, and print the input name. The tutorials cover basic concepts of input/output, conditional statements, methods, string manipulation and provide code snippets with explanations.

Uploaded by

Abhay Shukla
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)
12 views27 pages

C# Programming Basics Tutorial

The document provides 12 coding tutorials with examples of C# programs to print "Hello World", create a profile page by taking user input, determine if a number is odd or even, calculate factorials, add three numbers by calling a method, convert a string to uppercase, toggle case of a string, mask the last 5 digits of a phone number, get user input for name and gender and print with honorific, and print the input name. The tutorials cover basic concepts of input/output, conditional statements, methods, string manipulation and provide code snippets with explanations.

Uploaded by

Abhay Shukla
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

Tutorial – 1

1. Write a program to print “Hello World”.

Code:

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
[Link]("ABHAY SHUKLA");
[Link]("hello world!!");
[Link]();
}
}
}

Output:

[Link] your profile page as given below.


$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

Name: Ramesh Tamkuity


DOB: 15/10/1991
Address: 4, xyx society, Kalawad Road
City: Rajkot
Pincode: 360 001
State: Gujarat
Country: India
Email: abc@[Link]
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
[Link]("ABHAY");
[Link]("Enter Your Name : ");
string name = [Link]();

[Link]("Enter Your DOB : ");


string DOB = ([Link]());

[Link]("Enter Your Adress : ");


string adress = [Link]();

[Link]("Enter Your Pincode : ");


int pincode = Convert.ToInt32([Link]());

[Link]("Enter Your State : ");


string state = [Link]();

[Link]("Enter Your Country : ");


string country = [Link]();
[Link]("Enter Your Email Adress : ");
string email = [Link]();

[Link]("Name : " + name);


[Link]("DOB : " + DOB);
[Link]("Adress : " + adress);
[Link]("Pincode : " + pincode);
[Link]("State : " + state);
[Link]("Country : " + country);
[Link]("Email : " + email);
[Link]();
}
}
}

Output:
[Link] out whether the given number is odd or even
Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
[Link]("Abhay");
[Link]("Enter One Number : ");
int num =Convert.ToInt32([Link]());
if (num%2 == 0 )
{
[Link]("Number is Even");
}
else
{
[Link]("Number is odd");
}
[Link]();
}
}
}

Output:

4. Rearrange the given code to correct the program. The resultant program will be to
input a number and print whether the given number is odd or even.

Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
[Link]("Abhay");
int num;
[Link]("Enter One Number : ");
num =Convert.ToInt32([Link]());
if (num%2 == 0 )
{
[Link]("Number is Even");
}
else
{
[Link]("Number is odd");
}
[Link]();
}
}
}

Output:
5. Write output of the program. Also write comment for each line for the following
code.

Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
[Link]("Abhay");
int n, fact = 1;

[Link]("Enter Number : ");

string str = [Link](); // taking input from user

n = Convert.ToInt32(str); // convert sring to intrger

for (int i = 1; i <= n; i++)

fact = fact * i; //claculate the fectorial of given number


}

[Link]("Factorial : {0}", fact); // giving output

[Link]();
}
}
}

Output:

6. Write missing statement to get the desired output.


Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2
{
internal class Program
{
static int Sum(int x, int y, int z)

{
[Link]("Abhay");

int res;

res = x + y + z;

return res;
}

static void Main(string[] args)


{
[Link]("Enter Number 1: ");

int a = Convert.ToInt32([Link]());

[Link]("Enter Number 2 : ");

int b = Convert.ToInt32([Link]());

[Link]("Enter Number 3 : ");

int c = Convert.ToInt32([Link]());

int result = Sum(a, b, c);

[Link](result);

[Link]();

Output:

7. Predict and write the output of the given code.


Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2
{
internal class Program
{

static void Main(string[] args)


{
[Link]("Abhay");
int num1, res, i;

[Link]("Enter a number");
num1 = Convert.ToInt32([Link]());

i = 1; //Initialization

//Check whether condition matches or not


while (i <= 10)
{
res = num1 * i;
[Link]("{0} x {1} = {2}", num1, i, res);

i++; //Increment by one


}
[Link]();
}

}
}

Output:
8. Write a program to convert given name in upper characters.
Code:

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2
{
internal class Program
{

static void Main(string[] args)


{
[Link]("Abhay");
[Link]("Enter One String : ");
string st = [Link]();
st = [Link]();
[Link](st);
[Link]();
}

Output:

9. Write a Program to convert given name in toggle case


Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2
{
internal class Program
{

static void Main(string[] args)


{
[Link]("Abhay");
[Link]("Enter One String : ");
string st = [Link]();
st = [Link]();
[Link](st);
[Link]();
}

Output:

10. Write a Program which accepts mobile no as a string from the user and converts the
last 5 digits into X.
Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2

{
internal class Program
{

static void Main(string[] args)


{
[Link]("Abhay");
[Link]("Enter your mobile number : ");
string mobileNumber = [Link]();
int lastIndex = [Link] - 5;
string lastFiveDigits = [Link](lastIndex);
string newMobileNumber = [Link](lastFiveDigits, "XXXXX");
[Link](newMobileNumber);
[Link]();
}
}
}

Output:

11. Write a Program which accepts name and gender from the user. Here, gender may
have only 1 character, M or F.
Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2

{
internal class Program
{

static void Main(string[] args)


{
[Link]("Abhay");
[Link]("Enter your name : ");
string name = [Link]();
[Link]("Enter your gender (M/F) : ");
string gender = [Link]();
if (gender == "M")
{
[Link]("Mr. " + name);
}
else if (gender == "F")
{
[Link]("Ms. " + name);
}
else
{
[Link]("invelid choice");
}
[Link]();
}
}
}

Output:

12. Write a Program which accepts name from the user and prints the same
Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2

{
internal class Program
{

static void Main(string[] args)


{
[Link]("Abhay");
[Link]("Enter your name : ");
string name = [Link]();
[Link](name);
[Link]();
}
}

Output:

13. Write a Program to prints the following series


0 1 1 2 3 5 8 13 21 34 55
Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2

{
internal class Program
{

static void Main(string[] args)


{
[Link]("Abhay");
int n1 = 0, n2 = 1, n3;
[Link]("0,1");
for (int i = 2; i < 11; i++)
{
n3 = n1 + n2;
[Link](",{0}", n3);
n1 = n2;
n2 = n3;
}
[Link]();
}
}
}
Output:

[Link] a Program which accepts no from the user and print the same in words.
INPUT : 98732
OUTPUT: Nine Eight Seven Three Two
Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2

{
internal class Program
{
private static string[] digits = { "zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine" };
static void Main(string[] args)
{
[Link]("Abhay");
int number;
[Link]("Enter a number: ");
number = [Link]([Link]());

string words = "";


while (number > 0)
{
int digit = number % 10;
words = digits[digit] + " " + words;
number = number / 10;
}

[Link]("The number in words is: " + words);


[Link]();
}
}
}
Output:

15. Write a Program to check whether the given no is Armstrong no or not.


Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2

{
internal class Program
{
static void Main(string[] args)
{
[Link]("Abhay");
int number, temp, sum = 0;
[Link]("Enter a number: ");
number = [Link]([Link]());

temp = number;
while (temp > 0)
{
int digit = temp % 10;
sum += digit * digit * digit;
temp = temp / 10;
}

if (number == sum)
{
[Link](number + " is an Armstrong number.");
}
else
{
[Link](number + " is not an Armstrong number.");
}
[Link]();
}
}
}

Output:
[Link] a program to display a pattern like a right angle triangle using an asterisk
The pattern like :
*
**
***
****
Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2

{
internal class Program
{
static void Main(string[] args)
{
[Link]("Abhay");
int rows;
[Link]("Enter the number of rows: ");
rows = [Link]([Link]());

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j <= i; j++)
{
[Link]("*");
}
[Link]();
}
[Link]();
}
}
}

Output:
17. Write a Program to generate following output.
1
12
123
1234
Code:

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2

{
internal class Program
{
static void Main(string[] args)
{
[Link]("Abhay");
int rows;
[Link]("Enter the number of rows: ");
rows = [Link]([Link]());

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j <= i; j++)
{
[Link](j);
}
[Link]();
}
[Link]();
}
}
}
Output:

18. Write a program to make such a pattern like a right angle triangle with the number
increased by 1.
1
23
456
7 8 9 10

Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace ConsoleApp2

{
internal class Program
{
static void Main(string[] args)
{
[Link]("Abhay");
[Link]("Enter the number of row : ");
int rows = [Link]([Link]());
int number = 1;

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j <= i; j++)
{
[Link]("{0} ", number++);
}

[Link]();
}
[Link]();
}
}
}

Output:
19. Write a program to make such a pattern as a pyramid with an asterisk.
*
**
***
****

Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2

{
internal class Program
{
static void Main(string[] args)
{
[Link]("Abhay");
[Link]("Enter the number of row : ");
int rows = [Link]([Link]());

for (int i = 1; i <= rows; i++)


{
for (int j = rows - i; j >= 1; j--)
{
[Link](" ");
}

for (int j = 1; j <= 2 * i - 1; j++)


{
[Link]("*");
}

[Link]();
}
[Link]();
}
}
}

Output:

20. Write a program to make a pyramid pattern with numbers increased by 1.


1
23
456
7 8 9 10
Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2

{
internal class Program
{
static void Main(string[] args)
{
[Link]("Abhay");
[Link]("Enter the number of row : ");
int rows = [Link]([Link]());

for (int i = 1; i <= rows; i++)


{
for (int j = rows - i; j >= 1; j--)
{
[Link](" ");
}

for (int j = 1; j <= 2 * i - 1; j++)


{
[Link](j);
}

[Link]();
}
[Link]();
}
}
}

Output:

21. Write a program to find the sum of the series 5 +55 + 555 + 5555 + .. n terms.
Test Data :
Input the number of terms : 4
Input number : 5
Expected Output :
5 + 55 + 555 + 5555
The Sum is : 6170
Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2

{
internal class Program
{
static void Main(string[] args)
[Link]("Abhay");
[Link]("enter the digit : ");
int n =Convert.ToInt32([Link]());
[Link]();
int m = n;
int sum = 0;
for (int i = 0; i < n; i++)
{
[Link](m+" + ");
sum=sum+m;
m = m * 10 + n;

}
[Link]();
[Link]("the sum = " + sum);
[Link]();
}
}
}

22. Write a program to display a pattern like a diamond.


*
***
*****
*******
*********
*******
*****
***
*
Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace ConsoleApp2

{
internal class Program
{
static void Main(string[] args)
{
[Link]("abhay");
[Link]("Enter number of row : ");

int rows = [Link]([Link]());


[Link]();
for (int i = 1; i <= rows; i++)
{
for (int j = rows - i; j >= 1; j--)
{
[Link](" ");
}

for (int j = 1; j <= 2 * i - 1; j++)


{
[Link]("*");
}

[Link]();
}

for (int i = rows - 1; i >= 1; i--)


{
for (int j = rows - i; j >= 1; j--)
{
[Link](" ");
}

for (int j = 1; j <= 2 * i - 1; j++)


{
[Link]("*");
}

[Link]();
}

[Link]();
}
}
}

Output:

Common questions

Powered by AI

Simple console applications are beneficial for learning programming as they offer a straightforward environment to understand basic concepts such as control structures, data types, and logic implementation without GUI complexities. However, limitations include the lack of exposure to modern user interface development, event-driven programming, and interactions that are prevalent in real-world applications, potentially narrowing the practical knowledge base if not complemented by GUI-based projects .

Input sanitization is crucial to prevent errors and potential security vulnerabilities such as code injection attacks. In the given examples, programs usually read user input and convert it to different types (int, string), often without initial validation checks. Without sanitization, this could lead to exceptions or unexpected behavior if malicious input is provided, underlining the importance of validating and sanitizing input prior to processing .

Recursion can be applied to generate complex patterns by repeatedly breaking down a problem into simpler subproblems, such as fractals or mathematical series. Influencing factors include the elegance and simplicity of the recursion for the problem, the natural divisibility of the problem into subcomponents, and potential performance or stack size limitations. While recursive solutions are often cleaner, they can impose higher memory overhead compared to iterative loops .

The iterative approach to calculating factorial loops through numbers from 1 to n and accumulates the product, as seen in the provided code . It uses a simple loop and maintains a single recursive stack frame, making it more memory efficient than the recursive approach, which involves stack memory for each recursive call. Recursion offers a cleaner, more logical flow but can be less memory efficient for large inputs due to potential stack overflow issues.

String case conversion in programming is handled using methods that transform each character in a string to its upper or lower case equivalent. In C#, ToUpper() and ToLower() methods of the string class perform these conversions. These methods work by referencing character encoding to convert cases, reducing the need for manual character handling, and ensuring uniform conversions efficiently .

Looping constructs like for loops are essential for iterating a fixed number of times, making them ideal for generating number sequences and patterns. The given code examples use nested for loops, where the outer loop handles the number of iterations or rows, and the inner loop generates elements within each row. This structure is key in patterns that depend on a sequence of calculations and allows systematic and organized traversal and manipulation of data .

The use of Convert.ToInt32 for reading numeric input can lead to exceptions if the input cannot be converted to an integer, such as when the input is not a valid number. Unlike int.TryParse, Convert.ToInt32 does not handle conversion failures gracefully, and will throw a FormatException if the input is invalid, highlighting a lack of input validation .

The code checks if a number is even or odd by using the modulus operator (%) to determine the remainder when the number is divided by 2. If the remainder is 0, the number is even; otherwise, it is odd. This is because even numbers are divisible by 2 without any remainder, making the modulus operation a suitable choice for this check .

Pattern programs are highly effective for teaching loops and iteration as they provide visual feedback that helps students understand how iterations control the flow of a program. By interpreting how different patterns emerge from loop changes, students gain a hands-on understanding of nest loops. However, the approach might be less effective if not backed with context, potentially not demonstrating the practical applications of loops beyond pattern generation .

Developing a program that converts numbers into words requires understanding of digit manipulation, string operations, and possibly data structures to hold string equivalents of digits. Practical application involves iterating over digits, mapping each one to its word equivalent, and assembling these into a coherent string. This concept is applicable in areas such as speech synthesis, automated report generation, and voice-controlled interfaces .

You might also like