C# program to print name and age:
In this program, we will ask the user to enter the name and age, these values will be stored in two variables and the program will print them on the console. We will also learn how to use a class and methods to read and store these values.
Method 1: C# program to read the name, age and print these on console:
The below program asks the user to enter the name and age. It stores these values in two variables and prints these to the console.
using System;
namespace Program
{
class Program
{
static void Main(string[] args)
{
int Age;
string Name;
Console.WriteLine("Enter your name: ");
Name = Console.ReadLine();
Console.WriteLine("Enter your Age: ");
Age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nYou have entered:\nName: " + Name + "\nAge: " + Age);
}
}
}
- age is an integer variable and name is a string variable to hold the age and name of the user.
- Console.WriteLine is used to enter a message to the console. Console.ReadLine is used to read the user input. It returns a string. So, we are using Convert.ToInt32 method to convert it to an integer for age.
- The last line is printing the name and age values.
If you run this program, it will print output as like below:
Enter your name:
Alex
Enter your Age:
20
You have entered:
Name: Alex
Age: 20
Method 2: Read and print the name and age of a user using a separate method:
We can use a separate method to read and print the name and age values.
using System;
namespace Program
{
class Program
{
private static int Age;
private static string Name;
static void ReadUserData()
{
Console.WriteLine("Enter your name: ");
Name = Console.ReadLine();
Console.WriteLine("Enter your Age: ");
Age = Convert.ToInt32(Console.ReadLine());
}
static void PrintUserData()
{
Console.WriteLine("\nYou have entered:\nName: " + Name + "\nAge: " + Age);
}
static void Main(string[] args)
{
ReadUserData();
PrintUserData();
}
}
}
Here, we created two methods ReadUserData and PrintUserData. The ReadUserData method is used to read the age and name values and the PrintUserData method prints these values.
It will give similar output. We have to create all values static as we are calling these methods from the static Main method. We can also create a different class with non-static methods.
Method 3: Read and print the name and age of a user using a separate class:
Instead of using a separate method, we can also use a separate class. We can create objects of this class from anywhere we want and call its methods to read the name and age of a user or print these values.
using System;
namespace Program
{
class User
{
private int Age;
private string Name;
public void ReadUserData()
{
Console.WriteLine("Enter your name: ");
Name = Console.ReadLine();
Console.WriteLine("Enter your Age: ");
Age = Convert.ToInt32(Console.ReadLine());
}
public void PrintUserData()
{
Console.WriteLine("\nYou have entered:\nName: " + Name + "\nAge: " + Age);
}
}
class Program
{
static void Main(string[] args)
{
User user = new User();
user.ReadUserData();
user.PrintUserData();
}
}
}
Here,
- User is a different class and this class holds all user properties i.e. age and name.
- It has two public methods, ReadUserData and PrintUserData.
- We created one object user and calling these methods to read the user name, age and to print these values.
It will give similar result. The advantage of this method is that you can create different objects of the User class and call these methods on each of these objects. Each object will hold the data separately.
You might also like:
- C# program to remove the last n characters from a string
- C# string.ToLower method explanation with example
- C# String.ToUpper method explanation with example
- C# program to create an array of objects
- 4 different C# programs to convert a decimal number to binary
- C# program to convert a binary value to decimal
- C# program to print the result of the division of two numbers