0% found this document useful (0 votes)
27 views4 pages

1-C# Basics-Đã M Khóa

The document provides solutions to C# programming problems including accepting user input, returning the day of the week for a number, calling methods, displaying multiples of 5, and listing prime numbers.

Uploaded by

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

1-C# Basics-Đã M Khóa

The document provides solutions to C# programming problems including accepting user input, returning the day of the week for a number, calling methods, displaying multiples of 5, and listing prime numbers.

Uploaded by

hiepprook9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C# Basics

1. Write a program to accept a name and display the same.

Solution:

using System;

class TestInput
{
static void Main()
{
string strName;
Console.WriteLine("Enter Your Name");
strName=Console.ReadLine();
Console.WriteLine("Your Name is {0}", strName);
}
}

2. Write a program that accepts a number between 1 and 7 from the user and returns the
corresponding day of a week. (1 - Monday, 2 -Tuesday and so on)

Solution:

using System;

class DaysOfWeek
{
static void Main()
{
string strDow;
Console.WriteLine("Enter a number between 1 and 7 :");
strDow = Console.ReadLine();
switch(strDow)
{
case "1":
Console.WriteLine("First day of week is Sunday");
break;
case "2":
Console.WriteLine("Second day of week is Monday");
break;
case "3":
Console.WriteLine("Third day of week is Tuesday");
break;
case "4":
Console.WriteLine("Fourth day of week is Wednesday");
break;
case "5":
Console.WriteLine("Fifth day of week is Thursday");

1
break;
case "6":
Console.WriteLine("Sixth day of week is Friday");
break;
case "7":
Console.WriteLine("Seventh day of week is Saturday");
break;
default:
Console.WriteLine("Enter a number between 1 and 7");
break;
}
}
}

3. Write a program that calls a method to find the square of 10.

Solution:

using System;

class CalcSqr
{
static void Main()
{
int intNum = 10;
funcSqr(intNum);
Console.ReadLine();
}
static void funcSqr(int intNum)
{
int intsqr;
intsqr = intNum * intNum;
Console.WriteLine("Square of the number 10 is {0}",
intsqr);
}
}

4. Write a program to display the first 10 multiples of 5.

Solution:

using System;

class TestLoop
{
static void Main()
{
int intRes, intCnt = 1;
while (intCnt <= 10)
{

2
intRes = intCnt * 5;
Console.WriteLine("{0}",intRes);
intCnt = intCnt+1;
}
}
}

5. Write a program to list the first 10 prime numbers.

Solution:

using System;

class PrimeNumbers
{
static void Main()
{
int intNum = 1, intCnt, intNumHalf = 0, intI = 0;
bool IsPrime = true;
Console.WriteLine("The First 10 Prime Numbers are:");
while (intI < 10)
{
intNum += 1;
intNumHalf = (intNum / 2);
intCnt = 2;
while (intNumHalf >= intCnt)
{
if ((intNum % intCnt) == 0)
{
IsPrime = false;
break;
}
intCnt = intCnt+1;
}
if (IsPrime == true)
{
intI++;
Console.WriteLine("{0}",intNum);
}
else
IsPrime = true;
}
Console.ReadLine();
}
}

3
DO IT YOURSELF

1. Write a program to accept a number and display whether it is odd or even.

2. Write a program to accept a character as input from the user. If the letter input is any one out
of “a”, “e”, “i”, “o”, or “u” then display a message “You have input, Vowel” else display
“This is not a Vowel”.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy