Posts

Showing posts with the label CExamples

C Program convert octal to binary number

Here, you will find the solution on how to convert the octal number to binary number in the c programming language. Following is the solution to the problem: Code: #include <math.h> #include <stdio.h> int main() {     int octal,original;     printf("Enter an octal number: ");     scanf("%d", &octal);   original=octal;     int deci = 0, i = 0;     long long bini = 0;     // convert octal to decimal     while (octal != 0) {         deci += (octal % 10) * pow(8, i);         ++i;         octal /= 10;     }     int a = 1;    // convert decimal to binary     while (deci != 0) {         bini += (deci % 2) * a;         deci /= 2;         a*= 10;     }         printf("%d in octal = %lld in binary", original, bini);     return 0; } Output: Enter an octal number: 25 25 in octal = 10101 in binary

C program to convert binary to octal number

Here,we will find the solution of the conversion of the binary to the octal number system in the c programming language. Following is the solution of conversion: Code: #include <math.h> #include <stdio.h> int main() {     long long bina,original;     printf("Enter a binary number ? ");     scanf("%lld", &bina);          original=bina;     int oct = 0, deci = 0, i = 0;     // convert binary to decimal     while (bina != 0) {         deci += (bina % 10) * pow(2, i);         i++;         bina /= 10;     }     int j = 1;     // convert to decimal to octal     while (deci != 0) {         oct += (deci % 8) * j;         deci /= 8;         j *= 10;     }  printf("%lld in binary= %d in octal", original, oct);    return 0; } Output: Enter a binary number ? 10101 10101 in binary= 25 in octal

C Program convert octal to decimal Number

In this example,we will discuss how to convert octal to decimal number system in the c programming language. Following is the solution to the problem. Code: #include <stdio.h> #include <math.h> using namespace std; int main() {    int octalNumber,originalnumber;     printf("Enter a octal number: ");     scanf("%d", &octalNumber);   originalnumber=octalNumber;     int decimalNumber = 0, i = 0, remainder;     while (octalNumber != 0)      {            decimalNumber += (octalNumber%10) * pow(8,i);         ++i;         octalNumber/=10;     }      printf("%d in octal = %d in decimal", originalnumber, decimalNumber);    return 0; } Output: Enter a octal number: 74 74 in octal = 60 in decimal

C Program convert decimal to octal Number

In this example,we will discuss about how to convert decimal to octal in the c programming language.Following is the solution of the problem. code: #include <stdio.h> #include <math.h> using namespace std; int main() {    int deciNumber,originalnumber;     printf("Enter a decimal number: ");     scanf("%d", &deciNumber);   originalnumber=deciNumber;     int octalnumber = 0, i = 1, remainder;     while (deciNumber != 0)      {            remainder = deciNumber % 8;         deciNumber /= 8;         octalnumber += remainder * i;         i *= 10;     }      printf("%d in decimal = %d in octal", originalnumber, octalnumber);    return 0; } Output: Enter a decimal number: 60 60 in decimal = 74 in octal

C program to convert decimal to binary number

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

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.

convert lower to upper case

string conversion:  Here is the code of convert lower case string to upper case in the c programming language. The concept of ASCII involves in the conversion of string.   code: #include<stdio.h> #define size 100   void convet1st2Capital(char str[]); int main(void){ char str[size]; printf("Enter the string to capitalize initials:\n"); gets(str); convet1st2Capital(str); } void convet1st2Capital(char str[]){ int s = 0; while (!(str[s] == '\0')){ if (str[s] >= 'a' && str[s] <= 'z'){ str[s] -= 32; }   s++; } printf("%s", str); } Output: In this example,declare the global variable for size of character array and function to convert the lower case string to upper case string . In the function, the loop is used to check the lower case character and convert into the upper by subtracting the 32 in the character ASCII.In the last,display the string .

To check strong number in c program

