Found 1401 Articles for C

Bash program to find A to the power B?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

3K+ Views

Here we will see how to get the number A raise to the power B using bash script. The logic is very simple. We have to use the ‘**’ operator or power operator to do this. Let us see the following program to understand the concept clearly.Example#!/bin/bash # GNU bash Script a=5 b=6 echo "$(($a ** $b))"Output15625

Bash program to check if the Number is a Palindrome?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

4K+ Views

To check whether a number is palindrome or not, we have to reverse the number, and then if the actual number and the reversed number are same, then this is palindrome. In Bash, performing the reverse operation is very easy. We have to use the ‘rev’ command to do that. Let us see the program to understand clearly.Example#!/bin/bash # GNU bash Script n=12321 rev=$(echo $n | rev) if [ $n -eq $rev ]; then    echo "Number is palindrome" else    echo "Number is not palindrome" fioutlineNumber is palindrome

Average of ASCII values of characters of a given string?

Arnab Chakraborty
Updated on 02-Jul-2020 07:26:14

765 Views

Here we will see how to count the average of the ASCII values of each character in a given string. Suppose the string is “ABC”. The asci values are 65, 66, 67. So the average of these three is 66.AlgorithmasciiAverage(String)Begin    sum := 0    for each character c in String, do       sum := sum + ASCII of c    done    return sum/length of String EndExample Live Demo#include using namespace std; float asciiAverage(string str){    int sum = 0;    for(int i = 0; i str;    cout

Average of first n odd naturals numbers?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

197 Views

Here we will see how to get the average of n odd natural numbers. The n is given by the user. To get the ith odd number we can use this formula 2*i+1. Here also we are using this formula. Let us see the algorithm to get the clear idea.AlgorithmavgOddNaturalNumber(n)Begin    sum := 0    for i in range 0 to n-1, do       sum := sum + (2i + 1)    done    return sum/n EndExample Live Demo#include using namespace std; float asciiAverage(string str){    int sum = 0;    for(int i = 0; i str;    cout

ASCII NUL, ASCII 0 (‘0’) and Numeric literal 0?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

7K+ Views

Here we will see the ASCII NUL, ASCII 0 and the Numeric Literal 0. The ASCII null is represented as 0x00, and zero is represented as 0x30. The ASCII NUL character is used to denote the end of the string in C or C++. When programmer used ‘0’ (character 0) it is treated as 0x30. This is a hexadecimal number. The decimal equivalent is 48. To put ASCII NUL we have to mention ‘\0’ instead of ‘0’.char asciiNul = ‘\0’; int zero = 0; char zeroChar = ‘0’;The first one is ASCII NUL, second one is numeric 0, and the third one is character 0.

Arrange given numbers to form the biggest number?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

173 Views

Here we will see how to generate the biggest number by rearranging the given numbers. Suppose there are {45, 74, 23} is given, the program will find the largest number, that is 744523. So each digit will not be arranged. but the whole number will be placed to make largest number.To solve this problem, we will use the string sorting. But the comparison logic is different. The comparing function will take two numbers a and b, then concatenate them to form ab and ba. Among them which one is bigger, that is considered.AlgorithmcompareStrings(a, b)begin    ab := concatenate b with ... Read More

Argument Coercion in C/C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

1K+ Views

Here we will see about the argument coercion in C or C++. The Argument Coercion is one technique by which the compiler can implicitly convert the arguments from one type to another type. It follows argument promotion rule. If one argument is lower datatype, that can be converted into higher datatypes, but the reverse is not true. The reason is if one higher datatype is converted into a lower datatype, it may loss some data.Let us see one pyramid that can express how the implicit conversion takes place.Example Live Demo#include using namespace std; double myAdd(double a, double b){    return a+b; ... Read More

Area of triangle formed by the axes of co-ordinates and a given straight line?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

83 Views

Here we will see how to get the area of a triangle formed by the x and y axis and another straight line. The diagram will be look like below. The equation of the straight line is −𝑎𝑥+𝑏𝑦+𝑐=0The line is cutting the x-axis at the point B, and cutting the y-axis at the point A. The intercept form will be like below −So the x-intercept is −𝑐∕𝑎 and y-intercept is −𝑐∕𝑏 . So the area of the triangle isExample Live Demo#include #include using namespace std; double areaTriangle(double a, double b, double c){    return fabs((c*c) / (2*a*b)); } main() {   ... Read More

All permutations of a string using iteration?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

557 Views

In this section we will see how to get all permutations of a string. The recursive approach is very simple. It uses the back-tracking procedure. But here we will use the iterative approach.All permutations of a string ABC are like {ABC, ACB, BAC, BCA, CAB, CBA}. Let us see the algorithm to get the better idea.AlgorithmgetAllPerm(str)begin    sort the characters of the string    while true, do       print the string str       i := length of str – 1       while str[i - 1] >= str[i], do          i := ... Read More

What's the difference between sizeof and alignof?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

410 Views

Here we will see what are the differences of sizeof and the alignof operator in C++. The alognof() operator is introduced in C++11.The alignof() operator is used to get the alignment in bytes. It requires instances of type. the type is either complete type or a reference type. There is another operator called the sizeof() operator, that returns the size of one type. For normal datatypes the sizeof and the alignof returns the same value. For some user defined datatype, the alignof returns some different value. Let us see the example to get the idea.Example Live Demo#include using namespace std; struct ... Read More

Advertisements