Thursday, March 29, 2018

List Files in Directory


Following C Program will display the List of Files present in current directory. This C program is stored in D:\\CProgramPracticals\ directory; hence it will display all files stored in this directory.

Program to Display List of Files in Current Directory

#include <stdio.h>
#include <dirent.h> 
int main()
{
    DIR *my_dir;
    struct dirent *temp_dir;
    my_dir = opendir(".");
    if (my_dir)
    {
        while ((temp_dir = readdir(my_dir)) != NULL)
        {
            printf("%s\n", temp_dir->d_name);            
        }
        closedir(my_dir);
    }
    return 0;
}



Output of Program

.
..
Armstrong-number.c
bell-using-ascii-7.c
bitcoin_profitloss.c
bitcoin_profitloss.exe
blink.c
cprogrampracticals.h
digitsToWords.c
digitsToWords.exe
EMICalulator.c
EMICalulator.exe
leapYear.c
leapYear.exe
LifeInsurancePremiumCalculator.c
LifeInsurancePremiumCalculator.exe
piano.c
ReadIPAddress.c
ReadIPAddress.exe
StockBroker.c
StockClientBill.c
StockClientBill.exe
swap-variable.c
SystemDateTime.c


Saturday, March 24, 2018

Program Execution Time


This JAVA program will help you to find total time taken by FOR loop of the program. You can extend this JAVA program to find total execution time of the program.

Program will display Total time taken in milliseconds. 

Program Execution Time

class TimeTaken 
{
    public static void main(String[] args) 
    {
        long startTime = System.currentTimeMillis();
        long sum = 0;
        for (int i = 0; i < 10000000; i++) 
        {
            sum = sum + i;
        }
        long endTime = System.currentTimeMillis();
        long timetaken = endTime - startTime;

        System.out.println("FOR loop execution time = " + timetaken + " milliseconds.");
    }
}

Output of program

FOR loop execution time = 14 milliseconds.

Tuesday, March 20, 2018

Bitcoin Trading Profit Loss


Bitcoin Trading Profit Loss Calculation.

Following C Program will help you to calculate your Bitcoin Trading  profit or loss.

#include<stdio.h>

int main(){

   float current_btc_price, avg_cost;
   float total_btc, profitloss;

   printf("Enter Your Total Bitcoin: ");
   scanf("%f",&total_btc);

   printf("Enter Current Bitcoin Price: ");
   scanf("%f",&current_btc_price);

   printf("Your Average Bitcoin Purchase Price: ");
   scanf("%f",&avg_cost);

   //profitloss = (current_btc_price - averagecost) x total_btc

   profitloss = (current_btc_price - avg_cost) * total_btc;

   if(profitloss > 0)
printf("Bitcoin Trading Profit is: %0.2f",profitloss);
   else
printf("Bitcoin Trading Loss is: %0.2f",profitloss);

   return 0;
}


Output of program:

Enter Your Total Bitcoin: 2
Enter Current Bitcoin Price: 250000
Your Average Bitcoin Purchase Price: 150000
Bitcoin Trading Profit is: 200000.00


Friday, March 16, 2018

Networking Protocol Header


Various protocols like TCP, UDP, IP, IPSec, HTTPs etc have their own header. Following program can be extended to create full protocol header.

Write a C program to generate protocol header with following set of parameters into it:
  •  First parameter is Source IP Address
  •  Second parameter is Destination IP Address
  •  Third parameter is total number of characters present in input.txt file.
#include<stdio.h>
#include<conio.h>

int main()
{

FILE *input,*out;
int c,counter=0;
//Source Add
char source[]="192.168.1.1";
//Destination Add
char dest[]="192.168.1.20";

input= fopen("input.txt","r");
out=fopen("output.txt","w");

if(input==NULL){
printf("Input File Not found");
}
else if(out==NULL){
printf("Output file not Created");
}
else{
//Counting No of charecter in files
do{
c=getc(input);
counter++;
}while(c!=EOF);

counter= counter-1;
//Generatting Headers

fprintf(out,"%s,%s,%d",source,dest,counter);
printf("Header generated Successfully.\nCheck output.txt file.");

fclose(input);
fclose(out);

return 0;
}
}


Output of program:

Header generated Successfully.
Check output.txt file.


Friday, March 9, 2018

Life Insurance Premium Calculator


Life Insurance Premium Calculator

This practical will calculate monthly, quarterly, half-yearly and yearly life insurance premium based on given sum assured.

Life Insurance Premium is the amount of money that an individual or business must pay for their life insurance policy. This insurance premium is income for the insurance company, and represents a liability of the company to provide coverage for claims being made against the life insurance policy.

