"c sharp" code for factorial using static variables

Answers were Sorted based on User's Feedback



"c sharp" code for factorial using static variables..

Answer / arup ratan banerjee

using System;
class fact
{
int factorial(int x)
{
Static int y=1;
y=y*x;
return y;
}
public static void Main()
{
fact f=new fact();
int k=Convert.ToInt32(System.ReadLine());
for(int i=1;i<=x;i++)
{
int x=f.factorial(i);
}
System.Console.WriteLine(x);
}
}

Is This Answer Correct ?    64 Yes 33 No

"c sharp" code for factorial using static variables..

Answer / deepa jalan

class Factorial
{
private static int temp = 1;

public static int CalFactorial(int n)
{
temp = temp * n;
return temp;


}
static void Main(string[] args)
{
Console.WriteLine("Enter A Number:");
int fact = Convert.ToInt32(Console.ReadLine());

int x = 1; ;
for (int i = 1; i <= fact; i++)
{
x = Factorial.CalFactorial(i);
}
System.Console.WriteLine(x);
Console.ReadLine();

}
}

Is This Answer Correct ?    23 Yes 3 No

"c sharp" code for factorial using static variables..

Answer / shadab md

Hi Everybody,
Arup Ratan Banerjee solution is near to factorial calculation,
if we make small changes it will work perfectly.
1.like declaring x variable before loop.
2.looping through k not x.
3. lastly and very important x=x* factorial(i);


**********************************************************
using System;
class fact
{

public static void Main()
{
fact f=new fact();
int x=1; //Declaration of x as 1
int k=Convert.ToInt32(System.ReadLine());
for(int i=1;i<=k;i++)
{
x= x *i;
}
System.Console.WriteLine(x);
}
}
**********************************************************
Thanks

Is This Answer Correct ?    25 Yes 8 No

"c sharp" code for factorial using static variables..

Answer / amer

static void Main(string[] args)
{


int x=1;
int b=Convert.ToInt32(Console.ReadLine());



for (int j = 1 ; j <= b; j++)
{
x = x * j;
Console.WriteLine(x);
}
}

Is This Answer Correct ?    17 Yes 5 No

"c sharp" code for factorial using static variables..

Answer / gyan

what u r trying to show,a factorial value of a number or
prime number or any other else, sorry brother i couldnot
undrstnd?

Is This Answer Correct ?    13 Yes 2 No

"c sharp" code for factorial using static variables..

Answer / sivalal

what is the use of try,catch statements in code behind
class?

Is This Answer Correct ?    17 Yes 13 No

"c sharp" code for factorial using static variables..

Answer / rahul

Dear Mr. Shadab Mohamd Pls Tell Me Why u hav Created f as an object when it is not used any where, The question tells u that u hav 2 create a fact. method using static variables where are the static variables used in ur programme.......

Is This Answer Correct ?    8 Yes 4 No

"c sharp" code for factorial using static variables..

Answer / m.shahwaiz hasan

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication20
{
class program
{
static long factorial = 1;
static int num;
static void facto()
{
Console.WriteLine("enter number u want for factorial but less than 20 0r greater than 0 ");
num = int.Parse(Console.ReadLine());
if (num > 20)
{
Console.WriteLine("you exceed the limit");
}
else if (num < 0)
{
Console.WriteLine("{0} is undefined", num);
}
else
{
for (int i = 1; i <= num; i++)
{
factorial *= i;
}
Console.WriteLine("\n factorial of {0} is {1} ", num, factorial);
}
}

class Program
{
static void Main(string[] args)
{
program.facto();
Console.ReadLine();
}
}

}
}

Is This Answer Correct ?    3 Yes 0 No

"c sharp" code for factorial using static variables..

Answer / sarath

The answer posted above by Arup Ratan Banerjee is entirely
wrong.he is just taking an input value and sending it as an
argument to factorial method and multiplying it with value
of y, which is 1 and then he is returing that result,which
is the input value only.

try this code in C#:


public class test1
{
public static void Main(string[] args){
int fact=0;
Console.WriteLine("Enter A Number:");
fact = Convert.ToInt32(Console.ReadLine());
for (int i=1; i<=fact; i++){
if (fact%i == 0){ //It works
System.Console.WriteLine(i);
}
}
Console.Read();
}
}

Is This Answer Correct ?    18 Yes 23 No

Post New Answer

More C Sharp Code Interview Questions

