Check if a string contains only digits

Problem statement:- Program to Check if a string contains only digits or not.

Example:-

              Input: Given

                                 String=49875

             Output: string contains only digits.

            Input: Given

                                String=csinfo360

             Output: String does not contain only digits.

Data requirement:-


   Input Data:- str

  Output Data:-
string output

  Additional Data:- 
in, count, len

Program in C

Here is the source code of the C Program to Check if a string contains only digits or not.

Code:

#include<stdio.h>
#include<string.h>
main()
{
    int count=0,in;
    char str[100]={0};
    printf("Enter your String:");
    gets(str);

     for(in=0;in<strlen(str);in++)
    {
            if(str[in] >= '0' && str[in] <= '9')
            {
                  count++;
            }
    }
    if(count==strlen(str))
        printf("String contains only digits.");
    else
        printf("String does not contains only digits.");
}

Input/Output:
Enter your String:49875
string contains only digits.

Program in C++

Here is the source code of the C++ Program to Check if a string contains only digits or not.

Code:

#include<iostream>
#include <cstring>
using namespace std;
main()
{
    int count=0,in;
    string str;
    cout<<"Enter your String:";
    getline(cin,str);
    int len=0;
     for(in=0;str[in]!='\0';in++)
    {
        len++;
    }
     for(in=0;in<len;in++)
    {
            if(str[in] >= '0' && str[in] <= '9')
            {
                  count++;
            }
    }
    if(count==len)
        cout<<"String contains only digits.";
    else
        cout<<"String does not contain only digits.";
}

Input/Output:
Enter your String:csinfo360
String does not contain only digits.

Program in Java

Here is the source code of the Java Program to Check if a string contains only digits or not.

Code:

import java.util.Scanner;
public class ContainsOnlyDigits {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str;
int count=0,in;
System.out.println("Enter your String:");
str=cs.nextLine();
for(in=0;in<str.length();in++)
    {
            if(str.charAt(in) >= '0' && str.charAt(in) <= '9')
            {
                  count++;
            }
    }
    if(count==str.length())
    System.out.print("String contains only digits.");
    else
    System.out.print("String does not contains only digits.");
cs.close();
}
}

Input/Output:
Enter your String:
csinfo360.com
String does not contain only digits.

Program in Python

Here is the source code of the Python Program to Check if a string contains only digits or not.

Code:

str=input("Enter Your String:")
count=0
for inn in range(0,len(str)):
    if str[inn] >= '0' and  str[inn] <= '9':
        count+=1
if count==len(str):
    print("String contains only digits.")
else:
    print("String does not contain only digits.")

Input/Output:
Enter Your String:9875410
String contains only digits.

Post a Comment

0 Comments