0

I am trying to solve this tutorial practice question that doesn't have an answer that I can check my code against. The goal is to write a program to display numbers whose digits are 2 greater than the corresponding digits of the entered number. So if the number input is 5656 then the output number should be 7878. I have figured out how to separate each number and add them, but I can't seem to get them to print in a four-digit sequence.


#include <stdio.h>

int main ()
{
    int n, one, two, three, four, final;
    scanf("%d", &n);

    one   = (n / 1000);
    n     = (n % 1000) + 2;
    two   = (n / 100) + 2;
    n     = (n % 100) + 2;
    three = (n / 10) + 2;
    n     = (n % 10) + 2;
    four  = (n / 1) + 2;
    n     = (n % 1) + 2;

    final = (one * 1000) + (two * 100) + (three * 10) + four;

    printf("%d", final);
    return 0;
}
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
Exodus5656
  • 41
  • 1
  • 6

4 Answers4

2
#include <stdio.h>
int main()
{
     int n,a[4], final;
    scanf("%d", &n);
    for(int i=3;i>=0;i--)
    {
        a[i]=n%10+2;
        n/=10;
    }
    final = (a[0] * 1000) + (a[1] * 100) + (a[2] * 10) + a[3];
    printf("%d", final);
    return 0;
}
1

Below function works with N number of digits. Idea is to extract each digit from the input number and add its decimal position.

#include <stdio.h>

int power(int x, int y)
{
  int res = 1;
  for (;y>0;y--)
  {
    res *=x;
  }
  return res;
}
int main ()
{
    int n;
    scanf("%d", &n);
    int sum = 0;
    int i=0;
    while(n>0)
    {
       sum += ((n%10) +2)*power(10,i);
       i++;
       n /=10;
    }

    printf("%d", sum);
    return 0;

}
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
  • 2
    Ouch, `pow`, [really](https://stackoverflow.com/questions/41072787/why-is-powint-int-so-slow)? – Déjà vu Aug 02 '18 at 07:56
  • I should have put in post that arrays and statements are not allowed since they have not been covered. This is really basic stuff. Will learn to edit post. – Exodus5656 Aug 02 '18 at 08:17
  • In this answer no array is used. – kiran Biradar Aug 02 '18 at 08:18
  • 1
    Well, instead of `int i = 0; … { …something * pow(10, i); … ++i; … }`, why not a simple `int i = 1; … something * i; … i *= 10; … }`? – Bob__ Aug 02 '18 at 08:26
0

Another idea:

char str[10]; // enough to contain an int as string + 1
char *s = str+sizeof(str); // points to last char + 1
int n;

scanf("%d", &n);
*--s = 0;  // terminate the string

while(n) {
    *--s = (((n % 10)+2)%10) + '0'; // write a char from the end
    n /= 10;
}
printf("%s\n", s);
Déjà vu
  • 28,223
  • 6
  • 72
  • 100
-1
    int a = 5696;
    //      output is 7818
    // ---------Java-----------
    // --------solution--------



    int first  = a/1000+2;
    
    int b = a%1000;
    int second = b/100+2;
    
    int c = b%100;
    int d = c/10+2;
    int third = d/10;
    
    int e = c%10;
    int fourth = e+2;
    
    String result = Integer.toString(first)+Integer.toString(second)+Integer.toString(third)+Integer.toString(fourth);
            
    System.out.println(result);
Josep Valls
  • 5,483
  • 2
  • 33
  • 67
  • Only code answers are generally poor answers. On top of that, the usefulness of a Java version for this question seems questionable. I suggest you to edit your answer in a way highlighting at least the Java peculiars here. – Zilog80 Aug 10 '21 at 09:13