100% found this document useful (1 vote)
125 views6 pages

C# Program Internal Exam

The document contains code snippets for several algorithms: 1) A Fibonacci sequence generator that prints the first n numbers of the sequence. 2) Code to find the three largest numbers in an array of 5 integers. 3) A program that checks if a number is prime by counting its factors. 4) A program that checks if a string is a palindrome by reversing it and comparing. 5) Code to convert between Celsius and Fahrenheit temperatures. 6) Code to sort an array of numbers by printing elements in ascending order.

Uploaded by

thangam86
Copyright
© Attribution Non-Commercial (BY-NC)
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
100% found this document useful (1 vote)
125 views6 pages

C# Program Internal Exam

The document contains code snippets for several algorithms: 1) A Fibonacci sequence generator that prints the first n numbers of the sequence. 2) Code to find the three largest numbers in an array of 5 integers. 3) A program that checks if a number is prime by counting its factors. 4) A program that checks if a string is a palindrome by reversing it and comparing. 5) Code to convert between Celsius and Fahrenheit temperatures. 6) Code to sort an array of numbers by printing elements in ascending order.

Uploaded by

thangam86
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 6

Fibonnacci

static void Main(string[] args) { int a = 0; int b = 1; Console.WriteLine("Enter the value"); int n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(a); Console.WriteLine(b); for (int i = 2; i < n; i++) { int temp = a+b; Console.WriteLine(temp); a = b; b = temp; } }

Largest there among 5 numbers


class Program { static void Main(string[] args) { int i, largest1,largest2,largest3; int[] array = new int[5]; for (i = 0; i < 5; i++) { Console.WriteLine("Enter elements:{0}", i+1); array[i] = Convert.ToInt32(Console.ReadLine()); } largest1 = 0; for (i = 0; i <5; i++) { if (array[i] > largest1) { largest1 = array[i]; } } Console.WriteLine("The Largest number is :{0}", largest1); largest2 = 0; for (i = 0; i < 5; i++) { if (array[i] > largest2 && array[i] < largest1 ) { largest2 = array[i]; } } Console.WriteLine("The Largest number is :{0}", largest2); largest3 = 0; for (i = 0; i < 5; i++) { if (array[i] > largest3 && array[i] < largest2) { largest3 = array[i]; } } Console.WriteLine("The Largest number is :{0}", largest3); Console.ReadLine(); } }

Prime Number
class Program { static void Main(string[] args) { Console.WriteLine("Enter a Number to verify the prime number:"); string strNum = Console.ReadLine(); int intNum = Int32.Parse(strNum); int ctr = 0; for (int i = 1; i <= intNum; i++) { if (intNum % i == 0) { ctr++; } } if (ctr == 2) { Console.WriteLine("Entered Number is a Prime Number"); } else { Console.WriteLine("Entered Number is not a Prime Number"); } } }

Palindrome

class Program { static void Main(string[] args) { string str, revstr; Console.WriteLine("Enter the string or number to check"); str = Console.ReadLine(); char[] tempstr = str.ToCharArray(); Array.Reverse(tempstr); revstr = new string(tempstr); bool caseignore = str.Equals(revstr, StringComparison.OrdinalIgnoreCase); if (caseignore == true) { Console.WriteLine("..." + str + " Is a Palindrome...."); } else { Console.WriteLine("..." + str + " Is Not a Palindrome... "); } Console.Read(); } }

Temprature Conversion class Program { static void Main(string[] args) { Console.WriteLine("Enter a choice::"); Console.WriteLine(":C to F"); Console.WriteLine(":F to C"); int choice = Convert.ToInt32( Console.ReadLine()); switch (choice) { case 1: { Console.WriteLine("Enter Centegrade value"); double c = Convert.ToDouble(Console.ReadLine()); double f = (c * (1.8)) + 32; Console.WriteLine("Value In F=" + f); break; } case 2: { Console.WriteLine("Entr Farenheite Value"); double f2 = Convert.ToDouble(Console.ReadLine()); double c2 = (f2 - 32) * 5 / 9; Console.WriteLine("Value in C=" + c2); break; } default: { Console.WriteLine("Wrong Value Enetred"); break; } }

Sorting of numbers class Program { static void Main(string[] args) { int[] array = new int[10]; int i = 0,high=0,j=0; Console.WriteLine("enter the Size of arry"); int n = Convert.ToInt32(Console.ReadLine()); for (i = 0; i < n; i++) { Console.WriteLine("Enter elements:{0}", i + 1); array[i] = Convert.ToInt32(Console.ReadLine()); if (array[i] > high) high = Convert.ToInt32(array[i]); } Console.WriteLine("array before sorting"); for (i = 0; i < n; i++) { Console.WriteLine("elements:{0}",+array[i]); } Console.WriteLine("array after sorting"); for (i = 0; i <= high; i++) { for (j = 0; j<n; j++) { if (i == array[j]) Console.WriteLine("elements:{0}", +array[j]); } } } }

You might also like