Exercise Set#7

on Monday, February 8, 2010
Exercise 1 --- Adding up Squares and Cubes
Write a program that adds up the squares and adds up the cubes of integers from 1 to N, where N is entered by the user:

Upper Limit: 5
The sum of Squares is 55
The sum of Cubes is 225
Do this by using just one loop.

Exercise 2 --- Power of a number
Write a program that computes XN.
XN = X * X * X * ... * X
--------------------
X: 1.3
N: 5

1.3 raised to the power 5 is: 3.71293

-------

X: 5.6
N: -3

N must be a positive integer.

Exercise 3 --- Wedge of Stars
Write a program that will write out a wedge of stars. The user will enter the initial number of stars, and the program will write out lines of stars where each line has one few star than the previous line:

Initial number of stars: 7

*******
******
*****
****
***
**
*

Exercise 4 --- Holiday Tree
Write a program that will write a tree made of stars on the terminal:

       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************


Add the following style to create monospaced characters




// put your tree here

4 comments:

ŇÃŜĮŘ ÃββÃŜ said...

Nys Blog
For More programming exercises ,visit this blog
programmingexercises

ŇÃŜĮŘ ÃββÃŜ said...

programmingexercises

Marvin Zarate said...

Exercise 1:

import java.util.*;

public class SquareCube
{
public static void main(String args[])
{
int sumOfSquares=0, sumOfCubes=0;

System.out.print("Enter maximum number: ");
Scanner input = new Scanner(System.in);
int max = input.nextInt();

for(int x = 1; x <= max; x++)
{
sumOfSquares = sumOfSquares + (x * x);
sumOfCubes = sumOfCubes + (x * x * x);
}
System.out.print("The sum of squares is: "+ sumOfSquares + "\n");
System.out.print("The sum of cubes is: "+ sumOfCubes);
}
}

Unknown said...

I'm trying to learn python and this is how i solved this. if you can comment/advice on my implementation, i would really appreciate it.

def add_sqrt(N):
n=1
sum_sqrt = 0
sum_cub = 0
while n <= N:
sum_sqrt = sum_sqrt + n*n
sum_cub= sum_cub +n*n*n
n += 1
print sum_sqrt, sum_cub


add_sqrt(2)

def com_xn(x,n):
if n <0:
print "n must be positive"

l = 1
while n > 0:
n += -1
l *= x
print l


com_xn(2,1)

def star_b(n):
while n > 0:
print "*"*n
n -= 1


star_b(5)


def star_tree():
n = 15
s=1
while n >0:
print " "*(n) , "*"*(s)
s +=2
n += -1

star_tree()





Post a Comment