Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 1 decade ago

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

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

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1, sum=0;

while(n<=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

8 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    #include<iostream.h>

    int main()

    {

    int n=1, sum=0;

    while(n<=10)

    {

    sum=sum+n;

    n++;

    }

    cout << "The sum is : " << sum << 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<iostream>

    using namespace std;

    int main()

    {

    int n=1, sum=0;

    while(n<=10)

    {

    sum=sum+n;

    n++;

    }

    cout << "The sum is : " << sum;

    return 0;

    }

    • Login to reply the answers
  • 1 decade ago

    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" & use "i" as a counter. This is the right program:

    #include<stdio.h>

    void main()

    {

    int i=1,sum=0,n;

    printf("please enter your number:");

    scanf("%d",&n);

    while (i<=n)

    {

    sum=sum+i;

    i++;

    }

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

    }

    • Login to reply the answers
  • Anonymous
    1 decade ago

    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 <iostream>

    using namespace std;

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

    {

    int i=1, sum=0;

    while(n<=10)

    sum=sum+n;

    n++;

    }

    cout << "the sum is " << sum << endl;

    return 0;

    }

    • Login to reply the answers
  • 1 decade ago

    C++ is a super set of C

    hence it can handle ur c program

    if u want it to be true C++,

    use #include<iostream.h> instead of #include<stdio.h>

    then use, cin and cout instead of printf and scanf

    #include<iostream.h>

    #include<conio.h>

    void main()

    {

    int sum=0,n=0;

    clrscr();

    while(n<=10)

    {

    sum=sum+n;

    n++;

    }

    cout<<"the sum is "<<sum;

    getch();

    }

    • Login to reply the answers
  • How do you think about the answers? You can sign in to vote the answer.
  • 1 decade ago

    #include <iostream>

    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 << "Welcome to C++!\n";

    return 0; // indicate that program ended successfully

    } // end function main

    Source(s): Microsoft Visual C++ v 6.0
    • Login to reply the answers
  • 1 decade ago

    #include<iostream.h>

    #include<conio.h>

    void main()

    {

    int i=1,sum=0;

    while(n<=10)

    {

    sum=sum+n;

    n++;

    }

    cout<<"The sum is "<<sum;

    }

    (note..u have made a mistake is braces..this will work)

    • Login to reply the answers
  • 1 decade ago

    dear c++ is classbase language.

    so u had define right libraries but it can't support ,printf,scanf function it support cin>>, and cout<<.

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

    like printf ,scanf and more, they define as different function....

    • Login to reply the answers
  • 5 years ago

    you will find everything here. http://bit.ly/1A3KcMZ

    • Login to reply the answers
Still have questions? Get your answers by asking now.