Server Side Programming Articles - Page 2169 of 2650

Python Program to find the sum of array

Pavitra
Updated on 26-Sep-2019 14:11:29

939 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven an array as an input, we need to compute the sum of the given array.Here we may follow the brute-force approach i.e. traversing over a list and adding each element to an empty sum variable. Finally, we display the value of the sum.We can also perform an alternate approach using built-in sum function as discussed below.Example# main arr = [1, 2, 3, 4, 5] ans = sum(arr, n) print ('Sum of the array is ', ans)Output15 All the variables & functions are ... Read More

Python program to find sum of absolute difference between all pairs in a list

Pavitra
Updated on 26-Sep-2019 13:57:48

359 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a list input , we need to find the sum of absolute difference between all pairs in a list.Enumerate() method adds a counter to an iterable and returns it in a form of enumerate object type.In this method, we have a list ‘diffs’ which contains the absolute difference.We use two loops having two variables initialized . One is to iterate through the counter and another for the list element. In every iteration, we check whether the elements are similar or not.If not, ... Read More

Python program to find largest number in a list

Pavitra
Updated on 04-Jul-2020 12:37:12

317 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven list input, we need to find the largest numbers in the given list .Here we will discuss two approachesUsing sorting techniquesUsing built-in max() functionApproach 1 − Using built-in sort() functionExample Live Demolist1 = [18, 65, 78, 89, 90] list1.sort() # main print("Largest element is:", list1[-1])OutputLargest element is: 90Approach 2 − Using built-in max() functionExample Live Demolist1 = [18, 65, 78, 89, 90] # main print("Largest element is:",max(list1))OutputLargest element is: 90ConclusionIn this article, we learnt about the approach to find largest number in a list.

Deep Copy of Struct Member Arrays in C

Narendra Kumar
Updated on 26-Sep-2019 13:44:41

2K+ Views

Structure allows us to create user defined datatype. Structure member can be primitive datatype or it can be an array of statically allocated memory. When we assign one structure variable to another then shallow copy is performed. However, there is an exception, if structure member is an array then compiler automatically performs deep copy. Let us see this with example −Example Live Demo#include #include typedef struct student {    int roll_num;    char name[128]; } student_t; void print_struct(student_t *s) {    printf("Roll num: %d, name: %s", s->roll_num, s->name); } int main() {    student_t s1, s2;    s1.roll_num = ... Read More

Alternating Vowels and Consonants in C/C++

Narendra Kumar
Updated on 26-Sep-2019 13:41:39

388 Views

Given an input string with vowels and consonants. Rearrange the string such a way that vowels and consonants occupies alternate position in final string. As we are arranging vowels and consonants at alternate position the input string must satisfy either of the following conditions −Number of vowels and consonants must be same e.g. string "individual" has 5 vowels and 5 consonants.If number of vowels are more, then difference between number of vowels and number of consonants must be 1 e.g. string "noe" has 2 vowels and 1 consonant.If number of consonants are more, then difference between number of consonants and ... Read More

Python program to convert decimal to binary number

Pavitra
Updated on 04-Jul-2020 12:41:01

1K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a number we need to convert into a binary number.Approach 1 − Recursive SolutionDecToBin(num):    if num > 1:       DecToBin(num // 2)       print num % 2Exampledef DecimalToBinary(num):    if num > 1:       DecimalToBinary(num // 2)    print(num % 2, end = '') # main if __name__ == '__main__':    dec_val = 35    DecimalToBinary(dec_val)Output100011All the variables and functions are declared in the global scope as shown below −Approach 2 − Built-in SolutionExample Live Demodef ... Read More

C++17 If statement with initializer

Narendra Kumar
Updated on 26-Sep-2019 13:34:49

14K+ Views

C++17 has extended existing if statement’s syntax. Now it is possible to provide initial condition within if statement itself. This new syntax is called "if statement with initializer". This enhancement simplifies common code patterns and helps users keep scopes tight. Which in turn avoids variable leaking outside the scope.ExampleLet us suppose we want to check whether given number is even or odd. Before C++17 our code used to look like this − Live Demo#include #include using namespace std; int main() {    srand(time(NULL));    int random_num = rand();    if (random_num % 2 == 0) {       cout

Python Program to check whether it is possible to make a divisible by 3 number using all digits in an array

Pavitra
Updated on 26-Sep-2019 12:33:11

297 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven an array input of integers, we need to find whether it’s possible to make an integer using all the digits available in these numbers such that it would be divisible by 3.Here we will generate a function that will take two arguments namely the array of integers and the length of the array.The implementation given below works on the concept from the mental mathematics. Here we observe that a number is divisible by 3 if the sum of the digits are divisible ... Read More

Python program to check if the given string is pangram

Pavitra
Updated on 26-Sep-2019 12:24:23

9K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a string input, we need to generate a Python program to check whether that string is Pangram or not.A pangram is a sentence/ series of words which contains every letter in the English Alphabets collection.Now let’s see how we can solve the problemWe will use a loop that checks whether each character present in the input string belongs to the alphabet set or not which we will declare manually .The implementation of the approach above is given by −Example Live Demoimport string def ... Read More

Python program to check if a given string is number Palindrome

Pavitra
Updated on 26-Sep-2019 12:22:39

7K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a string input, we need to create a python function to check whether it is a palindrome or not.A string is referred to be palindrome if the reverse of the string is identical with string.We can do this by two methods −Reversal by slicingComparison via negative indexingHere we will be learning reversal pf string bu the help of slicing.To reverse a string by th method of slicing specify the following statement −Str[ : : -1 ]Where the start and end parameters are ... Read More

Advertisements