Tuesday, May 15, 2012

Test Programming with C Problem 8

/* Problem 8: Any integer is input through keyboard. Write a program to find out whether it is an odd number or even number. */

#include<stdio.h>
#include<conio.h>

int check_oe(int x);

int main()
{
    int num, check;
   
    printf("Please input a number:\n");
    scanf("%d", &num);
   
    check = check_oe(num);
   
    if(check == 0) printf("%d is an even number.\n", num);
    else printf("%d is an odd number.\n", num);
   
   
    getch();
    return 0;  
}

int check_oe(int x)
{
    int test;
    test = x % 2;
    return test;
   
}

Test Programming with C Problem 7

/*Problem 7: Rahim's basic salary is input through the keyboard. His House rent allowance is 30% of basic salary and medical allowance is 5% of basic salary. He gets extra 1000 tk as technical allowance. Write a program to calculate his gross salary and print the result. */

#include<stdio.h>
#include<conio.h>

float calc_sal(float sal);

int main()
{
    float b_salary, total;
   
    printf("Please input Rahim's basic salary:\n");
    scanf("%f", &b_salary);
   
    total = calc_sal(b_salary);
   
    printf("Rahim's gross salary : %f\n", total);
   
    getch();
    return 0;
}
float calc_sal(float sal)
{
      float total;
      total = sal + (sal * 0.30) + (sal * 0.05) + 1000;
      return total;     
}

Test Programming with C Problem 6

/* Problem 6: If a 4 digit number is input through the keyboard, write a program to obtain the sum of the first and last digit of this number. */

#include<stdio.h>
#include<conio.h>

int main()
{
    int num, res1st, res2nd;
   
    printf("Please enter a 4 digit number.\n");
    scanf("%d", &num);
   
    res1st = num % 10;
    res2nd = num / 1000;
   
    printf("The sum of first and last digit of that number is %d\n", (res1st+res2nd));
   
    getch();
    return 0;
}

Test Programming with C Problem 5

/* Problem 5: If a 5 digit number is input through the keyboard, write a program to calculate and print the sum of its digits.  */


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

int main()
{
    int num, res1, res2, res2_nxt, res3, res3_nxt, res4, res4_nxt, res5;
   
    printf("Please input a 5 digit number.\n");
    scanf("%d", &num);
   
    res1 = num % 10;
   
    res2_nxt = num % 100;
    res2 = res2_nxt / 10;
   
    res3_nxt = num % 1000;
    res3 = res3_nxt / 100;
   
    res4_nxt = num % 10000;
    res4 = res4_nxt / 1000;
   
    res5 = num / 10000;
   
    printf("Sum of those 5 digits is %d\n", (res1+res2+res3+res4+res5));
   
    getch();
    return 0;
}

Test Programming with C Problem 3

/* Problem 3: Write a program to interchange the contents of two variables. */

#include<stdio.h>
#include<conio.h>


int main()
{
    int var_a;
    int var_b;
    int temp;
   
    printf("Please input values for variable a:\n");
    scanf("%d", &var_a);
   
    printf("Please input values for variable b:\n");
    scanf("%d", &var_b);
   
    printf("So,\na = %d\nb = %d\n", var_a, var_b);
   
     temp = var_a;
     var_a = var_b;
     var_b = temp;
   
    printf("\nAfter interchange:\na = %d\nb = %d\n", var_a, var_b);
   
    getch();
    return 0;
}

Monday, May 14, 2012

/* Problem 2: Write a program which will take 2 numbers as input and will print their addition, subtraction, multiplication and division as output. */

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

float addition(float x,float y);
float substruction(float x,float y);
float multiplication(float x,float y);
float divition(float x,float y);

int main()
{
    float num1, num2, add, sub, mul, div;
   
    printf("Please enter the first number:\n");
    scanf("%f", &num1);
   
    printf("Please enter the second number:\n");
    scanf("%f", &num2);
   
    add = addition(num1,num2);
    sub = substruction(num1,num2);
    mul = multiplication(num1,num2);
    div = divition(num1,num2);
   
    printf("Addition of those two numbers are %f\nSubstruction of those two numbers are %f\nMultiplication of those two numbers are %f\ndivition of those two numbers are %f\n", add, sub, mul, div);
    getch();
    return 0;
   
}

float addition(float x,float y)
{
      float res;
      res = x + y;
      return res;
}
float substruction(float x,float y)
{
      float res;
      res = x - y;
      return res;
}
float multiplication(float x,float y)
{
      float res;
      res = x * y;
      return res;
}
float divition(float x,float y)
{
      float res;
      if(y != 0) res = x / y;
      return res;
}

Test Programming with C [Problem 1]

/* Problem 1: Write a C Program to find area of a triangle (Hint : A = ½ *h*b). The height and base will be given as input. */


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

float tri_area(float x,float y);

int main()
{
    float h, b, ansarea;
   
    loop1:
    printf("Please enter the height of the triangle:\n");
    scanf("%f", &h);
   
    if(h < 0) {
         printf("Height can not be negative.\n");
         goto loop1;    
         }
   
    loop2:
    printf("\nPlease enter the base of the triangle:\n");
    scanf("%f", &b);
   
    if(b < 0) {
         printf("Base can not be negative.\n");
         goto loop2;    
         }
        
    ansarea = tri_area(h,b);
   
    printf("The area of the triangle is: %f", ansarea);
   
    getch();
    return 0;
}

float tri_area(float x,float y)
{
      float result;
     
      result = (0.5*x*y);
     
      return result;     
}