Wednesday, July 8, 2015

Square Number Values in C language


We are started learning how to write a computer program we learn the fundamentals of programming. This fundamentals are very important because we cannot solve the problem without proper understanding the basic concepts of programming. One of the basic concepts is the study of sequence, variable declarations and the use of iteration or looping statements.  When we talking about sequence we are referring the basic structure of our program the library statements that we declare prior we declare any single variable in our program. 

As a programmer it is also our responsibility to know and understand what type of variables and its corresponding data types to be used in our program. If we don’t know the variable and data type we can use we can generate errors in our program by providing erroneous data and the output information that will generate by our program in also incorrect.

As we said our computer is a tool and machine very dependent to its users and the programs that runs on it. I have also mention the use of iteration or looping statements. This structure in programming is very important also because it minimize the number of lines of code that we use in our program by using this structure we can cut down the number of lines of our codes and the repetitive commands and routine that our program will be perform.

In this article I will discuss another program that I wrote for my class programming activity using C programming language I called this program Square Number Values. This program is very simple yet it show how to use variable declaration, the use of pointers and string declaration. What the program will do is to ask or prompts the user to enter five numbers and then the program will square those values that are being provided by the user and then display the result in the screen. The compiler that I used in this program in Code::Block editor that has a build in compiler called Dev C++ that is freely available to download over the Internet free from any charge.

The first portion of our program code is that it will accept input values from the user. For your information I declare three variables in our program the x,y and the character pointer string that I named words which is also declared as one dimensional array.  What is pointer string will do every time our looping statement run or execute it put string value for example 1 it will attach the string value of st so the output will be 1st, 2nd,3rd,4th and finally the 5th for the input values for the user given to our program.

#include <stdio.h>
#include <stdlib.h>

main()
{
    int x=0,y=0;
    char *words[5] = {"st", "nd","rd","th","th"};
    int values[5];
       printf("\n\n");
       printf("\t ======[SQUARE NUMBER VALUES]=======");
       printf("\n\n");
       for (x=0; x<5; x++)
       {
        printf("Enter %d%s Number : ",x+1,words[x]);
        scanf("%d",&values[x]);
       }

Here is the second and the last portion of our program code what our program will do is to list the values given by the user and then our program will multiply the number b y itself in order to generate its square value equivalent again we are using one dimensional array as our data structure just to gather whose values previously given by our user of our program and also to display the list of values and its computed square values. After that our program will thank the user for using our program and will terminated the operation of our program and return to our windows operating system.

  printf("\n\n");
    printf(" Original Value     Square Values");
    printf("\n\n");
       for (x=0; x<5; x++)
        {
           printf("\n %7d      %13d",values[x], values[x] * values[x]);
        }
    printf("\n\n");
    printf("\t Thank you for using this program.");
    printf("\n\n");
    system("pause");
}

I hope in this article you have learned something the use of variable declaration, understand the usage of pointers and strings and finally the iteration and looping statements to generate square number values. If you have some questions feel free send me an email I am very glad to answer your questions about the articles that I wrote in this website.

If you have some questions please send me an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com.


People here in the Philippines can reach me at my mobile number 09173084360.


Thank you very much and Happy Programming.



Sample Program Output



 Program Listing

#include <stdio.h>
#include <stdlib.h>

main()
{
    int x=0,y=0;
    char *words[5] = {"st", "nd","rd","th","th"};
    int values[5];
       printf("\n\n");
       printf("\t ======[SQUARE NUMBER VALUES]=======");
       printf("\n\n");
       for (x=0; x<5; x++)
       {
        printf("Enter %d%s Number : ",x+1,words[x]);
        scanf("%d",&values[x]);
       }
    printf("\n\n");
    printf(" Original Value     Square Values");
    printf("\n\n");
       for (x=0; x<5; x++)
        {
           printf("\n %7d      %13d",values[x], values[x] * values[x]);
        }
    printf("\n\n");
    printf("\t Thank you for using this program.");
    printf("\n\n");
    system("pause");
}


No comments:

Post a Comment