0% found this document useful (0 votes)
4 views3 pages

Simple DotNet Programs

The document contains several C# programs demonstrating basic algorithms, including generating the Fibonacci series, checking for prime numbers, determining if a number is a palindrome, calculating the factorial of a number, and swapping two numbers using a third variable. Each program prompts the user for input and displays the result accordingly. Sample outputs are provided for each program to illustrate their functionality.

Uploaded by

bindiyap
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)
4 views3 pages

Simple DotNet Programs

The document contains several C# programs demonstrating basic algorithms, including generating the Fibonacci series, checking for prime numbers, determining if a number is a palindrome, calculating the factorial of a number, and swapping two numbers using a third variable. Each program prompts the user for input and displays the result accordingly. Sample outputs are provided for each program to illustrate their functionality.

Uploaded by

bindiyap
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/ 3

Fibonacci Series

using System;
public class FibonacciExample
{
public static void Main(string[] args)
{
int n1=0,n2=1,n3,i,number;
Console.Write("Enter the number of elements: ");
number = int.Parse(Console.ReadLine());
Console.Write(n1+" "+n2+" "); //printing 0 and 1
for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
Console.Write(n3+" ");
n1=n2;
n2=n3;
}
}
}
O/P :- Enter the number of elements: 15

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Prime Number Program

using System;
public class PrimeNumberExample
{
public static void Main(string[] args)
{
int n, i, m=0, flag=0;
Console.Write("Enter the Number to check Prime: ");
n = int.Parse(Console.ReadLine());
m=n/2;
for(i = 2; i <= m; i++)
{
if(n % i == 0)
{
Console.Write("Number is not Prime.");
flag=1;
break;
}
}
if (flag==0)
Console.Write("Number is Prime.");
}
}

O/p –

Enter the Number to check Prime: 17


Number is Prime.
Enter the Number to check Prime: 57
Number is not Prime.

Palindrome program

using System;
public class PalindromeExample
{
public static void Main(string[] args)
{
int n,r,sum=0,temp;
Console.Write("Enter the Number: ");
n = int.Parse(Console.ReadLine());
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
Console.Write("Number is Palindrome.");
else
Console.Write("Number is not Palindrome");
}
}
Enter the Number=121
Number is Palindrome.
Enter the number=113
Number is not Palindrome.

Factorial program

using System;
public class FactorialExample
{
public static void Main(string[] args)
{
int i,fact=1,number;
Console.Write("Enter any Number: ");
number= int.Parse(Console.ReadLine());
for(i=1;i<=number;i++){
fact=fact*i;
}
Console.Write("Factorial of " +number+" is: "+fact);
}
}

Enter any Number: 6


Factorial of 6 is: 720

Swapping two numbers using a third variable

using System;
namespace LogicalPrograms
{
public class Program
{
public static void Main()
{
int number1 = 10, number2 = 20, temp = 0;
Console.WriteLine($"Before SWapping number1= {number1}, number2 = {number2}");
temp = number1; //temp=10
number1 = number2; //number1=20
number2 = temp; //number2=10
Console.WriteLine($"After swapping number1= {number1}, number2 = {number2}");
Console.ReadKey();
}
}
}

You might also like