Periodic Table C Program.

Standard

Hey, I am happy to upload my first program on the internet. This program is written in C language. It is a program to find Atomic numbers of certain elements. Read the ‘Readme’ file to know how I made this program.

Download the source code and the text file here:-

Download periodic table project

For the execution of this program, all these files are required and should be saved in the same directory. If you have downloaded the source code open the bcpt.c file with a text editor. I have first declared three variables along with their appropriate data types , for the storage of all the elements of periodictable.txt, b , to store the element specified by the user, and a , to store the matched string after scanning the text file.

By specifying file pointer FILE *fp the text file periodictable.txt is opened in read mode.

     fp=fopen(“Periodictable.txt”,“r”);

I have also put up an ‘if’ check to notify the user if the text file does not exist in the main directory. If the file has been removed the program exits the execution.

When the file is open it gets scanned line by line until the End of file isn’t encountered and all of its data gets stored in the variable e. Meanwhile, we ask the user to enter an element and it gets scanned and the stored in variable b.

      while(fscanf(fp,“%s”,e)!=EOF){

The string compare function strcmp() is used in order to compare the strings stored in variable e and variable b. If they match that line is returned to the main.

The while loop specifically helps strcmp() in comparing each and every line one at a time until the end of the file and of the comparison matches the index of that particular file is returned and stored in another variable b. Later this variable is displayed on to the terminal or cmd with printf() function.

If the variable doesn’t exist in the text file then the else part is executed and you can printf() that the element entered by the user does not exist. I have used a flag found for stating the true or false as per the existence of the file. The strcmpi() lets the user a freedom to enter the element in upper case and lower case.

img

     while(fscanf(fp,"%s",e)!=EOF){
       if(strcmpi(b,e)==0){     /*Compare string one by one*/
            fscanf(fp,"%d",&a);
            found=1;
            break;}
              }
       if(found==1)
            printf("The atomic number of %s is %d .\n",b,a);
       else
            printf("The input element does not exist.”);

This program can be used for lots of other things like finding the values of certain physical constants like gravitational constant ‘G’. Also, you can access lots of other kinds of data with this program, you just have to edit the text file and the source file according to your needs.
Execute on Windows :- Open Command Prompt and (Change to the bcpt.c file’s directory), type :

> gcc bcpt.c
> a/.out

Execute on Linux :- Open Terminal and type :

$ gcc bcpt.c
$ ./a.out

Output:-

Console

The Modern Periodic table is referred from Wikipedia. If you find any bugs or have any suggestion to this program comment below and let me know. Happy programming !

Advertisements

2 thoughts on “Periodic Table C Program.

Leave a Comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s