Found 34494 Articles for Programming

Java program to calculate Body Mass Index (BMI)

Samual Sam
Updated on 31-May-2024 13:04:55

21K+ Views

The Body Mass Index is the body mass in kilogram divided by the square of body height in meters. This is expressed as kg/m^2.A Java program that calculates the Body Mass Index (BMI) is given as follows.Exampleimport java.util.Scanner; public class Example {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.print("Input weight in kilogram: ");       double weight = sc.nextDouble();       System.out.print("Input height in meters: ");       double height = sc.nextDouble();       double BMI = weight / (height * height);     ... Read More

Generate temporary files and directories using Python

Arjun Thakur
Updated on 25-Jun-2020 14:21:25

14K+ Views

The tempfile module in standard library defines functions for creating temporary files and directories. They are created in special temp directories that are defined by operating system file systems. For example, under Windows the temp folder resides in profile/AppData/Local/Temp while in linux the temporary files are held in /tmp directory.Following functions are defined in tempfile moduleTemporaryFile()This function creates a temporary file in the temp directory and returns a file object, similar to built-in open() function. The file is opened in wb+ mode by default, which means it can be simultaneously used to read/write binary data in it. What is important, ... Read More

Java program to check if a Substring is present in a given String

karthikeya Boyini
Updated on 25-Jun-2020 14:23:59

946 Views

A substring is a part of a string and it can be checked if a particular substring is present in the given string or not. An example of this is given as follows −String = The sunset is beautiful Substring = sunsetThis substring is present in the string.A program that demonstrates this is given as follows −Example Live Demopublic class Example {    public static void main(String args[]) {       String str = "Apples are red";       String substr = "are";       int n1 = str.length();       int n2 = substr.length();       System.out.println("String: " + str);       System.out.println("Substring: " + substr);       for (int i = 0; i

File and Directory Comparisons in Python

George John
Updated on 25-Jun-2020 13:34:45

5K+ Views

Python’s standard library has filecmp module that defines functions for comparison of files and directories. This comparison takes into consideration the properties of files in addition to data in them.Example codes in this article use following file and directory structure.Two directories dir1 and dir2 are first created under current working directory. They contain following files.--dir1/newfile.txt-- This is a file in dir1 --dir1/file1.txt-- Hello Python --dir1/file2.txt-- Python Standard Library --dir2/file1.txt-- Hello Python --dir2/file2.txt-- Python LibraryLet us now describe various comparison functions in filecmp module.filecmp.cmp(f1, f2, shallow=True)This function compares the two files and returns True if they are identical, False otherwise. The ... Read More

Java Program to check whether the character is ASCII 7 bit printable

Samual Sam
Updated on 25-Jun-2020 13:33:17

234 Views

To check whether the entered value is ASCII 7-bit printable, check whether the characters ASCII value is greater than equal to 32 and less than 127 or not. These are the control characters.Here, we have a character.char one = '^';Now, we have checked a condition with if-else for printable characters.if (c >= 32 && c < 127) {    System.out.println("Given value is printable!"); } else {    System.out.println("Given value is not printable!"); }Example Live Demopublic class Demo {    public static void main(String []args) {       char c = '^';       System.out.println("Given value = "+c);     ... Read More

Random access to text lines in Python (linecache)

Chandu yadav
Updated on 25-Jun-2020 13:36:20

780 Views

Purpose of linecache module in Python’s standard library is to facilitate random access to any text file, although this module is extensively used by Python’s traceback module to generate error trace stack. Further prettyprints of reading are held in a cache so that it saves time while reading lines repeatedly.The most important function in this module is getline() which reads a specified line number from given file. Following is the list of functions −getline(file, x)This function returns xth line from file. If not present it will return empty string. If the file is not present in current path, function ties ... Read More

Java Program to check whether the character is ASCII 7 bit numeric

karthikeya Boyini
Updated on 25-Jun-2020 13:38:30

142 Views

To check whether the entered value is ASCII 7-bit numeric, check the character from ‘0’ to ‘9’.Here, we have a numeric character.char one = '9';Now, we have checked a condition with if-else for numeric character from ‘0’ to ‘9’if (c >= '0' && c = '0' && c = '0' && c = '0' && c

Iterate over lines from multiple input streams in Python

Ankith Reddy
Updated on 25-Jun-2020 13:43:08

515 Views

Python’s built-in open() function opens one file in read/write mode and read/write operations on it. To perform processing on multiple files in a batch, one has to use fileinput module of Python’s standard library. This module provides a Fileinput class with functionality of iterating over files. The module also defines helper functions for the same purpose.Primary interface to this module is input() function. This function returns instance of Fileinput class.fileinput.input(files, inplace, mode)The files parameter is name of one or more files to be read one by one. Each file acts as a generator and using a for loop it can ... Read More

Java Program to check whether the entered character is ASCII 7 bit numeric and character

Samual Sam
Updated on 25-Jun-2020 13:44:38

97 Views

To check whether the entered value is ASCII 7-bit numeric and character (alphanumeric), check the characters ASCII value for −A to Z Or a to z Or 0 to 9Here, we have the following value −char one = '5';Now, we have checked some conditions with if-else for ASCII 7-bit numeric and character.if ((one >= 'A' && one = 'a' && one = '0' && one = 'A' && one = 'a' && one = '0' && one = 'A' && one = 'a' && one = '0' && one

Object-oriented filesystem paths in Python (pathlib)

Arjun Thakur
Updated on 25-Jun-2020 13:47:38

695 Views

The pathlib module provides an object oriented approach to handling filesystem paths. The module also provides functionality appropriate for various operating systems. Classes defined in this module are of two types – pure path types and concrete path types. While pure paths can only perform purely computational operations, concrete paths are capable of doing I/O operations too.pathlib module defines following classes −Sr.No.Module & Description1PurePathThe base class for all other classes2Pathsubclassed from PurePath. This is a concrete class that represents filesystem path.3PosixPathPath subclass for non-Windows OS4WindowsPathPath subclass for Windows systems5PurePosixPathPurePath subclass for non-Windows systems6PureWindowsPathPurePath subclass for Windows systemsWhen instance of Path ... Read More

Advertisements