<ul> <li id="Accommodation" runat="server" visible="false"><a href="../Elements/frmSearchAccommodation.aspx?submenuheader=1">Accommodation</a></li> <li id="Arts" runat="server" visible="false"><a href="../Elements/frmSearchProp.aspx?submenuheader=1">Arts And Props</a></li> <li id="Costume" runat="server" visible="false"><a href="../Elements/frmSearchCostume.aspx?submenuheader=1">Costume</a></li> <li id="Crew" runat="server" visible="false"><a href="../Elements/frmSearchCrew.aspx?submenuheader=1">Crew</a></li> <li id="Catering" runat="server" visible="false"><a href="../Elements/frmSearchCatering.aspx?submenuheader=1">Catering</a></li> <li id="Equipment" runat="server" visible="false"><a href="../Elements/frmSearchEquipment.aspx?submenuheader=1">Equipment</a></li> <li id="Film" runat="server" visible="false"><a href="../Elements/frmSearchFilmTapeStock.aspx?submenuheader=1">Film And Tape Stocks</a></li> <li id="Location" runat="server" visible="false"><a href="../Elements/frmSearchLocation.aspx?submenuheader=1">Location</a></li> <li id="Picture" runat="server" visible="false"><a href="../Elements/frmSearchPictureVehicle.aspx?submenuheader=1">Picture Vehicles</a></li> <li id="Production" runat="server" visible="false"><a href="../Elements/frmSearchProductionSupplies.aspx?submenuheader=1">Production Supplies</a></li> <li id="Post" runat="server" visible="false"><a href="../Elements/frmSearchPostProductionHouse.aspx?submenuheader=1">Post Production Houses</a></li> <li id="Rigging" runat="server" visible="false"><a href="../Elements/frmSearchRigging.aspx?submenuheader=1">Rigging</a></li> <li id="Stunt" runat="server" visible="false"><a href="../Elements/frmSearchStunt.aspx?submenuheader=1">Stunt</a></li> <li id="Studio" runat="server" visible="false"><a href="../Elements/frmSearchStudio.aspx?submenuheader=1">Studio</a></li> <li id="SFX" runat="server" visible="false"><a href="../Elements/frmSearchSFX.aspx?submenuheader=1">SFX</a></li> <li id="Talents" runat="server" visible="false"><a href="../Elements/frmSearchTalent.aspx?submenuheader=1">Talents</a></li> <li id="Transport" runat="server" visible="false"><a href="../Elements/frmSearchTransport.aspx?submenuheader=1">Transport</a></li> <li id="Venue" runat="server" visible="false"><a href="../Elements/frmSearchVenue.aspx?submenuheader=1">Venue</a></li> <li id="Other" runat="server" visible="false"><a href="../PageUnderConstruction.aspx?submenuheader=1">Other</a></li> </ul> I have this code in leftmenu.ascx page..now on leftmenu.ascx.cs page i want to access id values. but i dont want to check manually like Accommodation,Arts etc.. In cs file my code is like string str[]; //this array contains some values say 10 strings for(i=0;i<100;i++) { if(str[i]==id id.visible=true } i.e.i want to travel for each element of str and each value of id.if they match then id.visible=true how to do?

0 Answers  


How to use ASP.NET 2.0's TreeView to Display Hierarchical Data?

1 Answers  


how do i copy textbox contents of 1 form to another form

4 Answers   Wipro,


Code for Reading and writing from a file?

0 Answers   IBM, Xoriant,


Write a program. there are 1..n numbers placed in an array in random fashion with one integer missing. find the missing number.

1 Answers   Infosys, Mind Tree,






Create a class called Accounts which has data members like ACCOUNT no, Customer name, Account type, Transaction type (d/w), amount, balance D->Deposit W->Withdrawal If transaction type is deposit call the credit(int amount) and update balance in this method. If transaction type is withdraw call debit(int amt) and update balance. Pass the other information like Account no,name,Account Type through constructor. Call the show data method to display the values.

1 Answers   Cognizant,


How to add a value from textBox over an existing certain column in SQL Server

0 Answers  


How to Create Files by Using the FileInfo Class?

1 Answers  


Coding for Manipulate XML File Data Using C#?

1 Answers  


working with arrays

1 Answers  


c# code to Count number of 1's in a given range of integer (0 to n)

0 Answers  


"c sharp" code for factorial using static variables

9 Answers  


Categories
  • ASP.NET Code Interview Questions ASP.NET Code (46)
  • VB.NET Code Interview Questions VB.NET Code (9)
  • C Sharp Code Interview Questions C Sharp Code (51)
  • ADO.NET Code Interview Questions ADO.NET Code (8)