Posts

Showing posts with the label CExamples

pyramid pattern

Image
 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

Image
  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

Image
 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

Differentiate and add the digits of three digit number

 Here, the code of differentiate and add the digits of a three-digit number entered by the user. Code: //The task is to add the digits of three digit number ,that is input by the user. #include <stdio.h> int main() { int a, b, c, d, e, sum = 0;  //variable declaration printf("Enter a three digit number:"); scanf("%d", &a); // The user input the 3 digit number b = a / 100; //first digit c = a % 100; //last 2 digits d = c / 10;  //middle digit e = c % 10;  //last digit sum = b + d + e; printf("\nThe indvidual digits are : %d\t%d\t%d", b, d, e); printf("\nThe sum is :%d\n", sum); //sum of 3 digits will appear on screen. getchar; return 0; }

Find the number is even or odd

Here,the code of finding the number is even or odd.For understand the following code first understand the following topics. output input operators Code:  //this task is to find the number is even or odd ,that is enter by the user #include <stdio.h> int main() { int a = 0; printf("Please enter the number\n"); scanf("%d", &a); if (a % 2 == 0)  //it means if the remainder come 0 after dividing by2 ,then it is even otherwise it is odd.  printf("\nThe number is Even.\n"); else printf("\nThe number is Odd.\n"); getchar; return 0; }

calculate the health allowance, house rent, and gross salary

  Here ,code of the calculation health allowance,house rent,and gross salary.For understanding this code you should learn first following topics. output input //caluculating health allownce,house rent ,Gross salary   Code #include <stdio.h> int main() { int x = 0, y = 0, z = 0,g=0;  // variable declaration  printf("Enter the Basic Salary\n"); scanf("   %d", &x);  // user input the basic salary printf("                      Salary Details                     \n"); y = (40 * x) / 100;  //to calculate the health allownce printf("     Basic Salary:                             Rs%d\n",x); printf("     Health Allownce:                          Rs%d\n", y); z = (20 * x) / 100;  //to calculate  the house rent printf("     House Rent:                               Rs%d\n", z); g = x + y + z;      //to calculate the gross salary printf("  ________________________________________________________