Posts

C program to find the size of variables

 In this example, discuss how to find the size of the variable in c programming language. Following is the code of the problem. Code: #include<stdio.h> int main() {     int intvar;     float floatvar;     double doublevar;     char charvar;     // sizeof is used to find the size of a variable      printf("Size of char: %zu byte\n", sizeof(charvar));     printf("Size of int: %zu bytes\n", sizeof(intvar));     printf("Size of double: %zu bytes\n", sizeof(doublevar));     printf("Size of float: %zu bytes\n", sizeof(floatvar));                return 0; } Output: Size of char: 1 byte Size of int: 4 bytes Size of double: 8 bytes Size of float: 4 bytes

C program to find Ascii value of character

 In this example, discuss how to find the ASCII value of the character in the c programming language. Following is the solution to this problem. code: #include <stdio.h> int main() {       char ch;     printf("Enter a character: ");     scanf("%ch", &ch);            // %d display the integer value(ascii value) of a character     // %c display  actual character     printf("ASCII value of %c = %d", ch, ch);          return 0; } Output: Enter a character: a ASCII value of a = 97 Here,take a character input from the user and display its integer value by using %d in printf.

Tree data structure and its terminology

Image
Why we need a tree data structure? In the array, linked list, stack, and queue data is store in a sequential manner which is difficult to search specific data .the search becomes slow when a huge amount of data stored. This is a very annoying thing in the fast world. All people want to get the result as soon as possible. Different Tree data structures allow to access the data quickly and also it is not a linear data structure. First of all, look at the terminologies of the tree before understanding the concept of the tree. What is a tree? A collection of nodes that are connected by directed edges is known as tree. It is a nonlinear structure as compare to arrays, linked lists, and stacks. Node: A node is a struct that contains a value (data) and stores its pointer. There are two types of nodes internal nodes those nodes which contain at least one child leaf node and external nodes those nodes which are the last. Edge: This is a link between nodes. Root: It is the top most node of a tre

CPP program to calculate the power

 In this solution try to solve, the problem of finding the power of a number manually and with pow() function in the cpp programming language. Firstly, look at manually finding the power of any number. Code: #include <iostream> using namespace std; int main()  {     int exponent;     float base, pow = 1;     cout << "Enter base and exponent respectively:  ";     cin >> base >> exponent;     cout << base << "^" << exponent << " = ";     for(int i=1;i<=exponent;i++){         pow *= base;     }     cout << pow;          return 0; } Output: Enter base and exponent respectively:  3 3 3^3 = 27 Compute the power of the number by using pow() function from the cmath library. code: #include <iostream> #include<cmath> using namespace std; int main()  {     int exponent;     float base, powe = 1;     cout << "Enter base and exponent respectively:  ";     cin >> base >> exp

CPP Program to find the factorial by recursion

  In this program, the solution of finding the factorial of any number positive number by using the recursion method in the cpp language. For a complete understanding of this code, you must have knowledge of the cpp recursion . Code: #include<iostream> using namespace std; int fact(int n); //declare the function int main() {     int n;     cout << "Enter a positive integer for finding the factorial ";     cin >> n;     cout << "Factorial of the number " << n << "= " << fact(n);     return 0; } int fact(int n) //define the function for factorial {     if(n >= 1)         return n * fact(n - 1);     else         return 1; } Output: Enter a positive integer for finding the factorial 4 Factorial of the number 4= 24

CPP Program concatenate two strings

In this program, we will discuss how to concatenate the two strings in the cpp programming language. Two methods of concatenation are discussed in the following.   By using a string object : If you use a string object to deal with the strings in cpp then this method is useful for you. Code : #include <iostream> using namespace std; int main() {     string str1, str2, sum;     cout << "Enter string str1  ";     getline (cin, str1); //same for second string     cout << "Enter string str2 ";     getline (cin, str2);     sum = str1 + str2;     cout << "Result of two string  = "<< sum;     return 0; } Output: Enter string str1  cpp is best programming Enter string str2 language Result of two string  = cpp is best programming language By using c style string: if you use c style to deal with the string in the cpp then this method is helpful for you. Code: #include <iostream> #include <cstring> using namespace std; int mai

CPP Program copy a string in three different ways

  In this program, discussion about how to copy the string in the three different methods in the cpp programming language. Ist method: string object Code: #include <iostream> using namespace std; int main() {     string str1, str2;     cout << "Enter string s1: ";     getline (cin, str1);     str2 = str1;     cout << "string1 = "<< str1 << endl;     cout << "string2 = "<< str2;     return 0; } Second method:  Copy c string Code : #include <iostream> #include<cstring> using namespace std; int main() {     char str1[100], str2[100];     cout << "Enter string s1: ";     cin.getline(str1,100);     strcpy(str2,str1);     cout << "string1 = "<< str1 << endl;     cout << "string2 = "<< str2;     return 0; } 3RD METHOD : By using loop Code: #include <iostream> #include<cstring> using namespace std; int main() {     char str1[100], str