0% found this document useful (0 votes)
12 views

C# new

Uploaded by

marieta.nikolova
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 views

C# new

Uploaded by

marieta.nikolova
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
You are on page 1/ 5

* C# Program to Generate Random Numbers Console.

WriteLine("Square root of {0} is: {1}", Number,


SqrtNumber);
using System;
Console.ReadLine(); } } }
class Program
* C# Program to Find the Cube Root of a Given Number
{ static void Main()
using System;
{ Console.WriteLine("Some Random Numbers that are
generated are : "); class CubeRoot

for (int i = 1; i < 10; i++) { public static void Main()

{ Randfunc(); } } { double num, res;

static Random r = new Random(); Console.Write("Enter the Number : ");

static void Randfunc() num = double.Parse(Console.ReadLine());

{ int n = r.Next(); res = Math.Ceiling(Math.Pow(num, (double)1 / 3));

Console.WriteLine(n); Console.Write("Cube Root : " + res); } }

Console.ReadLine(); } } * C# Program to Calculate Power of Three

* C# Program to Find Square Root of a Given Number using System;

using System; class Program

using System.Text; { static void Main(string[] args)

using System.Collections; { new GeneratePowers().RaiseToPower

using System.Data; (5, // 4 values per table

namespace Cons 3);

{ public class squareroot Console.ReadLine();// power to raise each value } }

{ public static void Main() public class GeneratePowers

{ Console.WriteLine("Enter a Number : "); { public void RaiseToPower(int maxIterations, int power)

int Number = Convert.ToInt16(Console.ReadLine()); { Console.WriteLine("{0,8}{1,16}",

double SqrtNumber = Math.Sqrt(Number); "Number", "Power of " + power);


for (int i = 1; i <= maxIterations; ++i) * C# Program to get the Local Time

{ Console.Write("{0,5}{1,15}\n", i, using System;

Math.Pow(i, power)); } } } class Program

* C# Program to find Product of 2 Numbers using Recursion { static void Main()

using System; { TimeZone zone = TimeZone.CurrentTimeZone;

class program DateTime local = zone.ToLocalTime(DateTime.Now);

{ public static void Main() Console.WriteLine("The Local Time is : {0}",local);

{ int a, b, result; Console.ReadLine(); } }

Console.WriteLine("Enter two numbers to find their product: "); * C# Program to get the Universal Time

a = int.Parse(Console.ReadLine()); using System;

b = int.Parse(Console.ReadLine()); class Program

prog pg = new prog(); { static void Main()

result = pg.product(a, b); { TimeZone zone = TimeZone.CurrentTimeZone;

Console.WriteLine("Product of {0} and {1} is {2}",a, b, result); DateTime univ = zone.ToUniversalTime(DateTime.Now);

Console.ReadLine(); } } Console.WriteLine("Universal Time is {0}",univ);

class prog Console.Read(); } }

{ public int product(int a, int b)

{ if (a < b)

{ return product(b, a); }

else if (b != 0)

{ return (a + product(a, b - 1)); }

else

{ return 0; } } }
* C# Program to Convert a Given Number of Days in terms of { for (int k = 5; k > i; k--)

* Years, Weeks & Days { Console.Write(" "); }

using System; for (int j = 0; j < i; j++)

class program { if (j == 0 || i == j)

{ public static void Main() { arr[i, j] = 1; }

{ int ndays, year, week, days, DAYSINWEEK=7; else { arr[i, j] = arr[i - 1, j] + arr[i - 1, j - 1]; }

Console.WriteLine("Enter the number of days"); Console.Write(arr[i, j] + " "); }

ndays = int.Parse(Console.ReadLine()); Console.ReadLine(); } } }

year = ndays / 365; Runtime Test Cases

week = (ndays % 365) / DAYSINWEEK; Pascal Triangle :

1
days = (ndays % 365) % DAYSINWEEK;
11
Console.WriteLine("{0} is equivalent to {1}years, {2}weeks and
{3}days", ndays, year, week, days); 121

1331
Console.ReadLine(); }}
* C# Program to Print a BinaryTriangle
Program Explanation

In this C program, we are reading the number of days using ‘ndays’ integer variable. using System;
Convert the given number of days to a measure of time given in years, weeks and
days. For example 375 days is equal to 1 year, 1 week, and 3 days (ignore leap year). using System.Collections.Generic;

* C# Program to Illustrate Pascal Triangle using System.Linq;

using System; using System.Text;

class Pascal namespace Program

{ public static void Main() { class Program

{ int[,] arr = new int[8, 8]; { public static void Main(String[] args)

Console.WriteLine("Pascal Triangle : "); { int p, lastInt = 0, input;

for (int i = 0; i < 5; i++) Console.WriteLine("Enter the Number of Rows : ");


input = int.Parse(Console.ReadLine()); count = number - 1;

for (int i = 1; i <= input; i++) for (k = 1; k <= number; k++)

{ for (p = 1; p <= i; p++) { for (i = 1; i <= count; i++)

{ if (lastInt == 1) Console.Write(" ");

{ Console.Write("0"); count--;

lastInt = 0; } for (i = 1; i <= 2 * k - 1; i++)

else if (lastInt == 0) Console.Write("*");

{ Console.Write("1"); Console.WriteLine(); }

lastInt = 1; } } count = 1;
Console.Write("\n"); }
for (k = 1; k <= number - 1; k++)
Console.ReadLine(); } } }
{ for (i = 1; i <= count; i++)
Runtime Test Cases
Console.Write(" ");
Enter the Number of Rows : 5
count++;
1

01
for (i = 1; i <= 2 * (number - k) - 1; i++)

010 Console.Write("*");

1010 Console.WriteLine(); }
10101 Console.ReadLine(); } }
* C# Program to Print the Sum of all the Multiples of 3 and 5 Enter number of rows

using System; 3

class program *

***
{ public static void Main()
*****
{ int number, i, k, count = 1;
***
Console.Write("Enter number of rows\n");
*
number = int.Parse(Console.ReadLine());
* C# Program to Display Floyd's Triangle with an Numeric Mode
using System; class Pascal

class Program { public static void Main()

{ static void Main(string[] args) { int[,] arr = new int[8, 8];

{ int i, j, k = 1; for (int i = 0; i < 8; i++)

for (i = 1; i <= 10; i++) { for (int k = 7; k > i; k--)

{ for (j = 1; j < i + 1; j++) { //For loop to print spaces

{ Console.Write(k++ + " "); } Console.Write(" "); }

Console.Write("\n"); } for (int j = 0; j < i; j++)

Console.ReadLine(); } } { if (j == 0 || i == j)
Runtime Test Cases { arr[i, j] = 1; }
1 else { arr[i, j] = arr[i - 1, j] + arr[i - 1, j - 1]; }
23
Console.Write(arr[i, j] + " "); }
456
Console.WriteLine(); }
7 8 9 10
Console.ReadLine(); } }
11 12 13 14 15
Runtime Test Cases
16 17 18 19 20 21
1
22 23 24 25 26 27 28
11
29 30 31 32 33 34 35 36
121
37 38 39 40 41 42 43 44 45
1331
46 47 48 49 50 51 52 53 54 55
14641

1 5 10 10 5 1
* C# Program to Display Numbers in the form of Triangle
1 6 15 20 15 6 1
using System;

You might also like