Saturday 15 October 2011

(m) If a five-digit number is input through the keyboard, write a program to print a new number by adding one to each of its digits. For example if the number that is input is 12391 then the output should be displayed as 23402.

/* This program takes a five-digit integer from user, and shows its reverse.*/
#include<stdio.h>
#include<conio.h>
int main ()
{
/*Declaring and initializing variables*/
    int value,dig1,dig2,dig3,dig4,dig5,temp;

/*prompt to take the input*/
    printf("Please enter a five-digit number: ");

/*Taking input*/  
    scanf("%d",&value);
    temp = value ;

/*Calculations*/
    //separating digits 
    dig1 = value % 10;
    value = value / 10;
    dig2 = value % 10;
    value = value / 10;
    dig3 = value % 10;
    value = value / 10;
    dig4 = value % 10;
    value = value / 10;
    dig5 = value % 10;
    
    //adding 1 to each digit
    dig1 = ((dig1+1)%10);
    dig2 = ((dig2+1)%10);
    dig3 = ((dig3+1)%10);
    dig4 = ((dig4+1)%10);
    dig5 = ((dig5+1)%10);

/*showing Results*/  
    printf("\n   Solution: ");
    printf("\n   __________");
    printf("\n\n   By adding one in each digit of %d it becomes : %d%d%d%d%d",temp,dig5,dig4,dig3,dig2,dig1);
   
    getch();
}

  

  


