Thursday, July 30, 2009

How to change c program into c++ example plz?

c program = write a program print the sum of natural number?





#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


int i=1, sum=0;


while(n%26lt;=10)


sum=sum+n;


n++;


}


printf("the sum is %d",sum);


}


please friends could you change program into c++ thankyou.........


i did "C" program i will do c++ before that i want to practice c++ and also C" plz sorry ifi did mistakes in the program which i wrote above thankssssss

How to change c program into c++ example plz?
#include%26lt;iostream.h%26gt;





int main()


{


int n=1, sum=0;


while(n%26lt;=10)


{


sum=sum+n;


n++;


}


cout %26lt;%26lt; "The sum is : " %26lt;%26lt; sum %26lt;%26lt; endl;


return 0;


}





I can't understand why you have included conio.h.





Also if you are using Unix or Linux environment,,you can write the program as follows,to prevent warnings:





#include%26lt;iostream%26gt;





using namespace std;





int main()


{


int n=1, sum=0;


while(n%26lt;=10)


{


sum=sum+n;


n++;


}


cout %26lt;%26lt; "The sum is : " %26lt;%26lt; sum;


return 0;


}
Reply:c++ adds to the c language, so what you have will already work with a c++ compiler. However, the standard for formatted text output in c++ is cout (or its companion cerr), so I would change that. I would also explicitly include the arguments to main to make it cross platform compatible.





#include %26lt;iostream%26gt;





using namespace std;





int main(int argc, char *argv[])


{


int i=1, sum=0;


while(n%26lt;=10)


sum=sum+n;


n++;


}





cout %26lt;%26lt; "the sum is " %26lt;%26lt; sum %26lt;%26lt; endl;


return 0;


}
Reply:dear c++ is classbase language.


so u had define right libraries but it can't support ,printf,scanf function it support cin%26gt;%26gt;, and cout%26lt;%26lt;.





and another thing c++ is modular programming all function has it's own duty


like printf ,scanf and more, they define as different function....
Reply:C++ is a super set of C


hence it can handle ur c program





if u want it to be true C++,


use #include%26lt;iostream.h%26gt; instead of #include%26lt;stdio.h%26gt;





then use, cin and cout instead of printf and scanf





#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;





void main()


{


int sum=0,n=0;


clrscr();


while(n%26lt;=10)


{


sum=sum+n;


n++;


}


cout%26lt;%26lt;"the sum is "%26lt;%26lt;sum;


getch();


}
Reply:#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


int i=1,sum=0;


while(n%26lt;=10)


{


sum=sum+n;


n++;


}


cout%26lt;%26lt;"The sum is "%26lt;%26lt;sum;


}











(note..u have made a mistake is braces..this will work)
Reply:#include %26lt;iostream%26gt;








The following is the usual first C++ program (hello world type) when you begin, if you compare that with the usual C one it will give an idea of some differences (of which there are many). Good luck and bon voyage!





// function main begins program execution


int main()


{


std::cout %26lt;%26lt; "Welcome to C++!\n";





return 0; // indicate that program ended successfully





} // end function main
Reply:Before changing ur program into C++ u should correct it! ur program doesn't work! u haven't use "i" at all and instead u have used "n" which is not declared anywhere! Your program should ask the user to enter desired "n" %26amp; use "i" as a counter. This is the right program:


#include%26lt;stdio.h%26gt;


void main()


{


int i=1,sum=0,n;


printf("please enter your number:");


scanf("%d",%26amp;n);


while (i%26lt;=n)


{


sum=sum+i;


i++;


}


printf("sum=%d",sum);


}


No comments:

Post a Comment