In: Computer Science
write code with proper comments for ever step the code should be in form of pseudocode
To Print Triangle of any size, by taking size as
input.
To Print Triangle of any size, by taking size as input. If size is
4 then triangle is:
To calculate Factorial of any number.
To calculate the
power of any given number.
To Print Table of Any Number up till 10: as shown in this
figure.
for a program which reads 10 integers from the user, and output the
largest value
from input numbers.
Triangle:
for (int i = 1; i <= n; i++) {
// loop to print the number of spaces before the star
for (int j = rows; j >= i; j--) {
cout<<" ";
}
// loop to print the number of stars in each row
for (int j = 1; j <= i; j++) {
cout<<"*";
}
//new line
}
Factorial:
int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
Power:
int power(int a, int b)
{
if (b == 0)
return 1;
else if (b % 2 == 0)
return power(a, b / 2) * power(a, b / 2);
else
return a* power(a, b / 2) * power(a, b / 2);
}
Table:
int n = 5; // Take any input number
for (int i = 1; i <= 10; ++i)
cout << n << " * " << i << " = " << n * i << endl;
Largest:
int a[10];
int i;
int greatest;
printf("Enter ten values:");
//Store 10 numbers in an array
for (i = 0; i < 10; i++)
{ scanf("%d", &a[i]); }
//Assume that a[0] is greatest
greatest = a[0];
for (i = 0; i < 10; i++) {
if (a[i] > greatest)
{ greatest = a[i]; }
}
At the end greatest will have the largest number