Question

How to write this code in C++???? using fstream Write a program which: Asks the user...

How to write this code in C++????

using fstream

Write a program which:

Asks the user to enter a positive integer greater than 0

Validates that the entry is a positive integer

Outputs the digits in reverse order with a space separating the digits

Outputs the even digits not in reverse order with a space separating the digits (consider zero to be even)

If there are no even digits, the an appropriate message should be displayed: There are no even digits

Outputs the odd digits not in reverse order with a space separating the digits

If there are no odd digits, the an appropriate message should be displayed: There are no odd digits

Allows user is to repeat/continue the program as many times as he/she wants

Keeps a record in a txt file named outDataFile.txt with the history of all numbers entered and the associated results, in the following format:

the original number is 1234
the number reversed 4 3 2 1
the even digits are 2 4
the odd digits are 1 3
-----------------
the original number is 135
the number reversed 5 3 1
There are no even digits
the odd digits are 1 3 5
-----------------
the original number is 862
the number reversed 2 6 8
the even digits are 8 6 2
There are no odd digits
-----------------

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;

int main()
{
char s[1000000];
int k=0,l=0;
cout<<"enter the no. greater than 0"<<endl;
gets(s);
int n=strlen(s);
cout<<"the reversed digits";
for(int i=n-1;i>=0;i--)
{
cout<<s[i]<<" ";

}
cout<<endl;
for(int j=0;j<n;j++)

{
if(s[j]%2==0)
k++;
else
l++;

}
if (k==0)
cout<<"there are no even numbers";
else
{

cout<<"the even digits are ";
for(int j=0;j<n;j++)

{
if(s[j]%2==0)
cout<<s[j]<<" ";
}
cout<<endl;
}
if (l==0)
cout<<"there are no odd numbers ";
else
{
cout<<"the odd digits are ";
for(int j=0;j<n;j++)
{
if(s[j]%2!=0)
cout<<s[j]<<" ";
}

}
}

output

enter the no. greater than 0
12345
the reversed digits5 4 3 2 1
the even digits are 2 4
the odd digits are 1 3 5

Add a comment
Know the answer?
Add Answer to:
How to write this code in C++???? using fstream Write a program which: Asks the user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • use C++ please 1. Vowels and Consonants Write a program that asks the user to input...

    use C++ please 1. Vowels and Consonants Write a program that asks the user to input three different integers. Write a function called numberStyle for this program that will accept each integer (one at a time) and return the following . If the integer is even, return 1 . If the integer is odd, return -1 . If the integer is zero, return O In main, after calling the function output the appropriate message describing the integers ing the function...

  • Write a c++ complete program to meet the specifications. The program should prompt the user for...

    Write a c++ complete program to meet the specifications. The program should prompt the user for a positive integer. The program should print a message whether the integer is even or odd. The looping should end when the user enters a negative number. The negative number will not be tested for even or odd. The program will print out a message of how many numbers were entered (not counting the negative number) and how many even and odd numbers were...

  • C++ Problem #1 Write a program that prompts user to enter an integer up to 1000...

    C++ Problem #1 Write a program that prompts user to enter an integer up to 1000 and displays back a message about the number of digits in this number and if number is odd or even. For example, if user enters 93 message back should be "93 has 2 digits and is odd".

  • I need to revise a c++ code that asks to: enter up to 5 digits positive...

    I need to revise a c++ code that asks to: enter up to 5 digits positive integer number, then reverse the order of the digits. The program outputs both numbers and calculates their square roots in the following way Please enter a 5-digits integer: ( inputs 56789) output as follow The square root of 566789 is: The number in reverse is: 98765 The square root of the reversed number 98765 is:

  • Need help answering the following using java 2a. Write a java program (just a “void main”)...

    Need help answering the following using java 2a. Write a java program (just a “void main”) that will reverse the digits of a user entered, positive three-digit number. You can assume the user enters an integer. Your code segment should first verify that the user has entered a number from 100 to 999 (error message if not). If the user has entered a three-digit number, output a new number with the original digits reversed. 2b. Instead of being limited to...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • NASM ASSEMBLY WINDOWS DOSBOX: 5) Write assembly program that asks the user to enter a number...

    NASM ASSEMBLY WINDOWS DOSBOX: 5) Write assembly program that asks the user to enter a number ( the user should input any integer number, say N), then the program outputs N if N is even; or N+3 if N is odd ( note: the output is always even) Example: Enter any number: 25 Even output is: 28 Another example: Enter any number: 32 Even output is: 32.

  • Write a program in Python that asks the user for the ISBN of the book in...

    Write a program in Python that asks the user for the ISBN of the book in the format xxx-x-xxx-xxxxx-x. To validate: Initialize a total to 0 Have the program remove the dashes so that only the 13 digits are left in the ISBN string for each index of the first 12 digits in the 13 digit ISBN string get the digit at the current index as an integer If the current index is an even number, add the digit to...

  • Write code that prompts the user to input a number. The program should then output the...

    Write code that prompts the user to input a number. The program should then output the number and a message saying whether the number i "Positive, "Negative" or "Zero". It should further output whether the number is odd or even (zero is counted as even) For example:

  • Write a python program that does the following. a. It asks the user to enter a...

    Write a python program that does the following. a. It asks the user to enter a 5-digit integer value, n. b. The program reads a value entered by the user. If the value is not in the right range, the program should terminate. c. The program calculates and stores the 5 individual digits of n. d. The program outputs a “bar code” made of 5 lines of stars that represent the digits of the number n. For example, the following...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT