To check if a given number is even or odd in C++, we simply divide the given number by 2, if the remainder is 0 then it is even otherwise odd.
[mai mult...]Write a program for Adding two numbers in C++
To Add two numbers in C++ we will read two numbers a and b from the user then perform add operation to add a and b together to print the addition of two numbers in C++.
[mai mult...]Find Size of int, float, double and char in Your System using C++
This program declares 4 variables of type int, float, double and char. Then, the size of each variable is evaluated using sizeof operator.
To find the size of variable, sizeof
operator is used.
sizeof(dataType);
Program to Find Quotient and Remainder in C++
The division operator /
computes the quotient (either between float or integer variables). The modulus operator %
computes the remainder when one integer is divided by another (modulus operator cannot be used for floating-type variables).
Find the quotient and remainder of a given dividend and divisor. In this program, the user is asked to enter two integers (divisor and dividend) and the quotient and the remainder of their division is computed. To compute quotient and remainder, both divisor and dividend should be integers.
[mai mult...]Program to Add Two Numbers in C++
User is asked to enter two integers. Then, the sum of those two integers is stored in a variable and displayed on the screen. Primary tabs
In this program, the user is asked to enter two integers.These two integers are stored in variables first_number and second_number respectively.Then, the variables are added using the +
operator and stored in the sum variable. Finally, sum is displayed on the screen.
C++ Program to Print Number Entered by User
Print the number entered by a user using C++ cout statement.
This program asks the user to enter a number. When the user enters an integer, it is stored in variable number using cin
. Then it is displayed on the screen using cout
.
Replace the spaces of a string with a specific character using Java
- public class ReplaceSpace
- {
- public static void main(String[] args) {
- String string = “Once in a blue moon”;
- char ch = ‘-‘;
- //Replace space with specific character ch
- string = string.replace(‘ ‘, ch);
- System.out.println(“String after replacing spaces with given character: “);
- System.out.println(string);
- }
- }
Output:
String after replacing spaces with given character: Once-in-a-blue-moon
Find the most repeated word in a text file with Java
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.util.ArrayList;
- public class MostRepeatedWord {
- public static void main(String[] args) throws Exception {
- String line, word = “”;
- int count = 0, maxCount = 0;
- ArrayList<String> words = new ArrayList<String>();
- //Opens file in read mode
- FileReader file = new FileReader(“data1.txt “);
- BufferedReader br = new BufferedReader(file);
- //Reads each line
- while((line = br.readLine()) != null) {
- String string[] = line.toLowerCase().split(“([,.\\s]+) “);
- //Adding all words generated in previous step into words
- for(String s : string){
- words.add(s);
- }
- }
- //Determine the most repeated word in a file
- for(int i = 0; i < words.size(); i++){
- count = 1;
- //Count each word in the file and store it in variable count
- for(int j = i+1; j < words.size(); j++){
- if(words.get(i).equals(words.get(j))){
- count++;
- }
- }
- //If maxCount is less than count then store value of count in maxCount
- //and corresponding word to variable word
- if(count > maxCount){
- maxCount = count;
- word = words.get(i);
- }
- }
- System.out.println(“Most repeated word: “ + word);
- br.close();
- }
- }
Output:
Most repeated word: word
Find the frequency of characters with Java
To accomplish this task, we will maintain an array called freq with same size of the length of the string. Freq will be used to maintain the count of each character present in the string. Now, iterate through the string to compare each character with rest of the string. Increment the count of corresponding element in freq. Finally, iterate through freq to display the frequencies of characters.
For example: Frequency of p in above string is 2.
[mai mult...]How to Reverse a String in Java
Example 1: Reverse a string word by word using recursion
- import java.util.Scanner;
- public class ReverseStringExample1
- {
- public static void main(String[] args)
- {
- String str;
- System.out.println(“Enter a string: “);
- Scanner scanner = new Scanner(System.in);
- str = scanner.nextLine();
- scanner.close(); //closes the input stream
- String reversed = reverseString(str);
- System.out.println(“The reversed string is: “ + reversed);
- }
- public static String reverseString(String s)
- {
- if (s.isEmpty()) //checks the string if empty
- return s;
- return reverseString(s.substring(1)) + s.charAt(0); //recursively called function
- }
- }
Output: