2 Replies - 368 Views - Last Post: 12 May 2013 - 09:31 PM Rate Topic: -----

#1 saeee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-May 13

Problem

Posted 12 May 2013 - 02:23 AM

Hi, i am new to c language. please correct this.
i want the sum of first n numbers, and also prompt for another try

 
#include<stdio.h>
#include<ctype.h>
main ()
{
    for (;;)/>
    {
        int sum = 0;
        char answer='n';
        int number;
        printf("\n Enter the number :");
        scanf("%d", &number);
        for (int i=1; i<=number; i=i+1)
        {
            sum=sum+i;
        }
        printf("\n the requested number %d , and the sum is %d ", number , sum );
        printf("\n Wanna another try /n type Y or N :");
        scanf("%c", &answer);
        if (tolower(answer)=='n')
            break;
    }
    return 0;
}



sorry for bad english.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: Problem

Posted 12 May 2013 - 02:49 AM

You don't say what the problem is, but try making this

scanf("%c", &answer);


into this (notice the space)

scanf(" %c", &answer);


If that doesn't fix the problem, then please TELL US what the problem is.
Was This Post Helpful? 1
  • +
  • -

#3 saeee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-May 13

Re: Problem

Posted 12 May 2013 - 09:31 PM

thanks a lot.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1