Wednesday 13 March 2013

Perfect Number | program for single number | 1 to 100 | in c# with source code | output

Perfect Number  | program for  single number | 1 to 100 | in c# with source code | output

Perfect Number  | single number | 1 to 100 | in c# with source code | output with explanation
In this program i have shown how to check whether entered number is perfect number or not.
Logic i have developed is so simple .Anyone can learn easily.
so first i will show you how to check perfect  number logic for single number.
Definition of perfect number:- Perfect number is number which is equal to  its factor's addition.
example 6 has 3 factor 3 and 2 and 1 now if i will add 1 +2+ 3=6 which is number itself.so , we can say
6 is perfect number.
now check for 8.
1,2,2,2 here if i will add 4 number i will get 1+2+2+2=7 which is not equal to 8.so, 8 is not perfect number.
Here is source code in c# .similar code we can write in c and other language just change the
printing syntax.

  protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("</br>");
        Response.Write("How to check The given number is Perfect number or not");
        Response.Write("</br>");
        Response.Write("----------------------------------------------------------");
        Response.Write("</br>");
        int num = 10;
        int sum = 0;
        for (int i = 1; i < num; i++)
        {
            if (num % i == 0) // finding its factor
            {
                sum = sum + i;
            }
        }
            if(sum==num)
            {
                Response.Write (num+ "is Perfect Number ");
            }
            else
            {
                Response.Write (num+ " is  Not Perfect Number ");

            }
       

    }


output:-
How to check The given number is Perfect number or not
----------------------------------------------------------
10 is Not Perfect Number


Now i want to print list of perfect number from 1 to 100 .
logic will be same just include one more for loop.




   protected void Page_Init(object sender, EventArgs e)
    {
        Response.Write("</br>");
        Response.Write ("Print List oF  perfect Number from 1 to 100");
        Response.Write("</br>");
        Response.Write("----------------------------------------------");
        Response.Write("</br>");
        int num = 100;
        int sum = 0;
        for (int j = num; j > 0;)
        {
            for (int i = 1; i < j; i++)
            {
                if (j % i == 0)
                {
                    sum = sum + i;

                }
               

            }
            if (j == sum)
            {
                Response.Write(j + " is Perfect Number");
                Response.Write("</br>");
            }
            j--;

            sum = 0;
           



        }

    }
     

output:-


Print List oF perfect Number from 1 to 100
----------------------------------------------
28 is Perfect Number
6 is Perfect Number

Tuesday 12 March 2013

Sum Of series in c#.net source code with output and explanation

Sum Of series in c#.net source code with output and explanation

Sum Of series in c#.net source code with output and explanation
in this program we will see how to calculate sum of series till sum number like 10 , 20 , etc....
Now what we will do we will just take two value assign them as 0  and 1. so , at least we will get
some value at start.
In this article i have shown here how  to calculate sum of series in c# .net on Page Init
 event.


 protected void Page_Init(object sender, EventArgs e)
    {
       
     
       int a=0;   //here i have initialize two valus as 0 and 1.
        int b=1;
        int num=20;
        Response.Write("Sum Of series Before:"+num);
        Response.Write("</br>");
       int  sum=0;
        for (int j=1;j<=num;j++)
        {
            sum=a+b; //swappping.............
            a=b;
            b=sum;
            Response.Write(sum+"-");  // printing the values.......
        }

       
        }


OUTPUT
Sum Of series Before:20
1-2-3-5-8-13-21-34-55-89-144-233-377-610-987-1597-2584-4181-6765-10946  

IF YOU WILL TAKE NUM=10
THEN OUTPUT WILL

Sum Of series Before:10
1-2-3-5-8-13-21-34-55-89 

Monday 11 March 2013

Factorial Number program source code | From 1 to n range | c#.net

  Factorial Number program source code | From 1 to n range | c#.net
  Factorial Number program source code | From 1 to n range | c#.net
In this program i have shown how to find factorial number of given range.
using for loop i have calculated factorial number from 1  to 5 .
But , we  can calculate factorial number from 1 to n using below given source code.
output is there.
same logic we  can apply in c language , vb.net also but difference is only of syntax will be changed.
rest logic will be same.

 protected void Page_Init(object sender, EventArgs e)
    {

        int sum = 1;
        int i = 5;

        for (int j = i; j > 0; )
        {

            for (int k = 1; k <= j; )
            {

                sum = sum * j;
                j--;
            }
            Response.Write("Factorial of " + i + "is" + "\t" + sum);
            Response.Write("</br>");
            i--;
            j = i;
            sum = 1;

        }
  
          
               }


Factorial of 5 is 120
Factorial of 4 is 24
Factorial of 3 is 6
Factorial of 2 is 2
Factorial of 1 is 1

Prime Number source code | C#.Net | Prime Number from 1 to n..with examples and comments

Prime Number source code | C#.Net | Prime Number from 1 to n..with examples and comments

 source code how to calculate prime number in c#.net.With valid and correct output.
Definition Of Prime Number:- The number which is divisible by 1 and itself.
or we can say prime number is a number which has only two factor first one is 1 and second
is that number itself.
 in this article i have shown to calculate prime number from one range to n range and also how to find
simple number  is prime or not.


Now first to calculate whether entered number is prime number or not.
here is code:- on page init


   
    protected void Page_init(object sender, EventArgs e)
    {
           
        int num=20, flag=0;
     
     
        for (int i=2;i<;num/2++)//i will be less than half of number it will also work.
        {
          if(num%i==0)
          {
          flag=1;   // now entered number 20 is divisible by 2 means 20 has another factor that is 2
so , it  is not prime number no need to go in execution use break statement.
          break;
         
          }
          else
          {
          flag=0;
          }
        }
        if (flag==0)
        {
          Response.Write(" +num+"is  not prime number :-");
        Response.Write("</br>");
        }
else
{

 Response.Write( +num+"is  a  prime number :-");
}

       
     


    }
}


It was simple program to check number is prime or not now i want to check number from n to n+...
or 1 to 20 , 1 to 100 , 1 to 1000 just enter num=100 or num=1000 as you want range.

   
    protected void Page_init(object sender, EventArgs e)
    {
           
        int num=20, flag=0;
        Response.Write("Prime Number from 2 to" +num+"is :-");
        Response.Write("</br>");
        for (int j=2;j<num;j++)
        {
        for (int i=2;i<j;i++)
        {
          if(j%i==0)
          {
          flag=1;
          break;
         
          }
          else
          {
          flag=0;
          }
        }
        if (flag==0)
        {
        Response.Write(j);
         Response.Write("</br>");
        }
       
        }


    }
}


Prime Number from 2 to 20 is :-
2
3
5
7
11
13
17
19