954,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Numbers in Turbo C++

I got a question which i cannot do. Pls help me.
(This is using Turbo C++ IDE).
The questions are:

Q1. WAP to print the sum of the following series:
2(square) + {2(square) + 4(square)} + {2(square) + 4(square) + 6(square)} .............. till N terms.

Q2. WAP to find out the no. of digits in the given number.

Tejas
Newbie Poster
6 posts since Jun 2004
Reputation Points: 11
Solved Threads: 0
 

Greetings.
Err, sorry for my ignorance, but what does WAP mean?

red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

WAP stands for Write A Program I belive.Teachers commonly use that when they write on a board.

About you second question:
It easy enough if you think logically.How do you find the number of digits in a number,by counting ofcourse.Now how do you get a computer to count?
That can be done by removing digits one by one and that can be done by dividing the number by ten.Simple eh?

while(num > 0)
{
   num = num/10;
   no_of_digits++
}


Very simple.

I could not really understand your first question.

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 

Greetings.
I see. I often see my lecturers using shortforms like "hth" & etc. without knowing what it means. ;)

Your suggestion to the second problem was cool.
How about converting the number to String and then using the string length function? ;)

About the first problem. May I rephrase what you were trying to mean.
Let's say if I input n=1, must I get 2^2 = 4 as the result?
Similarly, if I input n=2, the result must be 2^2 + 4^2 = 20.
Is this what you want?

red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

For those who didnt understand the first part of the problem, it simply says:
It sholud print the following series:
2^ + [ 2^ + 4^ ] + [ 2^ + 4^ + 6^ ] + [ 2^ + 4^ + 6^ + 8^ ] and so on.

By the way, WAP is a short form for write a prog. :idea: !!

Tejas
Newbie Poster
6 posts since Jun 2004
Reputation Points: 11
Solved Threads: 0
 

I think i have replied to ur doubt.

Tejas
Newbie Poster
6 posts since Jun 2004
Reputation Points: 11
Solved Threads: 0
 

Greetings.
Errr, I still don't really get what you mean.
However, could you check if it's something like this?
int square(int n)
{[INDENT]int result=0;[/INDENT][INDENT]for(int i=1; i<=n; i++)[/INDENT][INDENT]{[/INDENT][INDENT][INDENT]result += pow(2*i, 2);[/INDENT][/INDENT][INDENT]}[/INDENT][INDENT]return result;[/INDENT]}

where in this case, you have to prompt the user for an input for "n".

red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

Red_evolve... i've written a version for Q1 myself and it's pretty similar to yours (mine's a program---in my opinion you understood pretty good)

#include
#include
long elemget(int n)
{
long val=0;
for (int j=1;j<=n;j++)
val+=(j*2)*(j*2);
return val;
}
void main()
{
clrscr();
int n;
printf("Numarul de termeni:");scanf("%i",&n);
unsigned long sum=0;
for (int i=1;i

Fili
Light Poster
34 posts since Jun 2004
Reputation Points: 16
Solved Threads: 0
 

by the way I am also interested in computing this series, but i want to do it using a recursive function. Can anyone help me out?

Asif_NSU
Posting Whiz
353 posts since Apr 2004
Reputation Points: 113
Solved Threads: 3
 

Greetings.
To Fili:- Hey, thanks. :D
By the way, what language are you using?
Does "Numarul de termeni" mean "Please input something".
LOL. Just a wild guess.

To Asif_NSU:- I am not sure whether this will work as I've not debugged it.
(This is not a complete program though. :D)

long square(int n)
{
  if(n=1)
    return 4;
  else
    return ( pow(2*n,2) + square(n-1) );
}

void main()
{
  int input = 0;
  cout<<"Please enter an integer: ";
  cin>>input;
  cout<<"The result is "<<square(input)<<endl;
}
red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

Thanx. The code works... I just had to add the header files
and i had to change the if(n=1) into if (n==1)...a slip of the finger i guess.

Asif_NSU
Posting Whiz
353 posts since Apr 2004
Reputation Points: 113
Solved Threads: 3
 

woops i'm from romania. "Numarul de termeni:" means how many elements you will input and "Suma obtinuta" means the sum you have obtained. My mistake! :o

Fili
Light Poster
34 posts since Jun 2004
Reputation Points: 16
Solved Threads: 0
 

Greetings.
To Asif-NSU,
Yeah, my mistake. Sigh...

red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

i need the C++ program for sum of series usinf loop
2^2+4^2+........+n^2=2/3n(n+1)(2n+1)

prasanaharani
Newbie Poster
1 post since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Resurrecting a 5 year old thread to ask us to give you a free grade is frowned upon.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You