mercredi 11 novembre 2020

How do I make this code more efficent?This isn't working BTW

This isn't working but I thought the reasoning was right. task:The user inputs a sequence of number until the sum of two consecutives number is 10 or the number is the same of the one already written.If the first two are equal,the program should print out "you entered 2 equal numbers !" If the last 2 number of the sequence are equal the program should print out their sum.

CODE:

#include <stdio.h>
#include <stdlib.h>
int sum,i,x;

//giving it a general value cuz I don't know if writing just  num[] works
int num[100];

void main()
{
    do
    {
     printf("Enter a number:");
     scanf("%d",&num[i]);
      if(i>1)
        {
            if(num[i]+num[i+1]==10)
                sum=10;
            else if(i==2 && num[i]==num[i+1])
            {
                printf("You entered 2 equal numbers !");
                x=1;
            }
            else if(i>2 && num[i]==num[i+1])
            {
                sum=2*num[i];
                printf("sum:%d",sum);
            }
        }
     i++;
    }while(sum!=10 || x!=1);
}

Aucun commentaire:

Enregistrer un commentaire