mardi 16 novembre 2021

Changing the order of printing from right to left to left to right | C programming

Write a program that asks the user to enter the number n and the ordinal number of the digit c. The number n can be in the (exclusive) interval (-1000000, 1000000) (if this is not the case, the program prints Incorrect entry! And ends with the work), while the ordinal number of the digit can be from 1 to the number of digits of the entered number n (if this is not the case) the program prints Invalid digit! and terminates). The ordinal number of the digit is determined from left to right, ie if the number is n = 13546, and the ordinal number of the digit is c = 2, then 2 refers to the second digit from the left, ie the digit 3. The program should extract the digit with the ordinal number c from the number n, and print it and calculate its square.Write a program that asks the user to enter the number n and the ordinal number of the digit c. The number n can be in the (exclusive) interval (-1000000, 1000000) (if this is not the case, the program prints Incorrect entry! And ends with the work), while the ordinal number of the digit can be from 1 to the number of digits of the entered number n (if this is not the case) the program prints Invalid digit! and terminates). The ordinal number of the digit is determined from left to right, ie if the number is n = 13546, and the ordinal number of the digit is c = 2, then 2 refers to the second digit from the left, is the digit 3. The program should extract the digit with the ordinal number c from the number n, and print it and calculate its square.

The task must not be done over loops or strings

I did 90% of the task myself but I have a problem the numbers print me from right to left and need to go from left to right and I don't know how to do it? Thank you for any advice.

#include <stdio.h>

int main() {

int n, c, d1, d2, d3, d4, d5, d6,d7;

printf("Number: ");

scanf("%d", &n);

if (n >= -1000000 && n <= 1000000) ;

else {

printf("Incorrect entry");

return 0;

}

printf("Which number:");

scanf("%d", &c);

if (c >= 1 && c <= 7)

;

else {

printf("Incorrect number!”);

}

d1=n%10;

d2= (n/10)%10;

d3 =(n/100)%10;

d4 =(n / 1000)%10;

d5= (n / 10000) % 10;

d6=(n / 100000) % 10;

d7 = (n / 1000000) % 10;

if (c==1) {

printf(" %d, square is %d", d1, d1 * d1);}

if (c == 2) {

printf(" %d, square is %d", d6, d6* d6); }

if (c == 3) {

printf(" %d, square is %d", d3, d3 * d3); }

if (c == 4) {

printf(" (" %d, square is %d ", d4, d4 * d4); }

if (c == 5) {

printf(" %d, square is %d ", d5, d5* d5); }

if (c == 6) {

printf(" %d, square is %d ", d6, d6* d6);

}

if (c == 7) {

printf(" %d, square is %d, d7, d7* d7);

}

return 0;}

Aucun commentaire:

Enregistrer un commentaire