C Program
/* C - Count Digits */ #include <stdio.h> int main(){ long long n; scanf("%lld",&n); if(n<0) n=-n; int count = (n==0) ? 1 : 0; while(n>0){ count++; n/=10; } printf("%d",count); return 0; }
C Output
Input: 5023 Output: 4
C++ Program
// C++ - Count Digits #include <bits/stdc++.h> using namespace std; int main(){ long long n; cin>>n; if(n<0) n=-n; int count = (n==0) ? 1 : 0; while(n>0){ count++; n/=10; } cout<<count; }
C++ Output
Input: 123456 Output: 6
JAVA Program
// Java - Count Digits import java.util.*; class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); long n = sc.nextLong(); if(n<0) n = -n; int count = (n==0) ? 1 : 0; while(n>0){ count++; n/=10; } System.out.print(count); } }
JAVA Output
Input: 90876 Output: 5
Python Program
# Python - Count Digits n = int(input()) if n < 0: n = -n count = 1 if n == 0 else 0 while n > 0: count += 1 n //= 10 print(count)
Python Output
Input: 0 Output: 1
In-Depth Learning – Entire Concept in Paragraphs
Social Plugin