C Language | If , If-else and For Loop Statements

Friday 21 December 2012


I was playing a game with some kids called "is it hot", when I thought of putting it into a program. This game is mostly played with little kids. All you have to do is assign numbers in each temperature. For example, 1=hot, 2=cold and 3=warm. It is played by saying random numbers but the kids must take note that every time you say 1 or 2 or 3 they must answer you with hot, cold or warm. If you say other numbers except the three they should say, "out of order". This is kind of game is fun if the kids are in groups because they need to work with their teams to win.

As for me, I made a program out of this game to show you how you can put up If Statement, If-else Selection Statement and For Loop Statement in one program. There are certain programs that needs a strong foundation to be more productive especially programs with a lot of selection, decision making and repetitive statements. This example I have is one of those programs. Check out the codes below.


#include<stdio.h>
#include<conio.h>

main()
{
      int a=1, b=1; 
      int count;
      
      printf("Enter how many counts: ");
      scanf("%d",&count);
      
      if(count>30) {
      printf("\nYou have exceeded the maximum input of 30");
      getch();
      return 0;
      }
   

C Language | Area of any Triangle

Wednesday 19 December 2012

Trigonometry is a study of triangles. In trigonometry, there are a lot of theorems, postulates and equations for specific triangles. There are also great mathematicians and philosophers who created formulas for specific topics.

During my first semester in a trigonometry class we were asked to make a program in C language out of the topics we discussed the whole semester. As for me, I made a program that calculates the area of a triangle given only three sides. This program was based on the theorem which Heron, a Greek philosopher and mathematician, created. This theorem is known as the SSS Theorem, made out from the Law of Cosines.

The program I made is basically filled with mathematical equations and statements. So, I included a header file that reads specific mathematical commands and that is math.h. To experience the program, check out the codes below.

#include<conio.h>
#include<stdio.h>
#include<math.h>



int main()
{  
 
    float a,b,c;
    float s,area;
 
    printf("Enter side a: ");
    scanf("%f",&a);
 
    printf("Enter side b: ");
    scanf("%f",&b);
 
    printf("Enter side c: ");
    scanf("%f",&c);
 
    s = (a+b+c)/2;
    area = sqrt(s*(s-a)*(s-b)*(s-c));
 
    printf("\nArea of triangle is: %.2f",area);
 
    getch();
}


The program runs like these. When executed, the program will ask the user to enter three inputs which will be the given sides of a triangle. Then the inputs will be stored on the three storage variables declared above namely a, b and c. These variables will go through the equations. As seen above, there are two equations present, the semi perimeter equation and the equation in finding the area. After executing the equations the program will now show you the result running the printf statement. Then you can now terminate the program. Check out the sample output below.



Hopefully this program helped you a lot. Hope to hear from you soon. Thanks!

C Language | Trigonometric Function SINE

Trigonometry is the study of any triangles. In this, trigonometric functions are present to help us calculate anything from missing areas to missing sides to elevations. There are six (6) Trigonometric Functions namely sine, cosine, tangent, cotangent, secant and cosecant.

In connection to trigonometric functions I made a program that shows you how it works. This program shows you how to calculate for the sine function equivalent of any triangle. This program is used with the header file math.h to allow mathematical commands inside the program. Check out the sample codes below.


     1: #include <stdio.h>
   2:  #include <conio.h>
   3:  #include <math.h>
   4:  # define pi 3.14159265
   5:   
   6:   
   7:  int main()
   8:  {
   9:            int angleA;
  10:            
  11:            printf ("Enter angle: ");
  12:            scanf("%d", &angleA);

C/C++ Language | Check Conversion

Sunday 16 December 2012


ATM machines are now used around the globe because it is the only and convenient way of getting cash immediately especially if you have emergencies. Nowadays, ATM machines are not just for personal banking, it is also used in paying your employees wages or sending money to your relatives. ATM is a card-to-cash basis transaction. Now, as I was thinking about this kind of program I come up with the idea of making a  program that converts the check figures into words to automate a billing system.

This program is used with selection statements and decision making statements namely If Statements and Switch case statements. The program also has a limit input of 100,000,000. Check out the codes below.


 1: #include <stdio.h>
   2:  #include <conio.h>
   3:   
   4:  main()
   5:  {
   6:   
   7:   
   8:  int num;
   9:  int num1,num2,num3,num4;
  10:   
  11:   
  12:  printf("\nEnter Numbers: ");
  13:  scanf("%d", &num);      
  14:            if ( num > 100000000 ) {
  15:            printf("\n\nYou have exceeded the maximum input of 100,000,000.\n");
  16:            }
  17:  if(num<=100000000)
  18:  {
  19:  num1=num/1000 % 10;
  20:  num2=num/100 % 10;
  21:  num3=num/10 % 10;
  22:  num4=num/1 % 10;
  23:   

C Language | Decimal-to-Roman Conversion

Friday 14 December 2012

Centuries ago, people were using the Roman Numerals as their numbers. Today, we are using the Decimal numbers. Before, roman numerals is a specific letter that has an equivalent value. For example, capital letter I indicates that its value is one (1). Though we are not really using roman numerals today, we are still studying about this because this is one of the important history and foundations in mathematics.

Today's generation, there a a lot of kids that have difficulties in studying the roman numerals especially that it is composed of letters. So, there are a lot of confusions going on in the process of learning. Because of this, I made a program that can convert decimal numbers to roman numerals to make it more easy for the kids to learn. This program will also expand the knowledge of the future programmers about decision making statements and repetitive statements. Decision making statement is what we call Switch case statements. While, repetitive statements are the Do-While and For Loop statements. For this program we will be using for loop statement.  To start, check out the codes below.


   9:  #include <stdio.h>
  10:   
  11:  #include <conio.h>
  12:   
  13:   
  14:   
  15:  main()
  16:   
  17:  {
  18:   
  19:   
  20:   
  21:  int count;
  22:   
  23:  int b;
  24:   
  25:   
  26:   
  27:  int num=1;
  28:   
  29:  int num1,num2,num3,num4;
  30:   
  31:        
  32:   
  33:        printf("Enter how many counts: ");
  34:   
  35:        scanf("%d",&count);

C Language | Area of a Circle

Sunday 9 December 2012

In Geometry, there are a lot of shapes that has different sides and sizes but there is one shape that has no sides and that is circle. A circle has an infinity line because of its round shape. The area of a circle is the simple thing you can calculate because of its simple equation. You only need to multiply the radius and pi then multiply again the result to the radius: a = ((PI * r) * r), respectively.

To calculate the area of a circle easily I made a program out of its equation. Since we're going to use PI anywhere in the program we need to declare it globally, meaning we need to declare it out of the main program. Check out the sample code below.


    1: #include <stdio.h>
   2:  #include <conio.h>
   3:  #define PI 3.141
   4:   
   5:  int main()
   6:  {
   7:    float r, a;
   8:   
   9:    printf("Radius: ");
  10:    scanf("%f", &r);
  11:   

C Language | Peso-Dollar Conversion

Saturday 8 December 2012


Most Filipinos have relatives who migrated abroad especially in the US. Most of them migrated because they married foreigners, others migrated because of petitions and there are others who migrated because of work. Being a relative living abroad, there are instances that you need to help your relatives back in your hometown and that means sending money. People receiving money from their relatives living in the other countries, especially first timers, tend to confuse the value of the money they receive. US Dollars is one of the currency holding a high currency exchange rate value in the Philippines. That means one (1) US Dollar can be converted to atleast forty (40) Php. But there are instances that the value of US Dollars change due to economic changes as well.

To prevent confusions and to help first timers understand the difference of the two currencies, I made a program that will help people convert currencies easily depending on how much the value of US Dollars are in the Philippines. This program is used with three functions namely main, display and convert. Main function will hold the main program and the other functions hold the conversion and the currency being converted. Check out the codes below.


   1:   
   2:  #include<stdio.h>
   3:  #include<conio.h>
   4:   
   5:  float convert(float pesoValue, int choice, float amount);
   6:  void display(int choice, float result);
   7:   
   8:  int main()
   9:  {
  10:      int choice;
  11:      float pesoValue, amount, result = 0.0;
  12:   
  13:          printf("USD 1 is equivalent to Php ");
  14:          scanf("%f", &pesoValue);

C Language | Surface Area and Volume of a Cube

Friday 7 December 2012

When we play board games there will always be a dice that holds a series of numbers that represents how many our moves must be in the game. A dice is shaped like a cube, it has six equal sides. Cube shaped things are seen everywhere. I was always amazed by the cube because of its special form and unique shape, because of this I always wonder how to calculate its area and volume considering how big it can be.

As I was going through my High School and College years I learned how to calculate the area and volume of a cube and so much more things. To share and help you with calculating the area and volume of a cube I made a program that allows a user to calculate the said areas easily. Check out the sample code below.

     1:  
   2:  #include <conio.h>
   3:  #include <stdio.h>
   4:  #include <math.h>
   5:   
   6:  float sol1(float y, float n1)
   7:  { 
   8:          return y = n1 * n1 * n1;
   9:         
  10:          
  11:        }
  12:  float sol2(float z, float n1)
  13:  { 

C Language | Prime Numbers

Thursday 6 December 2012

In numbers, there are those that can be divided with different other integers and those that are only limited to the integer itself and number one (1). That kind of numbers are known as the Prime Numbers. Prime numbers can only be divided by the number one (1) or the prime number itself.

Sometimes, it can be difficult for us to recognize prime numbers specially when the numbers are large. That's why I made a program that evaluates a number and tells us if the number is a prime number or not. In this program, I used looping statements and If else statements to make the program more stable. Check out the sample code below.

     1:  
   2:  #include<stdio.h>
   3:  #include<conio.h>
   4:   
   5:  main()
   6:  {
   7:        int input, num=0, div, count, count2,x;
   8:        printf("Enter how many counts: ");
   9:        scanf("%d",&count);
  10:        
  11:        if(count>3000){
  12:        printf("You have exceeded the maximum input of 3000");
  13:        getch();
  14:        return 0;
  15:        }

C Language | For Loop Statement

Wednesday 5 December 2012


After I showed you how the Do-While Loop executes, I will now show you the second looping statement which is more easier to use, the For Loop. For Loop repetition statement is a one line argument. It holds the control variable, condition and the increment of control variable in one line/argument, which makes it more easy. For Loop is used on most programs because it helps a lot in making a program short and straight.

Now, I made a sample program to show you how the for loop works. The sample I made is the same example I had in the do-while loop. I just changed it to for loop statement. In this example, you will observe the difference between the do-while and for loop statements and how the codes are arranged. Check out the codes below.

     1:  
   2:  #include <stdio.h>
   3:  #include <conio.h>
   4:   
   5:  int main()
   6:  {
   7:  int n;
   8:  int value = 1;

C Language | Do-While Loop Statement


From my previous codes, we encountered looping statements that helped us with the repetitive parts of the program. Now, I will introduce you the two looping statements that is very useful in program control. These looping statements are the Do-While Loop and For Loop.

Now, I will show you the Do-While Loop. Do-While Loop processes the statement once before processing the condition. Do-While loop is used on programs that statements are needed to be executed first before processing the condition. To show you how the do-while loop work, I made a simple program that will execute it. Check out the codes below.

     1:  
   2:  #include <stdio.h>
   3:  #include <conio.h>
   4:   
   5:  int main()
   6:  {
   7:  int n;
   8:  int generate = 1;
   9:   
  10:  printf("Enter number: ");
  11:  scanf("%d", &n);
  12:   
  13:  do
  14:  {

C Language | Subtraction


On my previous topic, I was sharing about basic mathematics. I already show you how to add two integers and how to add a couple of integers being manipulated through your inputs and looping statements. Now, basic mathematics will not be complete without subtraction. Subtraction is the process of taking out certain amount from another amount. To understand more of this process I made a program that subtracts numbers. It also acts like a calculator.

This program, like the previous addition program I made, is used with a data type int because we're using integers, which needs to be whole numbers as inputs. Check out the codes below.


     1:  
   2:  #include<stdio.h>
   3:  #include<conio.h>
   4:   
   5:  int main()
   6:  {
   7:  int a,b,c;
   8:   
   9:  printf("Enter number: ");
  10:  scanf("%d", &a);
  11:   
  12:  printf("\nEnter number: ");

C Language | Addition part 2


Last time, I showed you how to create a simple addition program using C language. Now, we will still be adding numbers but I put something new in it. Yes, we will still add integers/numbers but it is now in the users decision how many integers he will add.

We will still use int as our data identifier because we are still inputting integers. The new command we're going to put into the code is the for loop statement. This statement will control how many inputs a user want. From the name itself, loop, it means infinity. For loop is a repetitive statement, the program will not stop/end until the argument inside the loop is met. This statement is useful when you have to make repetitive programs because this will excuse you from doing the same code again and again.

The program you see below is an example of adding numbers but inputs can be manipulated by the user. Check out the codes below.


   1:   
   2:   
   3:   
   4:  #include <stdio.h>
   5:  #include <conio.h>
   6:   
   7:  int main()
   8:  {
   9:      int a, b = 0, c = 1, sum = 0;
  10:   
  11:      printf("How many numbers you want to add?: ");
  12:      scanf("%d", &a);

C Language | Addition part 1

Wherever we went we always encounter math, either when you're at home, in school or even in the market. Most of time, basic mathematics is always useful like addition and subtraction. Before, adding or subtracting is used with beads or stones or whatever things that might represent the figure all of it can be useful. Then, new generation has come and calculators were born. Today, computers are used. So, I thought of making a program that can add numbers just like a calculator using C language.

The program is designed to add two (2) numbers/integers using the basic mathematics and C language commands. Check out the codes below.



     #include<stdio.h>
     #include<conio.h>
     
     int main()
    {
     int a,b,c;
     
     printf("Enter number: ");
     scanf("%d", &a);
     
     printf("\nEnter number: ");
     scanf("%d", &b);
    
     c = a + b;
    
     printf("\nThe sum is %d", c);
     getch();
     }

C Language | Hello World a start with programming

Monday 3 December 2012

C language is a basic programming language used to create simple to complex programs. C has different kinds of commands such as pre-processors, arrays, strings, if else statements and switch case statements. During my first lecture in this subject, programming, my professor introduce to us C language using the well known Hello World program. So, to introduce to you C language I will show you the Hello World program.

In this program, we used simple codes. We have there the pre-processor command which is the #include and the header files stdio.h and conio.h. There is also our command for output which is the printf. Lastly, to terminate the program getch(); is the key for that. It prompts user to input any character that will not show but rather to terminate the program. Check out the codes below.


   1:  #include <stdio.h>
   2:  #include <conio.h>
   3:   
   4:  int main()
   5:  {
   6:  printf("Hello World!");
   7:  getch();
   8:  }

Related Posts Plugin for WordPress, Blogger...