Program to count the number of digits in an integer.

Write a C program to count the number of digits in an integer. or Write a program to count the number of digits in an integer in C.

Program in C

Code:

/*Write a C program to count the number of digits in an integer. or Write a program to count the number of digits in an integer Using C*/

#include<stdio.h>
int main()
{
    int n;
    printf("Enter a number:");
    scanf("%d",&n);
    int count=0;
    while(n!=0)
    {
        n=n/10;
        count++;
    }
      printf("The number of digits in the number is %d",count);
}

Input/Output:
Enter a number:79546
The number of digits in the number is 5

Write a C++ program to count the number of digits in an integer. or Write a program to count the number of digits in an integer in C++.

Program in C++

Code:

/*Write a C++ program to count the number of digits in an integer. or Write a program to count the number of digits in an integer using C++*/

#include<iostream>
using namespace std;
int main()
{
    int n;
    cout<<"Enter a number:";
    cin>>n;
    int count=0;
    while(n!=0)
    {
        n=n/10;
        count++;
    }
      cout<<"The number of digits in the number is "<<count;
}

Input/Output:
Enter a number:586453865
The number of digits in the number is 9

Write a JAVA program to count the number of digits in an integer. or Write a program to count the number of digits in an integer in Java.

Program in Java

Code:

/*Write a JAVA program to count the number of digits in an integer. or Write a program to count the number of digits in an integer using Java*/

import java.util.Scanner;
public class countdigit {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int n,count=0;
    System.out.println("Enter a number:");
    n=cs.nextInt();
    while(n!=0)
    {
        n=n/10;
        count++;
    }
   System.out.println("The number of digits in the number is "+count);
cs.close();
}
}

Input/Output:
Enter a number:
54546

The number of digits in the number is 5 

Write a PYTHON to count the number of digits in an integer. or Write a program to count the number of digits in an integer in Python.

Program in Python

Code:

''Write a Python program to count the number of digits in an integer. or 
  Write a program to count the number of digits in an integer using Python '''

n=int(input("Enter a number:"))

count=0
while n>0:
   n=int(n/10)
   count+=1
print("The number of digits in the number is", count)

Input/Output:
Enter a number:4879
The number of digits in the number is 4




Post a Comment

0 Comments