0

Hello fairly new at coding, i have this program which should add a number each time the program is run, it computes 1 + 2 + 3 +......(n - 1) + n and an if statement that compares the value to (n*(n+1))/2 and then displays a message if its the same or different.

#include <stdio.h>

int main(void)
{

  int value = 0, n_data_value, count = 0, second_value;

  printf("Enter a number\n");
  scanf("%lf", &n_data_value);

  while (count < 10) {
   value = (1 + value) + (n_data_value - 1) + n_data_value;
   printf("%f\n", value);
   second_value = (n_data_value * (n_data_value + 1))/2;
   printf("The number is %f\n", value);
   count = count + 1;

   if(value = second_value){
     printf("value = %f and second_value = %f", value, second_value);
   }
  } 
 return 0;
}

When i run it i just get this

Enter a number 3

0.000000

The number is 0.000000

0.000000

The number is 0.000000

0.000000

The number is 0.000000

0.000000

The number is 0.000000

0.000000

The number is 0.000000

0.000000

The number is 0.000000

0.000000

The number is 0.000000

0.000000

The number is 0.000000

0.000000

The number is 0.000000

0.000000

The number is 0.000000

1
  • When you get your program working and fix your loop logic and maths, you'll find a disparity between the formula and your looped value after N=65535. See stackoverflow.com/questions/12923523/… Commented Oct 29, 2012 at 13:26

4 Answers 4

1

Use %d with int variables in scanf/printf.

%lf is for double values.

Sign up to request clarification or add additional context in comments.

Comments

1

The test for value matching second_value is also wrong. Use == to test for equality

i.e. change

if(value = second_value){

to

if(value == second_value){

Note that compiling with warnings enabled (-Wall for gcc, /W4 for msvc) would have flagged this up for you.

2 Comments

Hmm how do i enable warnings in the compiler? just add -wall or? :)
If you're compiling from the command line, add -Wall for gcc or /W4 for msvc. If you're using an IDE, there will be a build settings dialog which accepts compiler flags
0

If you want to do it more than 1 time : just add something like that :

#include <stdio.h>

int main(void)
{

   int value = 0, n_data_value, count = 1, second_value;
   char char_quit='';

   do //Infinite loop
   {

      printf("Enter a number\n");
      scanf("%d", &n_data_value);

      while (count <= n_data_value) {//while loop to calculate 1+2+3+...+n
        value = value + count;
        count = count + 1;
      }
     second_value = (n_data_value * (n_data_value + 1))/2;//calculates n(n+1)/2
     printf("The number is %d\n", second_value);

     if(value == second_value)//compares value
     {
        printf("value = %d and second_value = %d", value, second_value);
     } 

     //Ask to continue ?
     printf("Do you want to continue ? Y/N \n"); //Y or any other key will force to continue the program
     scanf("%c", &char_quit );

  }while (char_quit == 'n' || char_quit == 'N');

  return 0;
}

You could naturally use something like : while(1) insteand of the "do while" if you want an infinite loop.

Comments

0

I think you just need this

#include <stdio.h>

int main(void)
{

  int value = 0, n_data_value, count = 1, second_value;

  printf("Enter a number\n");
  scanf("%d", &n_data_value);

  while (count <= n_data_value) {//while loop to calculate 1+2+3+...+n
   value = value + count;
   count = count + 1;
  }
  second_value = (n_data_value * (n_data_value + 1))/2;//calculates n(n+1)/2
  printf("The number is %d\n", second_value);

 if(value == second_value){//compares value
     printf("value = %d and second_value = %d", value, second_value);
   } 
 return 0;
}

1 Comment

Yes that works, but what if i wanted to run the program more times? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.