Thread: Add two numbers

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Delhi,India
    Posts
    5

    Question Add two numbers

    I am writing a program to add two numbers. But instead of adding, the result is some thing else like z=0.0000 or z=4209163 while my two numbers to be added are 10 and 20. Please help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Show your program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Delhi,India
    Posts
    5

    Unhappy Add two numbers

    My program is:

    Code:
    #include<stdio.h> 
    #include<conio.h> 
    void main() 
    { 
    float n1,n2,ans; 
    printf("Enter the First number\n"); 
    scanf("%f",&n1); 
    printf("Enter the Second number\n"); 
    scanf("%f",&n2); 
    ans=n1 + n2; 
    printf("Your Answer is -: %.3f",&ans); 
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Drop the & from the printf.
    main returns int, not void.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector vs. array.
    By matsp in forum C++ Programming
    Replies: 37
    Last Post: 06-23-2008, 12:41 PM
  2. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Replies: 3
    Last Post: 02-25-2004, 05:59 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM