Prime Number
1. Prime Number Upto 99
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Prime
{
class Program
{
void prime_num(long num)
{
bool isPrime = true;
for (int i = 0; i <= num; i++)
{
for (int j = 2; j <= num; j++)
{
if (i != j && i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
Console.WriteLine ( "Prime No:" + i );
}
isPrime = true;
}
}
static void Main(string[] args)
{
Program p = new Program();
p.prime_num (99L);
Console.ReadLine();
}
}
}
2.Prime Number Between Two Given Number
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PrimeNumber
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a No");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Second No");
int b = Convert.ToInt32(Console.ReadLine());
//Add two Number
int d = a + b;
Console.WriteLine("{0}", d);
//Prime Logic
bool c = true;
for (int i = a; i <= b; i++)
{
//Condition For Not a Prime All Non Prime No are Exlcluded Here
for (int j = 2; j <= b; j++)
{
if (i != j && i % j == 0)
{
c = false;
break;
}
}
//Condition For Prime No
if (c)
{
Console.WriteLine("Prime:" + i);
}
c = true;
}
Console.ReadLine();
}
}
}
Reverse A Number /String
1.Reverse a Number
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Reverse_a_Number
{
class Program
{
static void Main(string[] args)
{
int reverse=0,a;
Console.Write("Enter A Number");
a = Convert.ToInt32(Console.ReadLine());
while (a!=0)
{
reverse = reverse * 10;
reverse = reverse + a % 10;
a = a / 10;
}
Console.Write("{0}",reverse);
Console.ReadLine();
}
}
}
2.Reverse a String
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Reverse_a_String
{
class Program
{
static void Main(string[] args)
{
string Str, Revstr = "";
int Length;
Console.Write("Enter A String : ");
Str = Console.ReadLine();
Length = Str.Length - 1;
while (Length >= 0)
{
Revstr = Revstr + Str[Length];
Length--;
}
Console.WriteLine("Reverse String Is {0}", Revstr);
Console.ReadLine();
}
}
}
No comments:
Post a Comment