Showing posts with label Chapter 3: Structured Program Development. Show all posts
Showing posts with label Chapter 3: Structured Program Development. Show all posts

Wednesday, March 14, 2012

Enforcing Privacy with Cryptography

Simple crypter/decrypter
(I means really simple)

Reads four digit integer.Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the first digit with the third, and swap the second digit with the fourth.The decrypter works reversely.
/*
   title: EnforcingPrivacyWithCryptography
   author: aeriqusyairi
   date: dec30 2011
*/
#include<stdio.h>
#include<stdlib.h>

int main(){
    int integer=0,explode=0,a=0,b=0,c=0,d=0,i=1,swap=0,option=0;
    
    printf("Input the 4 digit integer: ");
    scanf("%d",&integer);
    
    printf("Select an option...\n1.Encrypt\n2.Decrypt\nOption: ");
    scanf("%d", &option);
    
    while(i <= 4){
       explode = integer % 10;
       integer /= 10;
       
       if(i == 1)
          d = explode;     
       else if(i == 2) 
          c = explode; 
       else if(i == 3) 
          b = explode; 
       else if(i == 4) 
          a = explode;  
          
       i++;        
    }
    
    if(option == 1){
       a = (a + 7) % 10;
       b = (b + 7) % 10;
       c = (c + 7) % 10;
       d = (d + 7) % 10;
    }else if(option == 2){
       a = (a + 3) % 10;
       b = (b + 3) % 10;
       c = (c + 3) % 10;
       d = (d + 3) % 10;   
    }

    printf("%d%d%d%d\n",c,d,a,b);

    system("pause");
    return 0;   
}

Target-Heart-Rate Calculator

The formula for calculating your maximum heart rate in beats per minute is 220 minus your age in years.

Your target heart rate is a range that is 50-85% of your maximum heart rate.

However, maximum and target heart rates may vary based on the health, fitness, and gender of the individual.

Program below reads the user's birth year and the current year and computes the person's age, maximum heart rate and the target heart rate range.
/*
   title: Target-Heart-RateCalculator
   author: aeriqusyairi
   date: dec30 2011
*/
#include<stdio.h>
#include<stdlib.h>

int main(){
   int birth=0,current=0,age=0,maxrate=0;
   
   printf("Input your birth year: ");
   scanf("%d", &birth);
   
   printf("Input current year: ");
   scanf("%d", ¤t);
   
   age = current - birth;
   maxrate = 220 - age;
   
   printf("You are %d years old.\n", age);
   printf("Your maximum heart rate is %d beats per minute.\n", maxrate); 
   printf("Your target heart rate range is between %.2f to %.2f beats per minute.\n", 0.5 * maxrate, 0.85 * maxrate);
   
   system("pause");
   return 0;    
}

World Population Growth Calculator

Reads current world population and the annual world population growth rate and computes the estimated world population after one, two,and three years.
/*
   title: WorldPopulationGrowthCalculator
   author: aeriqusyairi
   date: dec30 2011
*/
#include<stdio.h>
#include<stdlib.h>

int main(){
   int i=1;
   double a=0,b=0;
   printf("Input the current world population: ");    
   scanf("%lf", &a);
   printf("Input the annual world population growth rate: ");
   scanf("%lf", &b);
   
   while(i <= 3){
      a *= b;
      printf("The estimate world population after %d year is %lf.\n",i,a);   
      i++;        
   }
   
   system("pause");
   return 0;
}

Factorial

Reads a nonnegative integer and computes and print its factorial.
e.g n! = n.(n - 1).(n - 2).....1
/*
   title: Factorial
   coder: aeriqusyairi]
   date: dec30 2011
*/
#include<stdio.h>
#include<stdlib.h>

int main(){
   int a=0,b=0,factorial=1;
   printf("Input a nonnegative integer: "); 
   scanf("%d", &a);
   while(a != 0){
      factorial *= a;
      a--;
   }   
   printf("The factorial is %d.\n", factorial);
   system("pause");
   return 0;
}

Side of a Triangle

Reads three nonzero float values and determines and prints if they could represent the side of a triangle.
/*
   title: SidesOfATriangle
   coder: aeriqusyairi
   date: dec30 2011
   
   Note: Use triangle Inequality Theorem - 
      The longest side of any triangle must
      be less than the sum of the two
      shorter sides
*/
#include<stdio.h>
#include<stdlib.h>

int main(){
   float a=0,b=0,c=0,swap=0;
   printf("Input 3 nonzero values:\nFirst\n-");
   scanf("%f", &a);
   printf("Second\n-");
   scanf("%f", &b);
   printf("Third\n-");
   scanf("%f", &c);
   
   if(a > b && a > c){
      swap = c;
      c = a;
      a = swap;        
   }else if(b > a && b > c){
      swap = c;
      c = b;
      b = swap;       
   }
   
   printf("%s\n", c < a + b ? "Possible side of triangle.\n" : "Impossible side of a triangle.\n" );
   system("pause");
   return 0;
}

Side of a Right Triangle

