Monday, 8 January 2018

ATM Transaction program code using C

    /*
     * C Program to Display the ATM Transaction
     */
    #include <stdio.h>
     unsigned long amount=1000, deposit, withdraw;
    int choice, pin, k;
    char transaction ='y';

    void main()
    {
        while (pin != 5005)
        {
            printf("ENTER YOUR SECRET PIN NUMBER:");
            scanf("%d", &pin);
            if (pin != 5005)
            printf("PLEASE ENTER VALID PASSWORD\n");
        }

        do
        {
            printf("********Welcome to ATM Service**************\n");
            printf("1. Check Balance\n");
            printf("2. Withdraw Cash\n");
            printf("3. Deposit Cash\n");
            printf("4. Quit\n");
            printf("*******************************************\n\n");
            printf("Enter your choice: ");
            scanf("%d", &choice);
            switch (choice)
            {
            case 1:
                printf("\n YOUR BALANCE IN Rs : %lu ", amount);
                break;
            case 2:
                printf("\n ENTER THE AMOUNT TO WITHDRAW: ");
                scanf("%lu", &withdraw);
                if (withdraw % 100 != 0)
                {
                    printf("\n PLEASE ENTER THE AMOUNT IN MULTIPLES OF 100");
                }
                else if (withdraw >(amount - 500))
                {
                    printf("\n INSUFFICENT BALANCE");
                }
                else
                {
                    amount = amount - withdraw;
                    printf("\n\n PLEASE COLLECT CASH");
                    printf("\n YOUR CURRENT BALANCE IS%lu", amount);
                }
                break;
            case 3:
                printf("\n ENTER THE AMOUNT TO DEPOSIT");
                scanf("%lu", &deposit);
                            amount = amount + deposit;
                printf("YOUR BALANCE IS %lu", amount);
                break;
            case 4:
                printf("\n THANK U USING ATM");
                break;
            default:
                printf("\n INVALID CHOICE");
            }
            printf("\n\n\n DO U WISH TO HAVE ANOTHER TRANSCATION?(y/n): \n");
            fflush(stdin);
            scanf("%c", &transaction);
            if (transaction == 'n'|| transaction == 'N')
                        k = 1;
        } while (!k);
        printf("\n\n THANKS FOR USING OUT ATM SERVICE");
    }


Binary Search using C

//Program for Binary Search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],flag=0,i,n,top,mid,bottom,key;
clrscr();
printf("\nEnter size of the array:");
scanf("%d",&n);
printf("\nEnter elements into the array in sorted order:");
for(i=0;i<n;i++)
 scanf("%d",&a[i]);
printf("\nEnter element to be searched:");
scanf("%d",&key);
top = 0;
bottom = n-1;
while(top <= bottom && flag == 0)
 {
  mid = (top + bottom) /2 ;
  if(a[mid] == key)
  flag = 1;
else if(key>a[mid])
  top = mid + 1;
else
  bottom = mid - 1;
}
if(flag == 1)
  printf("\nSearch element %d found at position %d",key,mid+1);
else
  printf("\nSearch element %d is not found.",key);
}

Linear Search program in C

//program for linear search
#include<stdio.h>
void main()
{
int a[10],n,i,search,flag=0;
clrscr();
printf("How many numbers:");
scanf("%d",&n);
printf("\nEnter the numbers :");
for(i=0;i<n;i++)
 scanf("%d",&a[i]);
printf("\nEnter the element to be searched:");
scanf("%d",&search); //reading search element
 for(i=0;i<n;i++)
 {
  if(a[i]==search) // comparing search element to the
   { //element in the array list.
     flag=1;
     break; //if element is found make flag=1
   }
 }
 if(flag==1) //if flag=1 it means element found
  printf("\nElement found at position %d",i+1);
 else //i+! is the element position
  printf("\nElement Not Found");
getch();
}


Bubble Sort program code using C language

//Program for Bubble Sort
#include<stdio.h>
void main()
{
int a[10],n,i,j,t;
clrscr();
printf("How many numbers:");
scanf("%d",&n);
printf("Enter the numbers :");
for(i=0;i<n;i++)
 scanf("%d",&a[i]);
printf("The sorted list ...");
 for(i=0;i<n-1;i++) //for ‘n’ elements n-1 passes
 {
  for(j=0;j<n-1-i;j++) //and each time n-1-I comparisons
  {
   if(a[j]>a[j+1])
    {
    t=a[j];
    a[j]=a[j+1];
    a[j+1]=t;
    }
  }
 }
for(i=0;i<n;i++)
 printf("\n\t\t%d",a[i]);
getch();
}


Saturday, 6 January 2018

