import java.util.*; public class Main { public static void main(String args[]) { int p; Scanner s = new Scanner(System.in); System.out.print("Input a positive integer: "); int n = s.nextInt(); if (n > 0) { p=(n == 0 ? 0 : (n % 9 == 0 ? 9 : n % 9)); System.out.print("The single digit number is: " +p); } } }
Input a positive integer: 1224
The single digit number is: 9
def myfun(): num=int(input("Input a positive integer: ")) p=(num - 1) % 9 + 1 if num > 0 else 0 print("The single digit number is: ",p) if __name__=="__main__": myfun()
Input a positive integer: 1224
The single digit number is: 9
#include <stdio.h> int main() { int n,p; printf("Input a positive integer: "); scanf("%d",&n); p=(n == 0 ? 0 : (n % 9 == 0 ? 9 : n % 9)); printf("The single digit number is: %d",p); return 0; }
Input a positive integer: 1224
The single digit number is: 9