Posts

C Program to convert binary to decimal number

 In this example, we will learn how to convert the binary to decimal number in the c programming language. Following is the solution to the problem. code: #include <math.h> #include <stdio.h> int main() {     long long num,original;     printf("Enter a binary number: ");     scanf("%lld", &num);     original=num;          int deci = 0, i = 0, remainder;     while (num != 0) {         remainder = num % 10;         num /= 10;         deci += remainder * pow(2, i);         ++i;     }     printf("%lld in binary = %d in decimal", original, deci);     return 0; } Output: Enter a binary number: 111 111 in binary = 7 in decimal

C Program to swap the two numbers

In this example, discuss how to swapping the number in the c programming language. Following is the solution of the problem. Code: #include<stdio.h> int main() {    int firstnum, secondnum, temp;       printf("Enter firstnum number: ");       scanf("%d", &firstnum);       printf("Enter secondnum number: ");       scanf("%d", &secondnum);       // Value of firstnum is assigned to temp       temp = firstnum;       firstnum = secondnum;       // Value of temp  is assigned to secondnum       secondnum = temp;       printf("\nAfter swapping, firstnumNumber = %d\n", firstnum);       printf("After swapping, secondnumNumber = %d", secondnum);       return 0; } Output: Enter firstnum number: 4 Enter secondnum number: 5 After swapping, firstnumNumber = 5 After swapping, secondnumNumber = 4 Here,is another way of swapping the two numbers without using helping variable. Code: swapping the two numbers without helping variable

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