C Program code to display the inventory of items in a store / shop

    /*

     * C program to display the inventory of items in a store / shop

     * The inventory maintains details such as name, price, quantity

     * and manufacturing date of each item.

     */

    #include <stdio.h>
 
    void main()

    {
        struct date

        {
            int day;
            int month;
            int year;
        };

        struct details

        {
            char name[20];
            int price;
            int code;
            int qty;
            struct date mfg;
        };

        struct details item[50];

        int n, i;

        printf("Enter number of items:");
        scanf("%d", &n);
        fflush(stdin);
        for (i = 0; i < n; i++)
        {
            fflush(stdin);
            printf("Item name: \n");
            scanf("%s", item[i].name);
            fflush(stdin);
            printf("Item code: \n");
            scanf("%d", &item[i].code);
            fflush(stdin);
            printf("Quantity: \n");
            scanf("%d", &item[i].qty);
            fflush(stdin);
            printf("price: \n");
            scanf("%d",  &item[i].price);
            fflush(stdin);
            printf("Manufacturing date(dd-mm-yyyy): \n");
            scanf("%d-%d-%d", &item[i].mfg.day,
            &item[i].mfg.month, &item[i].mfg.year);
        }

        printf("             *****  INVENTORY ***** \n");
    printf("--------------------------------------------\n");

    printf("S.N.| NAME |CODE |QUANTITY|PRICE | MFG.DATE \n");

    printf("--------------------------------------------\n");

    for (i = 0; i < n; i++)

    printf("%d  %-15s  %-d  %-5d  %-5d %d/%d/%d \n", i + 1, item[i].name, item[i].code, item[i].qty,

        item[i].price, item[i].mfg.day, item[i].mfg.month,
        item[i].mfg.year);

    printf("--------------------------------------------\n");

    }

Friday, 5 January 2018

Decimal number and convert it to binary and count the number of 1's in the binary number in C

/*
 * C program to accept a decimal number and convert it to binary
 * and count the number of 1's in the binary number
 */
#include <stdio.h>

void main()
{
    long num, decimal_num, remainder, base = 1, binary = 0, no_of_1s = 0;

    printf("Enter a decimal integer \n");
    scanf("%ld", &num);
    decimal_num = num;
    while (num > 0)
    {
        remainder = num % 2;
        /*  To count no.of 1s */
        if (remainder == 1)
        {
            no_of_1s++;
        }
        binary = binary + remainder * base;
        num = num / 2;
        base = base * 10;
    }
    printf("Input number is = %d\n", decimal_num);
    printf("Its binary equivalent is = %ld\n", binary);
    printf("No.of 1's in the binary number is = %d\n", no_of_1s);
}


convert the given binary number into decimal in c

/*
 * C program to convert the given binary number into decimal
 */
#include <stdio.h>

void main()
{
    int  num, binary_val, decimal_val = 0, base = 1, rem;

    printf("Enter a binary number(1s and 0s) \n");
    scanf("%d", &num); /* maximum five digits */
    binary_val = num;
    while (num > 0)
    {
        rem = num % 10;
        decimal_val = decimal_val + rem * base;
        num = num / 10 ;
        base = base * 2;
    }
    printf("The Binary number is = %d \n", binary_val);
    printf("Its decimal equivalent is = %d \n", decimal_val);
}


Thursday, 4 January 2018

Reversing a String using C

#include <stdio.h>

int main ()
{
    char name[20];
    printf("enter your name \n");
    scanf("%[^\n]s",name);
     printf("%s\n",strrev(name));
    return 0;
}


C Program to Convert a Number Decimal System to Binary System using Recursion

/*
 * C Program to Convert a Number Decimal System to Binary System using Recursion
 */
#include <stdio.h>
int convert(int);
int main()
{  int dec, bin;
    printf("Enter a decimal number: ");
    scanf("%d", &dec);
    bin = convert(dec);
    printf("The binary equivalent of %d is %d.\n", dec, bin);
    return 0;
}

int convert(int dec)
{
    if (dec == 0)
    {
        return 0;
    }
    else
    {
        return (dec % 2 + 10 * convert(dec / 2));
    }
}

output:-
Enter a decimal number: 10
The binary equivalent of 10 is 1010

Decimal to Binary conversion using C

/*
 * C Program to Print Binary Equivalent of an Integer using Recursion
 */
#include <stdio.h>

int binary_conversion(int);

int main()
{
   int num, bin;

   printf("Enter a decimal number: ");
   scanf("%d", &num);
   bin = binary_conversion(num);
   printf("The binary equivalent of %d is %d\n", num, bin);
}

int binary_conversion(int num)
{
    if (num == 0)
    {
        return 0;
    }
    else
    {
        return (num % 2) + 10 * binary_conversion(num / 2);
    }
}


Wednesday, 3 January 2018

Find the first capital letter in a string using C

/*
 * C Program to find the first capital letter in a string using
 * Recursion
 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>

char caps_check(char *);

int main()
{
    char string[20], letter;

    printf("Enter a string to find it's first capital letter: ");
    scanf("%s", string);
    letter = caps_check(string);
    if (letter == 0)
    {
        printf("No capital letter is present in %s.\n", string);
    }
    else
    {
        printf("The first capital letter in %s is %c.\n", string, letter);    }
        return 0;
    }
    char caps_check(char *string)
    {
        static int i = 0;
        if (i < strlen(string))
        {
            if (isupper(string[i]))
            {
                return string[i];
            }
            else
            {
                i = i + 1;
                return caps_check(string);
            }
        }
        else return 0;
    }