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

1.factorial Program in C Sharp Using For Loop

This C# program calculates the factorial of a given number using a for loop. It takes a number as a parameter, initializes a variable to hold the factorial result, and uses a for loop to iteratively multiply the variable by integers from 1 to the given number. It then returns the final result.

Uploaded by

lavguly
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
0% found this document useful (0 votes)
75 views4 pages

1.factorial Program in C Sharp Using For Loop

This C# program calculates the factorial of a given number using a for loop. It takes a number as a parameter, initializes a variable to hold the factorial result, and uses a for loop to iteratively multiply the variable by integers from 1 to the given number. It then returns the final result.

Uploaded by

lavguly
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/ 4

1.

factorial program in c sharp using for loop This program calculate factorial of given number using for loop in C#. for example this program calculate factorial of 4 and give answer 24. you can try this program for different numbers. instead of for loop you can also use other loops like while and do while loop or you can go for recursive functions.

using System; using System.Text; using System.Collections; using System.Data; namespace Console_App { public class clsFactorial { public static void Main() { try { Console.WriteLine("The factorial of 4 is: {0}\n", Factorial(4)); } catch(Exception ex) { //handle exception here } Console.ReadLine(); } static long Factorial(long number) { long FinalNumber = 1; for (int i = 1; i <= number; i++) { FinalNumber = FinalNumber * i;

} return FinalNumber; } } }

2.Program that computes Fibonacci iteratively [C#]


using System; class Program { public static int Fibonacci(int n) { int a = 0; int b = 1; // In N steps compute Fibonacci sequence iteratively. for (int i = 0; i < n; i++) { int temp = a; a = b; b = temp + b; } return a; } static void Main() { for (int i = 0; i < 15; i++) { Console.WriteLine(Fibonacci(i)); } } } Output 1 1 2 3 5 8 13 21 34 55 89 144 233

377

3.Write a Program in C# to Check whether a number is Palindrome or not.


using System; using System.Collections.Generic; using System.Text; namespace Lab_Ex_1 { class PalindromeProgram { static void Main(string[] args) { Console.Write(" Enter any number: "); int num = int.Parse(Console.ReadLine()); int rev = 0, n, rem; n = num; while (num != 0) { rem = num % 10; rev = rev * 10 + rem; num = num / 10; } Console.WriteLine(" \n Original number is " + n); Console.WriteLine(" \n Reversed number is " + rev); if (n == rev) { Console.WriteLine("\n Original number = Reversed number."); Console.WriteLine("\n So, {0} is Palindorme ", n); Console.ReadLine(); } else { Console.WriteLine("\n Original number != Reversed number."); Console.WriteLine("\n So, {0} is not a Palindorme ", n); Console.ReadLine(); } } } }

4.Example program that reverses strings [C#]


using System; static class StringHelper { public static string ReverseString(string s) { char[] arr = s.ToCharArray(); Array.Reverse(arr); return new string(arr); } }

class Program { static void Main() { Console.WriteLine(StringHelper.ReverseString("framework")); Console.WriteLine(StringHelper.ReverseString("samuel")); Console.WriteLine(StringHelper.ReverseString("example string")); } } Output krowemarf leumas gnirts elpmaxe

You might also like