Home > Blockchain >  Add 1 to every two numbers in loop in C
Add 1 to every two numbers in loop in C

Time:11-03

Here's exactly what I need:

#include <stdio.h>
int main()
{
    int k,n,ss,s,i;
   scanf("%d", &n);
   k = n   2 * n;
    ss = k / 2;
    if (n <= 4)
      s = ss;
    else {
        
    /* ignore this above, ss is always different because of one 
    college assignment I'm working on, 
    s needs to grow for every two numbers by one */
    
    s = ss;
    int incrementVal = 1;

    for(i=5;i<n;i  )
    {
        s  = incrementVal;
        if (!(i%2))
            incrementVal  ;
            break;
    }
    
   }
    
    printf("Result: %d", s);
    
return 0;
}

So, I need this:

ss for 5 is 7, and s for 5 is ss 1=8

ss for 6 is 9, and s for 6 is ss 1=10


ss for 7 is 10, and s for 7 is ss 2=12

ss for 8 is 12, and s for 8 is ss 2=14


ss for 9 is 13, and s for 9 is ss 3=16

ss for 10 is 15, and s for 10 is ss 3=18


On this way till the end, this is hard for me to do with foor loop, do you have any idea?

*EDIT:

What I'm basicly trying to do is make shorten this bunch of if conditionals into one or two loops, and to make this if conditionals work not just from 5 do 50, but to work from 5 to n.

if (n >= 5 && n <= 6)
        s = ss   1;
      if (n >= 7 && n <= 8)
        s = ss   2;
      if (n >= 9 && n <= 10)
        s = ss   3;
      if (n >= 11 && n <= 12)
        s = ss   4;
      if (n >= 13 && n <= 14)
        s = ss   5;
      if (n >= 15 && n <= 16)
        s = ss   6;
      if (n >= 17 && n <= 18)
        s = ss   7;
      if (n >= 19 && n <= 20)
        s = ss   8;
      if (n >= 21 && n <= 22)
        s = ss   9;
        if (n >= 23 && n <= 24)
        s = ss   10;
         if (n >= 25 && n <= 26)
        s = ss   11;
         if (n >= 27 && n <= 28)
        s = ss   12;
         if (n >= 29 && n <= 30)
        s = ss   13;
         if (n >= 31 && n <= 32)
        s = ss   14;
         if (n >= 33 && n <= 34)
        s = ss   15;
         if (n >= 35 && n <= 36)
        s = ss   16;
         if (n >= 37 && n <= 38)
        s = ss   17;
        if (n >= 39 && n <= 40)
        s = ss   18;
     if (n >= 41 && n <= 42)
        s = ss   19;
        if (n >= 43 && n <= 44)
        s = ss   20;
        if(n>=44&&n<=45)
        s = ss   21;
        if (n >= 46 && n <= 48)
        s = ss 22;
        if (n >= 49 && n <= 50)
        s = ss   23;

// IN THE END
printf("Result: %d", s);

CodePudding user response:

You can use a simple mechanism to keep the incrementing value increase every 2 rounds in the loop.

Something like that:

#include <stdio.h>
int main()
{
    int s,n,i,j = 0;
    int incrementVal = 1;
    scanf("%d", &n);
    s=7;
    for(i=5;i<n;i  )
    {
        s  = incrementVal;
        j  ;
        if (j >= 2){
            incrementVal  ;
            j = 0;
        }
    }
    printf("Result: %d", s);
    return 0;
}

More compact answer would be:

#include <stdio.h>
int main()
{
    int n,i;
    int s = 7;
    int incrementVal = 1;

    scanf("%d", &n);
    for(i=5;i<n;i  )
    {
        s  = incrementVal;
        if (!(i%2))
            incrementVal  ;
    }
    printf("Result: %d", s);
    return 0;
}

CodePudding user response:

The complete code may be so:

#include <stdio.h>
int main()
{
    int s,n,i;
    scanf("%d", &n);
    for(s=7, i=5; i<=n; i  )
        s =(i i%2-4)/2;
    printf("Result: %d", s);
    return 0;
}

Edit:

My summation takes O(n) time complexity but it is trivial to write your final result in constant time by computing the closed form for your summation.

CodePudding user response:

I don't know what ss is, but according to

ss for 5 is 7, and s for 5 is ss 1=8
ss for 6 is 9, and s for 6 is ss 1=10
    
ss for 7 is 10, and s for 7 is ss 2=12
ss for 8 is 12, and s for 8 is ss 2=14
    
ss for 9 is 13, and s for 9 is ss 3=16
ss for 10 is 15, and s for 10 is ss 3=18

all you need is s = (2 * s) - 2;, no loops required.

  • Related