Algorithm To Find Sum of The Digits of Num
Algorithm To Find Sum of The Digits of Num
int sum = 0;
int currentDigit;
return sum;
}
An algorithm is written in simple English and is not a formal document. An algorithm
must:
Also note it is important to use indentation when writing solution algorithm because it
helps to differentiate between the different control structures.
2) Definiteness: - each step in algorithm is unambiguous. This means that the action
specified by the step cannot be interpreted (explain the meaning of) in multiple ways &
can be performed without any confusion.
5) Effectiveness:- it consists of basic instructions that are realizable. This means that the
instructions can be performed by using the given inputs in a finite amount of time.
Computational problem
From Wikipedia, the free encyclopedia
is a computational problem. Computational problems are one of the main objects of study
in theoretical computer science. The field of algorithms studies methods of solving
computational problems efficiently. The complementary field of computational
complexity attempts to explain why certain computational problems are intractable for
computers.
A decision problem is typically represented as the set of all instances for which the
answer is yes. For example, primality testing can be represented as the infinite set
In a search problem, the answers can be arbitrary strings. For example, factoring is a
search problem where the instances are (string representations of) positive integers and
the solutions are (string representations of) collections of primes.
R = {(4, 2), (6, 2), (6, 3), (8, 2), (8, 4), (9, 3), ...}
which consist of all pairs of numbers (n, p), where p is a nontrivial prime factor of n.
A counting problem asks for the number of solutions to a given search problem. For
example, the counting problem associated with primality is
"Given a positive integer n, count the number of nontrivial prime factors of n."
A counting problem can be represented by a function f from {0, 1}* to the nonnegative
integers. For a search relation R, the counting problem associated to R is the function
An optimization problem asks for finding the "best possible" solution among the set of all
possible solutions to a search problem. One example is the maximum independent set
problem:
Here, the valid instances are those graphs whose maximum independent set size is either
at most 5 or at least 10.
Decision promise problems are usually represented as pairs of disjoint subsets (Lyes, Lno)
of {0, 1}*. The valid instances are those in Lyes ∪ Lno. Lyes and Lno represent the instances
whose answer is yes and no, respectively.
• System Analysis
• Specification and Design
• Program
• Debug
• Alpha Test
• Beta Test
• making a formal
representation of the
system being designed
Virtually all programs have defects in them called 'bugs' and these need to
be eliminated. Bugs can arise from errors in the logic of the program
specification or errors in the programming code created by a programmer.
Some bugs are difficult to locate and fixing them is like solving a complex
puzzle.
Bugs and missing features due to the application being unfinished will be
found. Any errors in the code and specification will be corrected at this
stage.
This is where we hope that the remaining bugs are found, but some may
remain undetected or unfixed.
Program Development - Software Delivery
When they use the software, bugs that were not found during testing
may appear. As these new bugs are reported an updated version of the
software with the reported bugs corrected is shipped as a replacement.
1. Define the external specification including the user interface and event handlers
2. Build the user interface
3. Code event handlers and write common code
4. Debug the program
5. Document the program
This step is analogous to an architect imagining then drawing pictures and plans for a
house to be built. When the architect finishes, he or she turns the plans over to a building
contractor who constructs the house. If the plans were complete and well written, the
house will come out as the architect imagined it.
Similarly the external description of a program should give enough detail that a
programmer could use it to write the program as you envisioned it.
You should prepare a written description, an external specification, of the program you
are going to write before you begin writing it. For a short program, this description may
be only one page long, but for a large program like Microsoft Word, it would be very
long and detailed.
The external specification should show the appearance of the user interface -- which
controls are on the screen and how they are laid out.
It should also specify the events that can occur -- the actions the user can take, and what
the computer should be programmed to do for each of them. (As we will see later, all
events are not caused by user action).
3. Code the event handlers. For each event you define in step 1, you must write an event
handler, a subprogram telling the computer what to do when that event occurs.
4. When you first run your program, it will not work properly. Debugging is the process
of finding and correcting your errors. In testing a program, you should give it extreme
inputs in an attempt to force errors.
Some IT managers require programmers to write their debugging test plans before
beginning to program. They assume that if the programmer does not have a clear enough
external description to do so, they do not understand the problem well enough to program
it.
5. The job is not finished when the program is working correctly. The programmer must
prepare documents describing both the external specification and the internal design of
the program. This documentation will be of value to users and programmers who must
maintain and modify your program.
Many people may work as a team if a project is large. There might be architects,
programmers, testers and documentation writers.
It may sound like you just work your way through these steps in order, but, in practice,
you will find yourself going back at times. For example, while writing event handlers,
you might decide you need to change the user interface, so you need to back up and
change the external specification.
You might be tempted to skip some of these steps when working on simple programs like
those in this class, but when working on a larger program, that would be a big mistake.
The best way to save time on a programming project is to spend a lot of time on the
external design. A well-designed program will be easy to code, debug and document. As
they say "the best way to go fast is to go slow."
Algorithm LargestNumber
Input: A non-empty list of numbers L.
Output: The largest number in the list L.
largest ← L0
for each item in the list L≥1, do
if the item > largest, then
largest ← the item
return largest
3. fact=fact*number
4. number=number-1
5. repeat step 3 to 5 if number>0
6. print the value of fact variable
c++:
int x;
cout<<"enter number :";
cin>>x;
int factorial=1;
for (int i=x; i > 0;i--)
factorial =factorial * i;
cout<<" facotorial of "<<x <<" is "<<factorial<<"\n";
return 0;
def fact(n):
if n == 0:
return 1
else:
return n * fact(n-1)
Do you see how the de_nition that refers to itself turns into a function that
calls
itself? This is called a recursive function. The function _rst checks to see if we
are at the base case n == 0 and, if so, returns 1. If we are not yet at the base
case, the function returns the result of multiplying n by the factorial of n-1.
The
latter is calculated by a recursive call to fact(n-1).
I think you will agree that this is a reasonable translation of the recursive
de_nition. The really cool part is that it actually works! We can use this
recursive
function to compute factorial values.
>>> from recfact import fact
>>> fact(4)
24
>>> fact(10)
3628800
Some beginning programmers are surprised by this result, but it follows
naturally
from the semantics for functions that we discussed way back in Chapter 6.
Remember that each call to a function starts that function anew. That means
it
has its own copy of any local values, including the values of the parameters.
Figure 13.1 shows the sequence of recursive calls that computes 5!. Note
especially
how each return value is multiplied by a value of n appropriate for each
function invocation. The values of n are stored on the way down the chain
and
then used on the way back up as the function calls return.
There are many problems for which recursion can yield an elegant and ef-
_cient solution. The next few sections present examples of recursive problem
solving.\
• 5 months ago
100% 1 Vote