Reads three nonzero integer. Determines and prints if they could be the side of a right triangle.
/*
   title: SidesOfARighttriangle
   author: aeriqusyairi
   date: dec30 2011
   
   Note: Right triangle -> c^2 = a^2 + b^2
         Obtuse triangle -> c^2 > a^2 + b^2
         Acute triangle -> c^2 < a^2 + b^2
*/
#include<stdio.h>
#include<stdlib.h>

int main(){
   int a=0,b=0,c=0,swap=0;
   printf("Input 3 integer:\nFirst\n-");
   scanf("%d", &a);
   printf("Second\n-");
   scanf("%d", &b);       
   printf("Third\n-");
   scanf("%d", &c);
   
   if(a > b && a > c){
      swap = c;
      c = a;
      a = swap;     
   }else if(b > a && b > c){
      swap = c;
      c = b;
      b = swap;      
   }
  
   printf("%s\n", c * c == (a * a) + (b * b) ? "Possible side of right triangle.\n" : "Impossible side of right triangle");
   system("pause");
   return 0;
}

Diameter, Circumference, and Area of a Circle

Reads the radius of a circle as a float value and computes and prints the diameter, the circumference ,and the area.
π = 3.14159
/*Program to compute circle diameter,circumference, and area from radius*/
#include<stdio.h>
#define PI 3.14159
int main(){
   float radius;
   printf("Enter the radius of your circle: ");
   scanf("%f",&radius);
   printf("The diameter of your circle is %.2f\n",radius * 2);
   printf("The circumference of your circle is %.2f\n",2 * PI * radius);
   printf("The area of your circle is %.2f\n",PI * radius * 2);
   system("pause");
   return 0;         
}

Hollow Square of Asterisks

Similar to Square of Asterisk except it print a hollow square out of asterisk with respect to the side being reads.
/*
   title: hollowSquareOfAsterisk
   author: aeriqusyairi
   date: dec27 2011
*/

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

int main(){
   int side=0,c1=1,c2=1,c3=0;
   printf("Input the side of your square: ");
   scanf("%d", &side);
   for(c1=1;c1 <= side;c1++){
      if(c1 == 1 || c1 == side){
         for(c2=1;c2 <= side;c2++){
            printf("*");              
         }
         printf("\n");
      }else{
         for(c2=1;c2 <= side;c2++){
            if(c2 == 1 || c2 == side)
               printf("*");
            else{
               printf(" ");     
            }              
         }
         printf("\n");       
      }
   }    
   system("pause");
   return 0;
}

Square of asterisk

Reads in the side of a square and then print that square out of asterisk.
/*
   title: square of asterisk
   author: aeriqusyairi
   date: dec27 2011
*/

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

int main(){
   int side=0,c1=1,c2=1;
   printf("Input the side of your square: ");
   scanf("%d", &side);
   for(c1=1;c1 <= side;c1++){
      for(c2=1;c2 <= side;c2++){
         printf("*");              
      }
      printf("\n");
   }    
   system("pause");
   return 0;
}

Printing the Decimal Equivalent of a Binary Number

Just as in the decimal number system, in which the rightmost digit has a positional value of 1, and the next digit left has a positional value of 10, then 100, then 1000, and so on, in the binary number system the rightmost digit has a positional value of 1, the next digit left has a positional value of 2, then 4, then 8, and so on.

e.g 1101 is 1 * 1 + 0 * 2 + 1 *  4 + 1 * 8 or 1 + 0 + 4 + 8 or 13


/*
   title: printingDecimalEquivalentOfABinaryNumber
   author: aeriqusyairi
   date: dec27 2011
*/
#include<stdio.h>
#include<stdlib.h>

int main(){
   int binary=0,decimal=0,counter=0,two=1,digit=0;
   printf("Input a binary number: ");
   scanf("%d", &binary);    
   do{
      counter = binary;
      digit = binary % 10;
      decimal += digit * two; 
      binary /= 10;
      if(two == 1)
         two++;
      else
         two *= 2;
   }while(counter >=1);
   printf("%d\n", decimal);
   system("pause");
   return 0;
}

Palindrome Tester

A palindrome is a number or a text phrase that reads the same backward as forward
e.g 12321

Program below reads in a five-digit integer and determines whether or not it's a palindrome.
/*
   title: palindromeTester
   author: aeriqusyairi
   date: dec27 2011
*/
#include<stdio.h>
#include<stdlib.h>

int main(){
   int number=0,preserve=0,counter=1,f1=0,l1=0,f2=0,l2=0;
   printf("Input 5 digit integer: ");
   scanf("%d", &number); 
   preserve = number;
   while(counter <= 5){   
      switch(counter){
         case 1: l1 = number % 10;
                 number /= 10;
                 break;
         case 2: l2 = number % 10;
                 number /= 10;
                 break;
         case 3: number /= 10;
                 break;
         case 4: f2 = number % 10;
                 number /= 10;
                 break;
         case 5: f1 = number % 10;
                 number /= 10;
                 break;
         default: break;      
      }
      counter++;
   }
   if(f1 == l1 && f2 == l2)
      printf("Integer %d is a palindrome.\n\n", preserve);
   else{
      printf("Integer %d is not a palindrome.\n\n", preserve);  
   }
   system("pause");
   return 0;
}

Output:
;-)
Read more..