Skip to main content

Posts

Showing posts from February 23, 2012

Create a function that a number by value and returns smallest and largest factors of the number through pass by reference.

using System; namespace st { class HighestLowestFactors { public static void Main() { int highfact, lowfact; Console.WriteLine( "Enter a number : " ); int num = Int32.Parse(Console.ReadLine()); GetHighestLowestFactors( num, out highfact, out lowfact); Console.WriteLine( "For number {0}, Highest Factor is {1} and Lowest Factor is : {2}" , num, highfact,lowfact ); } public static void GetHighestLowestFactors( int num, out int hf, out int lf) { // get lowest factor other than 1. In case of prime number 1 lf = 1; for ( int i = 2; i <= num / 2; i++) { if (num % i == 0) { lf = i; break ; } } // get highest factor other than the number. In case

Accept a number on the command line and display whether it is a strong number.

using System; namespace st { class StrongNumber { public static void Main( string [] args) { int num = Int32.Parse(args[0]); int orgnum = num; int total = 0; while (num != 0) { // take a digit int digit = num % 10; int factorial = 1; for ( int i = 1; i <= digit; i++) factorial *= i; total += factorial; // remove digit num /= 10; } if (total == orgnum) Console.WriteLine( "{0} is a strong number" , orgnum); else Console.WriteLine( "{0} is not a strong number" , orgnum); } // main } // class } // st

Declare an array of 10 elements. Read values from user and insert them into array. Display how many values are in the array are positive, negative and zeros.

using System; namespace st { public class ArrayCount { public static void Main() { int [] a = new int [10]; for ( int i = 0; i < a.Length; i++) { Console.Write( "Enter a number :" ); a[i] = Int32.Parse(Console.ReadLine()); } int nop, non, noz; nop = non = noz = 0; foreach ( int n in a) { if (n > 0) nop++; else if (n < 0) non++; else noz++; } Console.WriteLine( "No. of positives : {0}" , nop); Console.WriteLine( "No. of negatives : {0}" , non); Console.WriteLine( "No. of zeroes : {0}" , noz); } // main } // class } // namespace

Create a function that takes two numbers and returns the highest common factor

1: using System; 2: namespace st 3: { 4: class HCF 5: { 6: public static void Main() 7: { 8: Console.WriteLine(Hcf(30, 55)); 9: Console.WriteLine(Hcf(28, 27)); 10: } 11: public static int Hcf( int n1, int n2) 12: { 13: // start the loop from half of the smallest of two numbers 14: for ( int i = n1 < n2 ? n1 / 2 : n2 / 2; i > 1; i--) 15: { 16: if (n1 % i == 0 && n2 % i == 0) 17: return i; // found Highest common factor 18: } // for 19:   20: return 1; // Didn't find highest common factor, so 1 is treated as so 21: } // HFC 22:   23: } // class 24:   25: } // namespace

Create a function that takes an array and returns the first two highest numbers

1: using System; 2:   3: namespace st 4: { 5: class TwoHighest 6: { 7:   8: public static void Main() 9: { 10: int fh, sh; 11: int [] a = { 10, 30, 40, 22, 55 }; 12: FirstTwoHighest(a, out fh, out sh); 13: Console.WriteLine( "First Highest {0}, Second Highest : {1}" , fh, sh); 14: } 15:   16: public static void FirstTwoHighest( int [] a, out int fh, out int sh) 17: { 18: fh = sh = 0; 19: foreach ( int n in a) 20: { 21: if (n > fh) 22: { 23: sh = fh; 24: fh = n; 25: } 26: else 27: if ( n > sh ) 28: sh = n; 29:   30: } // foreach 31: } // FirstTwoHighest 32:

Write a program to take a series of numbers from user and display the highest of all the numbers

1: using System; 2:   3: namespace st 4: { 5: class Highest 6: { 7: static void Main() 8: { 9: int highest = 0; 10: while ( true ) 11: { 12: Console.Write( "Enter a number :" ); 13: string input = Console.ReadLine(); 14: int num = Int32.Parse(input); 15: // stop if user's input is 0 16: if (num == 0) 17: break ; 18: if (num > highest) 19: highest = num; 20: } 21: Console.WriteLine( "Highest of all numbers is : {0}" , highest); 22: } // main 23: } // class 24: } // st

Write a program to take a number and display highest factor for that number.

1: using System; 2: namespace st 3: { 4: class HighestFactor 5: { 6: static void Main() 7: { 8: Console.Write( "Enter a number :" ); 9: string input = Console.ReadLine(); 10: int num = Int32.Parse(input); 11: for ( int i = num / 2; i > 0; i--) 12: { 13: if (num % i == 0) 14: { 15: Console.WriteLine( "Highest factor for {0} is {1}" , num, i); 16: break ; 17: } 18: } // for 19: } // main 20: } // class 21: } // namespace Technorati Tags: C#