Programming Articles - Page 2434 of 3366

Check if a string contains only alphabets in Java using ASCII values

AmitDiwan
Updated on 20-Sep-2019 08:29:26

493 Views

Let’s say we have set out inut string in myStr variable. Now loop through until the length of string and check for alphabets with ASCII values −for (int i = 0; i < myStr.length(); i++) {    char c = myStr.charAt(i);    if (!(c >= 'A' && c = 'a' && c = 'A' && c = 'a' && c

Program for EMI Calculator in C program

Sunidhi Bansal
Updated on 20-Sep-2019 11:48:47

861 Views

Given with certain values the program will develop an EMI calculator to generate the needed output. EMI stands for Equated Monthly Installment. So this calculator will generate monthly EMI amount for the user.ExampleInput-: principal = 2000    rate = 5    time = 4 Output-: Monthly EMI is= 46.058037The formula used in the below program is −EMI : (P*R*(1+R)T)/(((1+R)T)-1)where, P indicates loan amount or the Principal amount.R indicates interest rate per monthT indicates loan time period in yearApproach used below is as followsInput principal, rate of interest and time in float variableApply the formula to calculate the EMI amountPrint the ... Read More

Check if a string contains only alphabets in Java using Regex

AmitDiwan
Updated on 20-Sep-2019 08:24:32

988 Views

At first, convert the string into character array. Here, name is our string −char[] ch = name.toCharArray();Now, loop through and find whether the string contains only alphabets or not. Here, we are checking for not equal to a letter for every character in the string −for (char c : ch) {    if(!Character.isLetter(c)) {       return false;    }Following is an example to check if a string contains only alphabets using RegexExample Live Demopublic class Main {    public static boolean checkAlphabet(String name) {       char[] ch = name.toCharArray();       for (char c : ch) { ... Read More

Check if a string contains only alphabets in Java using Lambda expression

AmitDiwan
Updated on 20-Sep-2019 08:19:04

789 Views

Let’s say our string is −String str = "Amit123";Now, using allMatch() method, get the boolean result whether the string has only alphabets or now −boolean result = str.chars().allMatch(Character::isLetter);Following is an example to check if a string contains only alphabets using Lambda Expressions −Exampleclass Main {    public static void main(String[] args) {       String str = "Amit123";       boolean result = str.chars().allMatch(Character::isLetter);       System.out.println("String contains only alphabets? = "+result);    } }OutputLet us see another example with a different input −String contains only alphabets? = falseExampleclass Main {    public static void main(String[] args) ... Read More

Program for Celsius To Fahrenheit conversion in C++

Sunidhi Bansal
Updated on 20-Sep-2019 08:30:20

810 Views

Given with temperature ‘n’ in Celsius the challenge is to convert the given temperature to Fahrenheit and display it.ExampleInput 1-: 100.00    Output -: 212.00 Input 2-: -40    Output-: -40For converting the temperature from Celsius to Fahrenheit there is a formula which is given belowT(°F) = T(°C) × 9/5 + 32Where, T(°C) is temperature in Celsius and T(°F) is temperature in FahrenheitApproach used below is as followsInput temperature in a float variable let’s say CelsiusApply the formula to convert the temperature into FahrenheitPrint FahrenheitALGORITHMStart Step 1 -> Declare a function to convert Celsius to Fahrenheit    void cal(float cel) ... Read More

Insert a string into another string in Java

AmitDiwan
Updated on 27-Aug-2024 18:46:59

4K+ Views

In this article, we will explore how to insert a string into another string at a specific position using Java. To do so we will be using the StringBuffer class.  StringBuffer class: StringBuffer class creates and manipulates strings that can be changed. It changes the contents of the string without creating a new object each time. Problem Statement Write a program in Java to insert a string into another string − Input That's good! Output Index where new string will be inserted = 6Resultant String = That's no good! Steps to insert a string into ... Read More

Program for n’th node from the end of a Linked List in C program

Sunidhi Bansal
Updated on 04-Jul-2020 07:01:13

2K+ Views

Given with n nodes the task is to print the nth node from the end of a linked list. The program must not change the order of nodes in a list instead it should only print the nth node from the last of a linked list.ExampleInput -: 10 20 30 40 50 60    N=3 Output -: 40In the above example, starting from first node the nodes till count-n nodes are traversed i.e 10, 20 30, 40, 50, 60 and so the third node from the last is 40.Instead of traversing the entire list this efficient approach can be followed ... Read More

Initialize HashMap in Java

AmitDiwan
Updated on 20-Sep-2019 07:57:58

724 Views

The HashMap class uses a hashtable to implement the Map interface. This allows the execution time of basic operations, such as get( ) and put( ), to remain constant even for large sets.Following is the list of constructors supported by the HashMap class.Sr.NoConstructor & Description1HashMap( )This constructor constructs a default HashMap.2HashMap(Map m)This constructor initializes the hash map by using the elements of the given Map object m.3HashMap(int capacity)This constructor initializes the capacity of the hash map to the given integer value, capacity.4HashMap(int capacity, float fillRatio)This constructor initializes both the capacity and fill ratio of the hash map by using its ... Read More

Product of the nodes of a Singly Linked List

Sunidhi Bansal
Updated on 20-Sep-2019 08:01:06

400 Views

Given with n nodes the task is to print the product of all the nodes of a singly linked list. The program must traverse all the nodes of a singly linked list starting from the initial node till the NULL is not found.ExampleInput -: 1 2 3 4 5 Output -: 120In the above example, starting from first node all the nodes are traversed i.e 1, 2 3, 4, 5, 6 and their product is 1*2*3*4*5*6 = 120Approach used below is as followsTake a temporary pointer, lets say, temp of type nodeSet this temp pointer to first node which is ... Read More

Initialize an ArrayList in Java

Alshifa Hasnain
Updated on 18-Mar-2025 17:55:13

2K+ Views

In this article, we will learn to initialize an ArrayList in Java. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk. Different Approaches The following are the two different approaches to initialize an ArrayList in Java − Using add() Method Using asList() method Using add() Method One of the most frequent methods of ... Read More

Advertisements