Question
c# language
Description: Design and implement a C# application that allows the user to enter a number N. It returns to the user the resul
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C Sharp Application:

File: Form1.Designer.cs

namespace Fibonacii
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.tbNumber = new System.Windows.Forms.TextBox();
this.lblResult = new System.Windows.Forms.Label();
this.btnCalculate = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 25);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(89, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Enter an Integer: ";
//
// tbNumber
//
this.tbNumber.Location = new System.Drawing.Point(110, 22);
this.tbNumber.Name = "tbNumber";
this.tbNumber.Size = new System.Drawing.Size(118, 20);
this.tbNumber.TabIndex = 1;
//
// lblResult
//
this.lblResult.AutoSize = true;
this.lblResult.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblResult.Location = new System.Drawing.Point(28, 62);
this.lblResult.Name = "lblResult";
this.lblResult.Size = new System.Drawing.Size(0, 16);
this.lblResult.TabIndex = 2;
//
// btnCalculate
//
this.btnCalculate.Location = new System.Drawing.Point(251, 20);
this.btnCalculate.Name = "btnCalculate";
this.btnCalculate.Size = new System.Drawing.Size(75, 23);
this.btnCalculate.TabIndex = 3;
this.btnCalculate.Text = "Calculate";
this.btnCalculate.UseVisualStyleBackColor = true;
this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(338, 113);
this.Controls.Add(this.btnCalculate);
this.Controls.Add(this.lblResult);
this.Controls.Add(this.tbNumber);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Fibonacci";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox tbNumber;
private System.Windows.Forms.Label lblResult;
private System.Windows.Forms.Button btnCalculate;
}
}

File: Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fibonacii
{
public partial class Form1 : Form
{
//Exception class
class FibException : Exception
{
public FibException()
: base(String.Format("FibException: Fibonacci number cannot be negative")) { }
}

//Class that calculates Fibonacci number recursively
class FibRecursive
{
//Method that calculates the value
public int CalcFib(int n)
{
//Finding fibonacci
if (n < 0)
throw new FibException();

//Base cases
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return CalcFib(n - 1) + CalcFib(n - 2);
}
}

//Derived class
class FibIterative : FibRecursive
{
//Method that calculates the value
public int CalcFib(int n)
{
//Finding fibonacci
if (n < 0)
throw new FibException();

//Base cases
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
{
int a = 0, b = 1, c=0;

for (int i = 2; i <= n; i++)
{
//Calcalating value
c = a + b;
  
//Swapping
a = b;
b = c;
}

return c;
}
}
}
  
public Form1()
{
InitializeComponent();
}

//Calculate button click event
private void btnCalculate_Click(object sender, EventArgs e)
{
try
{
//Getting value
int n = Convert.ToInt32(tbNumber.Text);

//Calculating result
FibRecursive fObj = new FibRecursive();
int result = fObj.CalcFib(n);

//Writing result to lable
lblResult.Text = "Fibonacci of " + n.ToString() + " is " + result.ToString();
}
catch (FibException ex)
{
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
MessageBox.Show("Enter a valid number!!");
}
}
}
}

_______________________________________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
c# language Description: Design and implement a C# application that allows the user to enter a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence....

    Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence. Your program should prompt for an integer input n from the user. The program should call a recursive function to compute the nth fibonacci number. Your program must follow programming convention. You should submit program and screenshot of output in a single word/pdf file. You should use following recursive definition of fibonacci function: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) +fib(n-2)

  • We use JAVA. Thanks.    Create an application whose main method asks the user to enter...

    We use JAVA. Thanks.    Create an application whose main method asks the user to enter an n by m integer matrix that contains nm integer numbers. n and m should be between 1 and 10. If the user enters a             number less than 1 or greater than 10, the program will continue to ask the user to enter an integer number between 1 and 10. The program should print the sum of the boundary elements of the matrix....

  • In C++ 1. Write a program that allows a user to enter 10 integer values from...

    In C++ 1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array. 2. The program should then ask the user for a number to search for in the array: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...

  • Design JavaFX application with 7 labels and one textfield where user enters input inches. When user...

    Design JavaFX application with 7 labels and one textfield where user enters input inches. When user enters his choice and presses enter key to complete input, program outputs resulting yards, feet, and inches. Use class P5 that extends Application with start method in it, and class P5Pane that extends GridPane. The only instance variables for P5Pane class are inputInches where user enters input inches, and three labels: outYards, outFeet, and outInches where program displays result of conversion. Use the following...

  • In mathematics, the Fibonacci numbers are the series of number that exhibit the following pattern: 0,1,1,2,3,5,8,13,21,34,55,89,144,.......

    In mathematics, the Fibonacci numbers are the series of number that exhibit the following pattern: 0,1,1,2,3,5,8,13,21,34,55,89,144,.... In mathematical notation the sequence Fn of Fibonacci number is defined by the following recurrence relation: Fn=Fn-1+Fn-2 With the initial values of F0=0 and F1=1. Thus, the next number in the series is the sum of the previous two numbers. Write a program that asks the user for a positive integer N and generate the Nth Fibonacci number. Your main function should handle user...

  • PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a...

    PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a function yesOrNo which has no parameters. When called, it gets input from the user until the user types either 'yes' or 'no', at which point the function should return True if the user typed 'yes' and False if the user typed 'no'. Any other entries by the user are ignored and another value must be input. For example: Test Input Result print(yesOrNo()) hello blank...

  • ** C++ LANGUAGE ** Write a function that will implement each Fibonacci number with the help...

    ** C++ LANGUAGE ** Write a function that will implement each Fibonacci number with the help of an integer array of size 100 (elements of this array will be digits of the Fibonacci number). When the function is called to find , it will calculate all Fibonacci numbers from to using the basic formula . To add two Fibonacci numbers, the function will add elements of two arrays corresponding to and and store their sums in the array corresponding to...

  • In this assignment, you will design a simple application which will take stream of numbers and...

    In this assignment, you will design a simple application which will take stream of numbers and display the summation at the end of the program. The application will keep asking for user numbers and keep adding. The user input will be integer numbers and can be positive or negative. The program will repeat until user types “EXIT” which indicates the user wants to exit the program. After exiting, the program will display the summation of the numbers they entered. For...

  • in Python: Write a program that allows the user to enter a number between 1-5 and...

    in Python: Write a program that allows the user to enter a number between 1-5 and returns an animal fact based on what the user entered. The program should 1. Continue to run until the user enters “quit” to terminate the program, and 2. Validate the user’s input (the only acceptable input is 1, 2, 3, 4, 5 or “quit”). You can use the following animal facts: An octopus is a highly intelligent invertebrate and a master of disguise. Elephants...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT