Read an integer number from the user. If the number is not positive, change its sign. Create the algorithms for the following tasks: 1 Count the digits of the number 2 Count the odd digits of the number 3 Calculate the sum of the digit values of the number

Respuesta :

Answer:

ALGORITHM READ_NUMBER()

1.       SET NUM := 0 [INITIALLY SET THE VALUE OF NUM TO 0]

2.       WRITE : “Input a Number” [DISPLAY A MESSAGETO USER]

3. READ NUM [STORE THE VALUE TO NUM]

4.       IF NUM<0 [IF VALUE OF NUM IS -VE THEN MULTIPLY IT BY -1]

5.       NUM := NUM * (-1)

6.       [END OF IF]

7.       RETURN NUM [RETURN THE NUMBER]

1. ALGORITHM DIGIT_COUNT(NUM)

SET COUNT := 0

SET R:=0

REPEAT WHILE(NUM != 0)

R := NUM MOD 10

COUNT : = COUNT+1

NUM := NUM/10

[END OF LOOP]

RETURN COUNT

2. ALGORITHM DIGIT_ODDCOUNT(NUM)

SET COUNT := 0

SET R:=0

REPEAT WHILE(NUM != 0)

R := NUM MOD 10

IF (R MOD 2 != 0)

COUNT : = COUNT+1

[END OF IF]

NUM := NUM/10

[END OF LOOP]

RETURN COUNT

3. ALGORITHM SUM_OF_DIGITS(NUM)

SET SUM := 0

SET R:=0

REPEAT WHILE(NUM != 0)

R := NUM MOD 10

SUM : = SUM+R

NUM := NUM/10

[END OF LOOP]

RETURN SUM

C++ PROGRAM

#include<iostream>

using namespace std;

//method to count the number of digits

int digit_count(int num)

{

   int count=0; //set count to 0

   int r;

   //loop will continue till number !=0

   while(num!=0)

   {

      r=num%10; //find the individual digits

      count++; //increase the count

      num=num/10; //reduce the number

  }

  return count; //return the count

}

int odd_digit_count(int num)

{

   int count=0;//set count to 0

   int r;

   //loop will continue till number !=0

   while(num!=0)

   {

      r=num%10;//find the individual digits

      if(r%2!=0) //check for odd digits

      count++;//increase the count

      num=num/10;//reduce the number

  }

  return count;//return the count

}

int sum_of_digits(int num)

{

   int sum=0;

   int r;

   //loop will continue till number !=0

   while(num!=0)

   {

      r=num%10;//find the individual digits

      sum=sum+r;//find the sum of digits

      num=num/10;//reduce the number

  }

  return sum;//return sum

}

//input method

int input()

{

  int x;

  cout<<endl<<"Enter a number";

  cin>>x;//read the number

  if(x<0) //if -ve then turn it to +ve

  x=x*-1;

  return x; //return the number

}

//driver program

int main()

{

  int n,dc,odc,sod;

  n=input(); //call to input()

  dc=digit_count(n); //call to digit_count()

  odc=odd_digit_count(n); //call to odd_digit_count()

  sod=sum_of_digits(n);//call to sum_of_digits()

  //display the details

  cout<<endl<<"The positive number is : "<<n;

  cout<<endl<<"The number of digits in "<<n<<" is "<<dc;

  cout<<endl<<"The number of odd digits in "<<n<<" is "<<odc;

  cout<<endl<<"The sum of digits of "<<n<<" is "<<sod;

}

Explanation: