C++ Program to Find Sum of Natural Numbers Using Recursion

Nishu Kumari
Updated on 13-May-2025 18:44:02

3K+ Views

Natural numbers are positive numbers starting from 1, such as 1, 2, 3, and so on. Here, we will take a positive number n as input and and then will find the sum of all natural numbers from 1 up to n using recursion using C++ program. Example Input: n = 5 The sum of the first 5 natural numbers is: 1 + 2 + 3 + 4 + 5 = 15 Output: 15 Input: n = 8 The sum of the first 8 natural numbers is: 1 + 2 + 3 + 4 + 5 + 6 ... Read More

Return a Dictionary from a Python Function

Sarika Singh
Updated on 13-May-2025 18:43:53

40K+ Views

Any object, such as a dictionary, can be the return value of a Python function. To do so, create the dictionary object in the function body, assign it to any variable, and return the dictionary to the function's caller. Data values are stored as key-value pairs in dictionaries. A Python dictionary is a collection that is ordered, changeable, and forbids duplicates. In this article, we will discuss various methods to return a dictionary from a Python function. Using Dictionary Comprehension One line of Python code can create and initialize dictionaries using the simple and memory-efficient way known as Dictionary Comprehension. ... Read More

Exclude a Field in Gson During Serialization in Java

Aishwarya Naglot
Updated on 13-May-2025 17:38:16

766 Views

Serialization is the process that converts an object into a format that can be stored or transmitted easily. Excluding a Field in Gson During Serialization We might have some scenarios where we are asked or required to exclude a certain field from the object while serializing it. For example, we have a Student object with fields like name, age, and address. We want to serialize the object but exclude the address field for building basic information about the student in this case, we use In this article, we will learn how to exclude a field in Gson during serialization ... Read More

Access JSON Fields, Arrays, and Nested Objects of JsonNode in Java

Aishwarya Naglot
Updated on 13-May-2025 17:15:34

34K+ Views

What are JSON and JsonNode? JSON is a format that is used to store data and exchange data between a server and a client. To know more about JSON, you can refer to the JSON Tutorial. A JsonNode is Jackson's tree model for JSON. To read JSON into a JsonNode with Jackson by creating an ObjectMapper instance and call the readValue() method. We can access a field, array or nested object using the get() method of the JsonNode class. We can return a valid string representation using the asText() method and ... Read More

Calculate Power Using Recursion in C++

Nishu Kumari
Updated on 13-May-2025 17:12:30

5K+ Views

Calculating a power means multiplying the base by itself as many times as the exponent indicates. In this article, we'll show you how to write a C++ program to calculate the power of a number using recursion.  For example, 2 raised to the power of 3 (2^3) means multiplying 2 by itself 3 times: 2 * 2 * 2, which gives 8. Using Recursion to Calculate Power To calculate the power of a number, we use recursion, where a function keeps calling itself with smaller values until it reaches a stopping point. Here are the steps we ... Read More

Calculate Average of Numbers Using Arrays in C++

Nishu Kumari
Updated on 13-May-2025 17:10:42

9K+ Views

An array stores multiple values of the same data type. To find the average, we add all the numbers and then divide the total by how many numbers there are. In this article, we'll show you how to write a C++ program to calculate the average of numbers using an array.  For example, if we have the numbers 40, 80, 10, 75, and 25, their total is 230, and the average is 230 / 5 = 46. Approaches for Calculating Average Using Arrays In C++, we can calculate the average of numbers in an array in two main ... Read More

C++ Program to Check Leap Year

Nishu Kumari
Updated on 13-May-2025 17:07:56

14K+ Views

A leap year contains one additional day, i.e february 29, to keep the calendar year synchronized with the earth's orbit around the sun. We will write a C++ program that checks whether a given year is a leap year or not. A year that is divisible by 4 is known as a leap year. However, years divisible by 100 are not leap years while those divisible by 400 are. Here is a list of some leap years and non-leap years: ... Read More

C++ Program to Generate Multiplication Table

Nishu Kumari
Updated on 13-May-2025 17:06:55

5K+ Views

In this article, we will show you how to write a C++ program to generate the multiplication table of a number. A multiplication table shows how a number is multiplied by 1 to 10 and helps define the multiplication operation for that number. Each row displays the result of multiplying the number by values from 1 to 10. For example, the multiplication table of 4 looks like this: 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 ... Read More

C++ Program to Display Factors of a Number

Nishu Kumari
Updated on 13-May-2025 17:04:52

12K+ Views

A factor is a number that divides a given number completely without leaving a remainder. In this article, we'll show you how to display all the factors of a number using different methods in C++. For example, the factors of 12 are 1, 2, 3, 4, 6, and 12. In this article, we will look at two ways to find and display the factors of a number: Basic Iterative Approach Optimized Square Root Approach Factors of a Number Using Iterative (Basic) Approach In this approach, we use loop to go ... Read More

Check Whether a Number is Prime in C++

Nishu Kumari
Updated on 13-May-2025 16:59:42

4K+ Views

A prime number is a whole number greater than one and the only factors of a prime number should be one and itself. For example, 2, 3, 5, 7, and 11 are prime numbers. Our task is to write a C++ program that checks if a given number is prime or not. We will cover different approaches to solve this problem. We'll cover the following methods for checking whether a number is prime or not: Using a Simple Loop Using the Square Root Method Using ... Read More

Advertisements