18 comments:

  1. ADD ONE AT EVERY POSITION
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace probable
    {
    class Program
    {
    static void Main(string[] args)
    {
    int[] a = new int[5];
    int value, ans;
    Console.WriteLine("ENTER 5-DIGIT NUMBER");

    value = int.Parse(Console.ReadLine());
    ans = value;
    for (int i = 0; i < 5; i++)
    {
    a[i] = value % 10;
    value = value / 10;
    }
    for (int j = 0; j < 5; j++)
    {
    a[j] = ((a[j] + 1) % 10);
    }
    Console.WriteLine("SOLUTION OF {0} IS: ",ans);

    for (int k = 4; k >=0; k--)
    {
    Console.Write(a[k]);
    }


    Console.ReadKey();
    }
    }
    }

    ReplyDelete
  2. #include"stdio.h"
    #include"conio.h"
    void main()
    {
    long int a,r,add=0,rev=0;
    clrscr();
    printf("Please enter any five digits number");
    scanf("%ld",&a);
    while(a>0)
    {
    r=a%10;
    a=a/10;
    rev=rev*10+r;
    }
    while(rev>0)
    {
    r=rev%10;
    rev=rev/10;
    r=r+1;
    add=add*10+r;
    }
    printf("%ld",add);
    getch();
    }
    SAPTADIP SEN

    ReplyDelete
  3. #include"stdio.h"
    #include"conio.h"
    void main()
    {
    long int a,r,add=0,rev=0;
    clrscr();
    printf("Please enter any five digits number");
    scanf("%ld",&a);
    while(a>0)
    {
    r=a%10;
    a=a/10;
    rev=rev*10+r;
    }
    while(rev>0)
    {
    r=rev%10;
    rev=rev/10;
    r=r+1;
    add=add*10+r;
    }
    printf("%ld",add);
    getch();
    }
    SAPTADIP SEN

    ReplyDelete
  4. EASIEST WAY
    #include
    int main()
    {
    int a,b,c,d,e;
    printf("Enter 5numbers=");
    scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
    a++;
    b++;
    c++;
    d++;
    e++;
    printf("%d%d%d%d%d",a,b,c,d,e);
    return 0;
    }

    leave space after each digit when you type in output :)

    ReplyDelete
    Replies
    1. well, your program does not account for 9 .
      for example if user enters 99999, it will return 1010101010
      but it should have returned 00000

      Delete
  5. A]
    What would be the output of the following programs:
    (a)
    main( )
    {
    int a = 300, b, c ;
    if ( a >= 400 )
    b = 300 ;
    c = 200 ;
    printf ( "\n%d %d", b, c ) ;
    }

    find an output and post the explanation

    ReplyDelete
    Replies
    1. a=300 and 300 is not equal >= 400 then b would not be initialize to 300 and c will be.so the output would be
      b as garbage and c = 200

      Delete
  6. #include
    #include
    int main()
    {
    int n,rem,i=1,sum=0;
    printf("Enter a Number:");
    scanf("%d",&n);
    while(n!=0)
    {
    rem=n%10;
    sum=sum+(rem+1)%10*i;
    i=i*10;
    n=n/10;
    }
    printf("%d",sum);
    }

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. #include
    int main()
    {
    int value,sum;
    printf("enter the value");
    scanf("%d",&value);
    sum=value+11111;
    printf("the sum is:%d",sum);
    return 0;
    }

    ReplyDelete
  10. #include
    int new_num =0;
    int rem;

    void func(int num,int n){
    rem = num%10;
    if(rem==9){
    }
    else{
    new_num += n*(rem+1);
    }
    printf("rem = %d, new_num = %d\n",rem,new_num);
    num = num/10;
    if(num==0){
    printf("New number: %d",new_num);
    }
    else{
    func(num,10*n);
    }
    }

    int main(){
    int number = 12391;
    func(number,1);
    }

    ReplyDelete
  11. #include
    int main(){
    int in,a=0,p;
    printf("Enter a 5 digit number");
    scanf("%d",&in);
    p=in;
    a=((in%10)+1)+a;
    in = in/10;
    a=((in%10)+1)*10+a;
    in=in/10;
    a=((in%10)+1)*100+a;
    in=in/10;
    a=((in%10)+1)*1000+a;
    in=in/10;
    a=((in%10)+1)*10000+a;
    printf("%d is the output after adding 1 to all the digits of %d",a,p);
    return 0 ;
    }

    ReplyDelete
  12. dig1 = ((dig1+1)%10); please can you explain why we use %10

    ReplyDelete
  13. #include


    int main()
    {
    int a,b,c,d,e,f;
    printf("Enter a five digit number : ");
    scanf("%d",&a);
    b = (((a - a%10000)/10000)+1)%10 ;
    c = (((a%10000 - a%1000)/1000)+1)%10 ;
    d = (((a%1000 - a%100)/100)+1)%10 ;
    e = (((a%100 - a%10)/10)+1)%10 ;
    f = ((a%10) +1)%10 ;
    printf("%d" , b*10000 + c*1000 + d*100 + e*10 + f );

    }

    ReplyDelete
  14. ```/*If a five-digit number is input through the keyboard, write a
    program to print a new number by adding one to each of its
    digits. For example if the number that is input is 12391 then
    the output should be displayed as 23402.*/
    #include
    #include
    int main(int argc, char const *argv[])
    {
    int a,b,d=0,e=0;
    printf("Enter the number:");
    scanf("%d",&a);
    while(a!=0)
    {
    b=a%10;
    while(b==9)
    {
    b=-1;
    }
    d=d+((b+1)*pow(10,e));
    a=a/10;
    e=e+1;
    }
    printf("%d",d);
    return 0;
    }
    ```

    ReplyDelete
  15. #include

    int main()
    {
    int num,a,b,c,d,e,n,m,o,p,q,r;

    printf("\n printing the number after adding one to each of its dight is:");
    scanf("%d",&num");

    a=num%10;
    n=num/10;
    m=(a+1);

    b=num%10;
    n=num/10;
    o=(b+1);

    c=num%10;
    n=num/10;
    p=(c+1);

    d=num%10;
    n=num/10;
    q=(d+1);

    e=num%10;
    n=num/10;
    r=(e+1);

    printf("\n %d %d %d %d %d", r,q,p,o,m);

    return 0;

    }




    /* what I'm doing wrong in this ..variables are storing last digits and at last by adding one to each of the digit number should be print reversily

    ReplyDelete