Thursday, 27 February 2014

Others programs

//C program for ATM transactions
#include<stdio.h>
int totalThousand =1000;
int totalFiveFundred =1000;
int totalOneHundred =1000;
int main(){
    unsigned long withdrawAmount;
    unsigned long totalMoney;
    int thousand=0,fiveHundred=0,oneHundred=0;
    printf("Enter the amount in multiple of 100: ");
    scanf("%lu",&withdrawAmount);
    if(withdrawAmount %100 != 0){
         printf("Invalid amount;");
         return 0;
    }
    totalMoney = totalThousand * 1000 + totalFiveFundred* 500 +  totalOneHundred*100;
    if(withdrawAmount > totalMoney){
         printf("Sorry,Insufficient money");
         return 0;
    }
    thousand = withdrawAmount / 1000;
    if(thousand > totalThousand)
         thousand = totalThousand;
    withdrawAmount = withdrawAmount - thousand * 1000;
    if (withdrawAmount > 0){
         fiveHundred = withdrawAmount / 500;
         if(fiveHundred > totalFiveFundred)
             fiveHundred = totalFiveFundred;
         withdrawAmount = withdrawAmount - fiveHundred * 500;
    }
    if (withdrawAmount > 0)
         oneHundred = withdrawAmount / 100;
    printf("Total 1000 note: %d\n",thousand);
    printf("Total  500 note: %d\n",fiveHundred);
    printf("Total  100 note: %d\n",oneHundred);
    return 0;
}
Sample output:
Enter the amount in multiple of 100: 7800
Total 1000 note: 7
Total  500 note: 1
Total  100 note: 3
//How to pass one dimensional array to function in c
#include <stdio.h>
#define N 5
void fstore1D(int a[], int a_size);
void fretrieve1D(int a[], int a_size);
void fedit1D(int a[], int a_size);
int main(){
int a[N];
printf("Input data into the matrix:\n");
fstore1D(a, N);
fretrieve1D(a, N);
fedit1D(a, N);
fretrieve1D(a, N);
return 0;
}
void fstore1D(int a[], int n){
int i;
for ( i = 0; i < n; ++i )
scanf("%d", &a[i]);
}
void fretrieve1D(int a[], int n){
int i;
for ( i = 0; i < n; ++i )
printf("%6d ", a[i]);
printf("\n");
}
void fedit1D(int a[], int n){
int i, q;
for ( i = 0; i < n; ++i ){
printf("Prev. data: %d\nEnter 1 to edit 0 to skip.", a[i]);
scanf("%d", &q);
if ( q == 1 ){
printf("Enter new value: ");
scanf("%d", &a[i]);
}
}
}
Write a c program which passes two dimension array to function
#include <stdio.h>
#define M 3
#define N 5
void fstore2D(int a[][N]);
void fretrieve2D(int a[][N]);
int main(){
  int a[M][N];
  printf("Input data in matrix (%d X %d)\n", M, N);
  fstore2D(a);
  fretrieve2D(a);
  return 0;
}
void fstore2D(int a[][N]){
    int i, j;
    for (i = 0; i < M; ++i){
    for (j = 0; j < N; ++j)
         scanf("%d", &a[i][j]);
    }
}
void fretrieve2D(int a[][N]){
   int i, j;
   for ( i = 0; i < M; ++i ){
       for ( j = 0; j < N; ++j)
            printf("%6d ", a[i][j]);
       printf("\n");
   }
}
//Write a c program which takes password from user
#include<stdio.h>
#define MAX 500
int main(){
    char password[MAX];
    char p;
    int i=0;
    printf("Enter the password:");

    while((p=getch())!= 13){
         password[i++] = p;
         printf("*");
    }
    password[i] = '\0';
    if(i<6)
         printf("\nWeak password");
    printf("\nYou have entered: %s",password);
    return 0;
}
Sample output:
Enter the password:*******
You have entered: fgt67m,
//Write a scanf function in c which accept sentence from user
#include<stdio.h>
#define MAX 500
int main(){
    char arr[MAX];
    printf("Enter any sentence which can include spaces.\n");
    printf("To exit press enter key.\n");
    scanf("%[^\n]s",arr);
    printf("You had entered: \n");
    printf("%s",arr);
    return 0;
}
Sample output:
Enter any sentence which can include spaces.
To exit press enter key.
May I help you?
You had entered:
May I help you?
//Write a scanf function in c which accept paragraph from user
#include<stdio.h>
#define MAX 500
int main(){
    char arr[MAX];
    printf("Enter any paragraph which can include spaces or new line.\n");
    printf("To exit press the tab key.\n");
    scanf("%[^\t]s",arr);
    printf("You had entered: \n");
    printf("%s",arr);
    return 0;
}
//Print prime numbers between 1-300 using break and continue in c
Definition of prime number:
A natural number greater than one has not any other divisors except 1 and itself. In other word we can say which has only two divisors 1 and number itself. For example: 5
Their divisors are 1 and 5.
Note: 2 is only even prime number.
Example of prime numbers : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199 etc.
#include <math.h>
#include <stdio.h>
main(){
  int i, j;
  i = 2;
  while ( i < 300 ){
     j = 2;
     while ( j < sqrt(i) ){
         if ( i % j == 0 )
            break;
         else{
            ++j;
            continue;
         }
      }
      if ( j > sqrt(i) )
            printf("%d\t", i);
      ++i;
  }
  return 0;
}
//Palindrome in c without using string function
#include<stdio.h>
int main(){
  char str[100];
  int i=0,j=-1,flag=0;
  printf("Enter a string: ");
  scanf("%s",str);
  while(str[++j]!='\0');
  j--;
  while(i<j)
      if(str[i++] != str[j--]){
           flag=1;
           break;
      }
    
  if(flag == 0)
      printf("The string is a palindrome");
  else
      printf("The string is not a palindrome");
  return 0;
}
//How to get the ASCII value of a character in c
#include<stdio.h>
int main(){

    char c;
    printf("Enter any character: ");
    scanf("%c",&c);
    printf("ASCII value of given character: %d",c);
    
    return 0;
}
Sample output:
Enter any character: a
ASCII value of given character: 97
}

No comments:

Post a Comment