The sum of squares of the first n natural numbers is found by adding up all the squares.Input - 5Output - 55Explanation - 12 + 22 + 32 + 42 + 52There are two methods to find the Sum of squares of first n natural numbers −Using Loops − the code loops through the digits until n and find their square, then add this to a sum variable that outputs the sum.Example#include using namespace std; int main() { int n = 5; int sum = 0; for (int i = 1; i >= n; i++) sum += (i * i); cout
Smallest K digit number that divisible by X is found using the formula by checking divisible by X. The formula works in the following way −Compute minimum K digit number [min] for example: 10/100/1000 etc.Now find if min is divisible by X. if yes, then this is the answer.If not, then min+X - ([min+X]%k) is the answer.Example#include #include using namespace std; int main() { int X = 83; int K = 5; cout
Power function is calculated using the times multiple i.e. 5n is 5*5*5… n times. For this function, to work properly without using multiplication(*) and division(/) operators we will use nested loops that add the numbers n number of time.Example#include using namespace std; int main() { int a= 4 , b = 2; if (b == 0) cout
The sum of squares of the first n even numbers means that, we first find the square and add all them to give the sum.There are two methods to find the sum of squares of the first n even numberUsing LoopsWe can use loops to iterate from 1 to n increase number by 1 each time find the square and add it to the sum variable −Example#include using namespace std; int main() { int sum = 0, n =12; for (int i = 1; i
Finding the number of vowels in a string using pointers need you to understand string, vowels and how to use pointer with string.String is an array of characters. And vowels are characters from the set {a, e, i, o, u}. Pointer is a variable that stores the value of memory location on a variable.To find the number of vowels in a string. We will traverse the string and then compare each character with vowels and if it is equal then it increase a counter otherwise not.Condition of the below code is that it requires a string that has all lowercase ... Read More
A sequence of characters or a linear array of character is known as String. Its declaration is same as define other arrays.Length of array is the number of characters in the String. There are many in-built method and other methods to find the length of string. Here, we are discussing 5 different methods to find the length of a string in C++.1) Using the strlen() method of C − This function returns an integer value of the C. For this you need to pass the string in the form of character array.PROGRAM TO ILLUSTRATE THE USE OF strlen() METHOD#include ... Read More
Create a Python ListExampleC:\Py3Project>howdoi create a python listOutputRunning the above code gives us the following result −>>> l = [None] * 10 >>> l [None, None, None, None, None, None, None, None, None, None]Printing Today’s DateExamplec:\python3>howdoi print today's date in pythonOutputRunning the above code gives us the following result −for date in mylist : print str(date)Examplec:\python3>howdoi create fibonnaci series in pythonOutputRunning the above code gives us the following result −def F(n): if n == 0: return 0 elif n == 1: return 1 else: return F(n-1)+F(n-2)Examplec:\python3>howdoi use calendar in javascriptOutputRunning the above code gives us the following ... Read More
In python the print statement adds a new line character by default. So when we have multiple print statements the output from each of them is printed in multiple lines as you can see in the example below. Our goal is to print them in a single line and use some special parameters to the print function to achieve that.Normal Print()The below example prints multiple statements with new lines in each of them.Exampleprint("Apple") print("Mango") print("Banana")OutputRunning the above code gives us the following result −Apple Mango BananaUsing end ParameterWe can use the end parameter to use a specific character at the ... Read More
Greatest common divisor or gcd is a mathematical expression to find the highest number which can divide both the numbers whose gcd has to be found with the resulting remainder as zero. It has many mathematical applications. Python has a inbuilt gcd function in the math module which can be used for this purpose.gcd()It accepts two integers as parameter and returns the integer which is the gcd value.SyntaxSyntax: gcd(x, y) Where x and y are positive integers.Example of gcd()In the below example we print the result of gcd of a pair of integers.import math print ("GCD of 75 and 30 ... Read More
These two methods are part of python math module which helps in getting the nearest integer values of a fractional number.floor()It accepts a number with decimal as parameter and returns the integer which is smaller than the number itself.SyntaxSyntax: floor(x) Where x is a numeric valueExample of floor()In the below example we take different types of numeric values like, integer, positive decimal and negative decimal and apply the floor function to them. We get the nearest integer which is less than the numeric value supplied.import math x, y, z = 21 , -23.6 , 14.2 print("The value of ", x, ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP