C# Programming Examples of Variables and Data types

In this chapter you will learn:
  • How to use data types and variables in a program?

Qu 1: Write a program in which accepts user’s details as name, city, age and pin number. Then store all the values in the appropriate variable and then print all the information in correct format.

Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Variables_Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            string name;
            string city;
            sbyte age;
            int pin;

            // \n is used for line-break
            Console.WriteLine("Enter your name\n");
            name = Console.ReadLine();

            Console.WriteLine("Enter Your City\n");
            city = Console.ReadLine();

            Console.WriteLine("Enter your age\n");
            age = sbyte.Parse(Console.ReadLine());

            Console.WriteLine("Enter your pin\n");
            pin = Int32.Parse(Console.ReadLine());

            // Printing message to console
            //formatting output
            Console.WriteLine("==============");
            Console.WriteLine("Your Complete Address:");
            Console.WriteLine("============\n");

            Console.WriteLine("Name = {0}", name);
            Console.WriteLine("City = {0}", city);
            Console.WriteLine("Age = {0}", age);
            Console.WriteLine("Pin = {0}", pin);
            Console.WriteLine("===============");

            Console.ReadLine();
        }
    }
}

 

Output


Enter your name :   Steven Clark
Enter Your City :   California
Enter your age :   37
Enter your pin :  95012

=================================================
Your Complete Address:
=================================================

Name = Steven Clark
City = California
Age = 37
Pin = 95012
=================================================

__

Summary

In this chapter you learned about how to use data types in C# programming to create variable. In next chapter there are some programming exercises for you. It is recommended you to do all the Exercises honestly.

 

Share your thought