Thread: Need help with my Very newbie code

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    2

    Question Need help with my Very newbie code

    Hey guys, i've just started learning c, and i'm writing one of my first programs. I'm having some problems running it, though. Here is the code :

    Code:
    #include <stdio.h>
        int main ()
        {
        int soma,n,nf,nz;
        soma = 0;
        n = 0;
         printf ("Digite o primeiro termo da sequência");
         scanf("%d", &nz);
         printf ("digite o termo final da sequência");
         scanf("%d", &nf);
         printf ("o programa calculara a soma de todos os termos dentro da sequência/n");
         while (nz+n<=nf) {
            soma = soma + nz + n ;
            n=n+1 ;
         }
         printf("%d",soma);
         return 0 ;
    
    
         }
    Here's a quick tutorial of what it does (the text is in portuguese, i forgot to translate) : First i type the first number of a sequence, then, i type the last one, and then the program should sum all of the numbers within the sequence, including the first and last number. (i've tested on paper and so far it works with positive integers).
    My problem is that the program doesn't print "soma" ( soma = sum) after the while loop ends. How can i fix it?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,713
    Here's what I get.
    Code:
    $ gcc -Wall foo.c
    $ ./a.out 
    Digite o primeiro termo da sequência10
    digite o termo final da sequência200
    o programa calculara a soma de todos os termos dentro da sequência/n20055$
    First, there is a difference between "/n" and "\n".

    Second, your final printf should really be followed by a \n as well to guarantee you see the last printf on the terminal.

    But if you have the problem of the console window disappearing after the last input, then read FAQ > Stop my Windows Console from disappearing everytime I run my program? - Cprogramming.com
    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.

  3. #3
    Registered User
    Join Date
    Aug 2016
    Posts
    2
    Jeez... It was working all along, i just couldn't see it hahah. Thx for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help for this Newbie Code
    By khmerkid in forum C Programming
    Replies: 7
    Last Post: 12-25-2011, 11:02 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. OMG just STFU and HELP a NEWBIE with HIS CODE !!!!!!!!11
    By Dual-Catfish in forum Party Board
    Replies: 6
    Last Post: 07-14-2003, 07:09 AM
  4. Newbie - How do I know if I'm writing C code?
    By dfinner in forum C Programming
    Replies: 4
    Last Post: 07-31-2002, 04:17 PM
  5. newbie here.....need someone to look at my code
    By aml822 in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2001, 10:40 PM

Tags for this Thread