#include<stdio.h>
int main()
{
float sum_assured, years, monthly_premium
float quarterly_premium, half_yearly_premium, yearly_premium;

printf("Enter your sum assured:");
scanf("%d", &sum_assured);

printf("Enter life insurance policy term(in years):");
scanf("%d", &years);

printf("Your Monthly Premium    : %8.2f\n", sum_assured/(years*12));
printf("Your Quarterly Premium  : %8.2f\n", sum_assured/(years*4));
printf("Your Half Yearly Premium: %8.2f\n", sum_assured/(years*2));
printf("Your Yearly Premium     : %8.2f\n", sum_assured/(years));
printf("\nNote: Actual premium may vary because of various charges imposed by company.");
return 0;
}

Output of program

Enter your sum assured:250000
Enter life insurance policy term(in years):5
Your Monthly Premium    :  4166.67
Your Quarterly Premium  : 12500.00
Your Half Yearly Premium: 25000.00
Your Yearly Premium     : 50000.00

Note: Actual premium may vary because of various charges imposed by company.
* * * * *



Note: Insurance premiums charged by the insurance companies is determined by many parameter including statistics and mathematical calculations. The premium charged to a client is also depends on statistical data that exists about age, health and life history.

Thursday, March 8, 2018

Stock Broker


Stock Broker Client Billing C Program

This C program is simulation of one of the function of Online Stock Trading Software. Individual stock broker can print a client bill.


A stock broker is a professional generally associated with a brokerage firm or stock market broker dealer, who buys and sells stocks (shares) and other financial instruments (like Mutual Funds, Insurance, FDs, Bonds, etc. ) for both retail and institutional clients.

/* Stock Broker Client Billing C Program. Individual stock broker can print a client bill. */

#include<stdio.h>
#include<time.h>
int main()
{
int stock_quantity, client_id, brokerage = 45; 
float stock_price, total;
char choice, stock_name[20], client_name[20];
time_t t;
time(&t);


printf("Enter Client ID   : ");
scanf("%d",&client_id);

printf("Enter Client Name : ");
scanf("%s", client_name);

printf("Enter Stock Name  : ");
scanf("%s", stock_name);

printf("Enter Stock Quantity : ");
scanf("%d", &stock_quantity);

printf("Enter Stock Price    : ");
scanf("%f", &stock_price);

BACK:

printf("Enter your choice (B for Buy, S for Sell):");
scanf(" %c", &choice);

if(choice!='B' && choice!='S')
{
printf("\n"); goto BACK;
}

total = stock_quantity * stock_price;

printf("Collect Your Bill As Under...\n");

printf("========================================\n");
printf("      STOCK BROKER - CLIENT BILL\n");
printf("      --------------------------\n");
printf("Date and Time :%s",ctime(&t));
printf("Client ID and Name : %d, %s\n",client_id, client_name);
printf("----------------------------------------\n");
printf("Stock Name\t\t : %s \n", stock_name);
printf("Stock Quantity\t\t : %d\n", stock_quantity);
printf("Stock Price\t\t : %0.2f\n", stock_price);
printf("----------------------------------------\n");
printf("Total Stock Transaction Amount = %0.2f\n", total);
if(choice=='B')
printf("You will pay us INR %0.2f.\n", total+brokerage);
if(choice=='S')
printf("We will pay you INR %0.2f.\n", total-brokerage);
printf("========================================");

return 0;
}

Output of program

Enter Client ID   : 121
Enter Client Name : Mr.Patel
Enter Stock Name  : RELIANCE
Enter Stock Quantity : 10
Enter Stock Price    : 1251.75
Enter your choice (B for Buy, S for Sell):
Collect Your Bill As Under...
========================================
      STOCK BROKER - CLIENT BILL
      --------------------------
Date and Time :Thu Mar 08 20:08:48 2018
Client ID and Name : 121, Mr.Patel
----------------------------------------
Stock Name               : RELIANCE
Stock Quantity           : 10
Stock Price              : 1251.75
----------------------------------------
Total Stock Transaction Amount = 12517.50
You will pay us INR 12562.50.
========================================


Tuesday, March 6, 2018

EMI Calculator

This C program will help you to calculate your EMI. EMI means  Equated Monthly Installment. You can also use this C program to calculate monthly loan installment or personal loan estimator. 

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

int main()
{
    float principal_amount, roi, time, emi;

    printf("Enter your principal amount (INR): ");
    scanf("%f", &principal_amount);

    printf("Enter rate of interest: ");
    scanf("%f",&roi);

    printf("Enter your loan time in years: ");
    scanf("%f",&time);

    roi = roi/(12*100); /*interest for one month*/
    time=time*12; /*time for one month*/

    emi= (principal_amount*roi*pow(1+roi,time))/(pow(1+roi,time)-1);

    printf("Monthly EMI (INR) = %0.2f\n",emi);
   
    return 0;
}

Output of program

Enter your principal amount (INR): 50000
Enter rate of interest: 12
Enter your loan time in years: 5
Monthly EMI (INR) = 1112.22