Add two numbers

This is a discussion on Add two numbers within the C Programming forums, part of the General Programming Boards category; I am writing a program to add two numbers. But instead of adding, the result is some thing else like ...

  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
    21,931
    Show your program.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 06: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, 04:59 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21