Strong number The sum of the individual digit factorial is equal to the number itself is known as a strong number.For example, 145 is a strong number. Here is the code of the strong number. code: #include<stdio.h> int factorial(int);           // declaration of factorial function int factorial(int i) { int factorial= 1; int fact = 1; for (fact = 1; fact <= i; fact++) factorial = factorial*fact; return(factorial); } int main() { int num = 0;           //to store user input int digit1 = 0; int temp = 0; int digit2 = 0; int digit3 = 0; int strong = 0; char choice ;           // variable to continue or break                     //This loop will continue till the user pressing y and othewise it will break while (1){ printf("Enter the number, to check whether it is strong number or not :  "); scanf("%d", &num); digit1 = num / 100; temp = num % 100; digit2 = temp / 10; digit3 = temp % 10; strong = factorial(d

Calculator with function in c

Functions in c  Here is the code of the calculator with help of function in the c programming language code: #include <stdio.h> int sum(int, int);           //Declaration of function for sum int minus(int, int);           //Declaration of function for subtraction int multiply(int, int);      //Declaration of function for product int divide(int, int);         //Declaration of function for division int main() { int num1, num2;           //num1 and num2 declare to store values of user, which he want to calculate char operation;           //operation declare to ask the user what operation he want to use int ans = 0;                //this is used to store the answer after the calculation of function printf("\n Enter the num1 :\t"); scanf_s("%d", &num1); printf("\n Enter the num2 :\t"); scanf_s("%d", &num2); printf("\nEnter 1 for sum, 2 for minus, 3 for multiply, 4 for divide :\t"); scanf_s(" %c", &

pyramid pattern

How to print pyramid Here is the code for the printing the pyramid pattern in the c programming language. code: //print the pyramid pattern by taking input from the user #include <stdio.h> int main() { int space = 0; int star = 0; int lines = 0; int user = 0; int starplus = 0; printf("Enter the number of lines  :"); scanf("%d", &user); printf("\n"); starplus = user; while (user != -1){ for (lines = 1; lines <= user; lines++){ for (space = 1; space < lines; space++) printf(" "); for (star = 1; star <= starplus; star++){ printf("* "); } starplus--; printf("\n"); } printf("Enter the number of lines  :"); scanf("%d", &user); printf("\n"); starplus = user; } getch(); return 0; } In this example, three for loop is used with different counters.one is used for first spacing and second is used to print the  as

Print the pattern

How to print pattern  Here is the printing of the pattern of X  code: //print the X pattren in the c programming language with steriek #include <stdio.h> int main() { int lines = 0; int i = 0; int j = 0; lines = 9; for (i = 0; i < (lines); i++){ for (j = 0; j < (lines); j++){ if ((i == j) || (i + j == lines - 1)) printf("*"); else printf(" "); } printf("\n"); } getch(); return 0; }

Find the Factorial of the numbers

Here is an example of finding the factorial of any number in the c programming language. code: //how to find the factorials of the numbers #include<stdio.h> int main() { int n = 0;           //variable declare to store the value of user  int fact = 1;           //variable declare to calculate factorials printf("Calculating the factorial of the number entered by the user.\n"); printf("Enter the number:"); scanf("%d", &n); while (n) { fact = fact*n; n--; } printf("\nfact of given number is  %d.\n", fact); getchar; return 0; } In the above code, two int variables fact  and  n.n  is used to store the number of the user wants to find the factorial, which is used to store the value of the factorial. After taking input from the user by the input method, If you do not about the input method visit how to input. While loop used, in the body of the loop multiple the counter n with fact and store in the same variabl

Draw Triangles

Here,the code of the draw two triangles by taking input of numbers of rows from the user. Code: /*This program can correctly print the both triangles till 10 rows.  And in this program all the variable names are appropriate and can be easily understand*/ #include <stdio.h> int factorial(int); //This is factorial function. int main(){ int singlerow = 0; //This variable is for single row. And it will print each row continuously. int noofrows = 0; //This variable is for user input. Means what no of rows want to print? int beforetrianglespaces = 0;                                              //This variable used to print spaces before each line of triangle.  int n = 0;                                                        //As in formula, it is same variable. And it is for rows number.  int k = 0;                                    //As in formula, it is same variable. And it is for each row and it start from 0 and end to number of row which is presently continue. int