Write a program that reads in from the user an integer (num) between 1000 and 9999. Then it prompts the user to enter an integer (d) between 0 and 9 and a character (ch). Your program should replace the second and the last digit in num with d and it should display the character that precedes (ch) followed by the number after the change and then the character that comes after (ch). Use the division and modulus operators to extract the digits from num. Sample run: Enter an integer between 1000 and 9999: 2134 Enter a digit (between 0 and 9): 6 Enter a character: b Number was 2134. Result: a2636c.
package question2;
import java.util.Scanner;
public class Question2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer between 1000 and 9999: ");
int num = input.nextInt();
System.out.println("Enter a digit (between 0 and 9): ");
int d = input.nextInt();
System.out.println("Enter a character: ");
double ch = input.nextDouble();
}
}
|
Take a look at how strings work. When modifying certain digits of something this is the easiest way to go.
If you read a bit about how to use them and what you can do with them youll probably find the answer quickly. About your code: in the last line youre trying to get a double from the input. A double is just like a float but with higher precision. Its certainly not a String or (char)acter and so cant hold the character you entered, so you have to use a different datatype there. |
num = num - ( ( num / 100 % 10 ) * 100 ) - ( num % 10 ) + d * 100 + d; cout << (char)(ch-1) << num << (char)(ch+1);
|
so at the last line i have to change it to
Char ch = input.nextChar(); Still i need to figure out how to replace 2nd and 4th in num and replace it with integer d. As well display the precedent and the letter that comes after ch before&after the number. |

)
package question2;
import java.util.Scanner;
public class Question2 {
void main() {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer between 1000 and 9999: ");
int num = input.nextInt();
System.out.println("Enter a digit (between 0 and 9): ");
int d = input.nextInt();
System.out.println("Enter a character: ");
char ch = input.nextLine.charAt(0);//thats how u enter a char
String result = (ch-1);//it stores the final result
int r,c = 1,f=num;
while(num != 0)
{
r = num % 10;
num = num/10;
if(c==2 || c==4)result+=d;
else result+=r;
c++;
}
result+= (ch+1);
System.out.println("Number was "+f+". Result : "+result);
}
}
|
I program in java so here is solution -
Код:
package question2;
import java.util.Scanner;
public class Question2 {
void main() {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer between 1000 and 9999: ");
int num = input.nextInt();
System.out.println("Enter a digit (between 0 and 9): ");
int d = input.nextInt();
System.out.println("Enter a character: ");
char ch = input.nextLine.charAt(0);//thats how u enter a char
String result = (ch-1);//it stores the final result
int r,c = 1,f=num;
while(num != 0)
{
r = num % 10;
num = num/10;
if(c==2 || c==4)result+=d;
else result+=r;
c++;
}
result+= (ch+1);
System.out.println("Number was "+f+". Result : "+result);
}
}
But should work... |
|
There must be some little error but it must work i am sure...
And string cannot be modified but can be concated So i have made a string which is concated... And for giving him direct solution I am sorry for dat.. |