Posts

Showing posts with the label CExamples

code of Game of Chance

   A Game of Chance One of the most popular games o in casinos and back alleys triangular  games of chance is a dice game known as “craps,”  It is played in hack alleys throughout the world.  Following are rules of the Game: roll two dice. Each die has six faces. These faces contain one, two, three, four, five, and six spots.  the dice  to rest, the sum of the spots on the two upward faces is calculate. If sum is 7 or 11 on the first throw, the player wins.  If the sum is two, three, or twelve on the first throw Called "craps"),  the player loses (mean the "house" wins).  If the sum is four, five, six, eighth, nine, or ten on the first throw, then that sum becomes the player's "point.”  To win, you must continue rolling the dice until you make your points. The player loses by rolling a 7 before making the point.  Code of the game:   #include<stdio.h>  #include<stdlib.h>   #include<time.h>  /*contains prototype for function time */ enum St

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

Image
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

Image
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", &