I am new to c++ language, and i am stucked with this que. my teacher told me to write a prog. that will calculate
(1)square-(3)square+(5)square-(7)square+(9)square.............so on using for loop can you help me.

Recommended Answers

All 5 Replies

Do you perhaps wanna post your attempt (according to forum rules, of which I am unaware) so we can see where you are going wrong?

Please provide evidence of having done some work yourself if posting questions from school or work assignments.

//factorial prog.
#include<iostream.h>
#include<conio.h>
void main ()
{
 clrscr ();
 int fact,number;
 fact=1;
 cout<<"Enter number";
 cin>>number;
 for(int i=1;i<=number;i++)
 {
  fact=fact*i;
 }
 cout<<"Factorial="<<fact<<endl;
 getch();
}

//fibonacci series
#include<iostream.h>
#include<conio.h>
void main ()
{
 clrscr ();
 int f0,f1,f,n;
 f0=1;
 f1=1;
 cout<<"Fibonacci series:"<<endl;
 cout<<f0<<endl;
 cout<<f1<<endl;
 for(int 1=2;i<15;i++)
 {
  f=f0+f1;
  cout<<f<<endl;
  f0=f1;
  f1=1;
  }
  getch();
 } 

And what exactly does a factorial or fibonacci calculation have to do with the problem you are asked to solve?

Can you try to solve the problem that you are asked to solve?

OK, we can see that you have these two programs written, but what have you done about the current assignment?

BTW, three things to take note of. First, you should always declare main() as type int; while void main() is accepted by some older, non-standard compilers, it is not actually correct according to the C++ standard, and never has been acording to Bjarne Stroustrup. Second, in modern C++, the header should be <iostream>, not <iostream.h>; however, since it appears you are using the ancient Turbo C++ compiler, and probably don't have a choice in the matter, you can only do what you are doing for the time being. Third, you use the non-standard <conio.h> library, which is not part of the actual C++ language and is not supported by modern systems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.