BCA Raipur - BCA Notes S-1

Post your notes and other just by mailing us at bcaraipur@live.com

BCA Raipur - BCA Notes S-2

Post your notes and other just by mailing us at bcaraipur@live.com

BCA Raipur - BCA Notes S-3

Post your notes and other just by mailing us at bcaraipur@live.com

BCA Raipur - BCA Notes S-4

Post your notes and other just by mailing us at bcaraipur@live.com

BCA Raipur - BCA Notes S-5

Post your notes and other just by mailing us at bcaraipur@live.com

google search

Popular Posts


Welcome to BCA Notes

>>>>>>>>>>>>>>>>

Visitors

Search This Blog

Blogger templates

Visitor Map


Sunday, 19 January 2014

Check Whether a Number is Positive or Negative or Zero.

Check Whether a Number is Positive or Negative or Zero.


Source Code:-



#include <stdio.h>
int main()
{
    float num;
    printf("Enter a number: ");
    scanf("%f",&num);
    if (num<=0)
    {
        if (num==0)
          printf("You entered zero.");
        else
          printf("%.2f is negative.",num);
    }
    else
      printf("%.2f is positive.",num);
    return 0;
}

This program also can be solved using nested if else statement.


/* C programming code to check whether a number is negative or positive or zero using nested if...else statement. */

#include <stdio.h>
int main()
{
    float num;
    printf("Enter a number: ");
    scanf("%f",&num);
    if (num<0)      /* Checking whether num is less than 0*/
      printf("%.2f is negative.",num);
    else if (num>0)   /* Checking whether num is greater than zero*/
      printf("%.2f is positive.",num);
    else
      printf("You entered zero.");
    return 0;
}
Output 1


Enter a number: 12.3
12.30 is positive.
Output 2


Enter a number: -12.3
-12.30 is negative.
Output 3


Enter a number: 0
You entered zero.

Source Code to Check Leap Year

Source Code to Check Leap Year



/* C program to check whether a year is leap year or not using if else statement.*/

#include <stdio.h>
int main(){
      int year;
      printf("Enter a year: ");
      scanf("%d",&year);
      if(year%4 == 0)
      {
          if( year%100 == 0) /* Checking for a century year */
          {
              if ( year%400 == 0)
                 printf("%d is a leap year.", year);
              else
                 printf("%d is not a leap year.", year);
          }
          else
             printf("%d is a leap year.", year );
      }
      else
         printf("%d is not a leap year.", year);
      return 0;
}
Output 1


Enter year: 2000
2000 is a leap year.
Output 2


Enter year: 1900
1900 is not a leap year.
Output 3


Enter year: 2012
2012 is a leap year.

Source Code to Find Roots of Quadratic Equation

Source Code to Find Roots of Quadratic Equation


/* Program to find roots of a quadratic equation when coefficients are entered by user. */
/* Library function sqrt() computes the square root. */

#include <stdio.h>
#include <math.h> /* This is needed to use sqrt() function.*/
int main()
{
  float a, b, c, determinant, r1,r2, real, imag;
  printf("Enter coefficients a, b and c: ");
  scanf("%f%f%f",&a,&b,&c);
  determinant=b*b-4*a*c;
  if (determinant>0)
  {
      r1= (-b+sqrt(determinant))/(2*a);
      r2= (-b-sqrt(determinant))/(2*a);
      printf("Roots are: %.2f and %.2f",r1 , r2);
  }
  else if (determinant==0)
  {
    r1 = r2 = -b/(2*a);
    printf("Roots are: %.2f and %.2f", r1, r2);
  }
  else
  {
    real= -b/(2*a);
    imag = sqrt(-determinant)/(2*a);
    printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
  }
  return 0;
}
Output 1


Enter coefficients a, b and c: 2.3
4
5.6
Roots are: -0.87+1.30i and -0.87-1.30i
Output 2


Enter coefficients a, b and c: 4
1
0
Roots are: 0.00 and -0.25

C Program to Find the Largest Number Among Three Numbers

C Program to Find the Largest Number Among Three Numbers

In this program, user is asked to enter three numbers and this program will find the largest number among three numbers entered by user. This program can be solved in more than one way.

Source Code 1



/* C program to find largest number using if statement only */

#include <stdio.h>
int main(){
      float a, b, c;
      printf("Enter three numbers: ");
      scanf("%f %f %f", &a, &b, &c);
      if(a>=b && a>=c)
         printf("Largest number = %.2f", a);
      if(b>=a && b>=c)
         printf("Largest number = %.2f", b);
      if(c>=a && c>=b)
         printf("Largest number = %.2f", c);
      return 0;
}

Source Code 2



/* C program to find largest number using if...else statement */

#include <stdio.h>
int main(){
      float a, b, c;
      printf("Enter three numbers: ");
      scanf("%f %f %f", &a, &b, &c);
      if (a>=b)
      {
          if(a>=c)
            printf("Largest number = %.2f",a);
          else
            printf("Largest number = %.2f",c);
      }
      else
      {
          if(b>=c)
            printf("Largest number = %.2f",b);
          else
            printf("Largest number = %.2f",c);
      }
      return 0;
}

Source Code 3



/* C Program to find largest number using nested if...else statement */

#include <stdio.h>
int main(){
      float a, b, c;
      printf("Enter three numbers: ");
      scanf("%f %f %f", &a, &b, &c);
      if(a>=b && a>=c)
         printf("Largest number = %.2f", a);
      else if(b>=a && b>=c)
         printf("Largest number = %.2f", b);
      else
         printf("Largest number = %.2f", c);
      return 0;
}
Though the technique to solve this problem is different in these three examples, output of all these program is same.

Output:-


Enter three numbers: 12.2
13.452
10.193
Largest number = 13.45

Check Whether a Character is Vowel or consonant

Check Whether a Character is Vowel or consonant


Source Code:-



#include <stdio.h>
int main(){
  char c;
  printf("Enter an alphabet: ");
  scanf("%c",&c);
  if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U')
       printf("%c is a vowel.",c);
  else
       printf("%c is a consonant.",c);
  return 0;
}
Output 1

Enter an alphabet: i
i is a vowel.
Output 2

Enter an alphabet: G
G is a consonant.

Source Code to Check Whether a Number is Even or Odd

Source Code to Check Whether a Number is Even or Odd



/*C program to check whether a number entered by user is even or odd. */

#include <stdio.h>
int main(){
      int num;
      printf("Enter an integer you want to check: ");
      scanf("%d",&num);
      if((num%2)==0)      /* Checking whether remainder is 0 or not. */
           printf("%d is even.",num);
      else
           printf("%d is odd.",num);
      return 0;
}
Output 1


Enter an integer you want to check: 25
25 is odd.
Output 2


Enter an integer you want to check: 12
12 is even.

Source Code to Swap Two Numbers


Source Code to Swap Two Numbers




#include <stdio.h>
int main(){
      float a, b, temp;
      printf("Enter value of a: ");
      scanf("%f",&a);
      printf("Enter value of b: ");
      scanf("%f",&b);
      temp = a;    /* Value of a is stored in variable temp */
      a = b;       /* Value of b is stored in variable a */
      b = temp;    /* Value of temp(which contains initial value of a) is stored in variable b*/
      printf("\nAfter swapping, value of a = %.2f\n", a);
      printf("After swapping, value of b = %.2f", b);
      return 0;
}

Output


Enter value of a: 1.20
Enter value of b: 2.45

After swapping, value of a = 2.45
After swapping, value of b = 1.2

C Program to Find Size of int, float, double and char of Your System

C Program to Find Size of int, float, double and char of Your System

The size of a character is always 1 byte but, size of int, float and double variables differs from system to system. This program will compute the size of int, float, double and char of you system using sizeof operator. The syntax of size of operator is:

 
temp = sizeof(operand);
/* Here, temp is a variable of type integer,i.e, sizeof() operator 
   returns integer value. */

Source Code



/* This program computes the size of variable using sizeof operator.*/

#include <stdio.h>
int main(){
    int a;
    float b;
    double c;
    char d;
    printf("Size of int: %d bytes\n",sizeof(a));
    printf("Size of float: %d bytes\n",sizeof(b));
    printf("Size of double: %d bytes\n",sizeof(c));
    printf("Size of char: %d byte\n",sizeof(d));
    return 0;
}
Output

Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Note: You may get different output depending upon your system.
Explanation
In this program, 4 variables abc and d are declared of type int, float, double and char respectively. Then, the size of these variables is computed using sizeof operator and displayed.

C Program to Demonstrate the Working of Keyword long

C Program to Demonstrate the Working of Keyword long

Keyword long is used for altering the size of data type. For example: the size of int is either 2 bytes or 4 bytes but, when long keyword is used, the size of long int will be either 4 bytes or 8 bytes. Also, you can use long long int. The size of long long int is generally 8 bytes. This program will demonstrate the size of keyword long for my system. It may be different in your system.

Source Code



#include <stdio.h>
int main(){
    int a;
    long int b;                /* int is optional. */
    long long int c;            /* int is optional. */
    printf("Size of int = %d bytes\n",sizeof(a));
    printf("Size of long int = %ld bytes\n",sizeof(b));
    printf("Size of long long int = %ld bytes",sizeof(c));
    return 0;
}
Output

Size of int = 4 bytes
Size of long int = 4 bytes
Size of long long int = 8 bytes
In this program, the sizeof operator is used for finding the size of intlong int and long long int.
Thus, int and long int for my system can hold values from -231 to 231-1. If I have to work on data outside this range, I have to use long long int, which can hold values from -263 to 263-1 .
Similarly, the long keyword can be used double and floats types.

C Program to Find Quotient and Remainder of Two Integers Entered by User

C Program to Find Quotient and Remainder of Two Integers Entered by User

In this program, user is asked to enter two integers(dividend and divisor) and this program will compute the quotient and remainder and display it.

Source Code




/* C Program to compute remainder and quotient  */

#include <stdio.h>
int main(){
    int dividend, divisor, quotient, remainder;
    printf("Enter dividend: ");
    scanf("%d",&dividend);
    printf("Enter divisor: ");
    scanf("%d",&divisor);
    quotient=dividend/divisor;           /*  Computes quotient */
    remainder=dividend%divisor;          /* Computes remainder */
    printf("Quotient = %d\n",quotient);
    printf("Remainder = %d",remainder);
    return 0;
}
Output

Enter dividend: 25
Enter divisor: 4
Quotient = 6
Remainder = 1
Explanation

This program takes two integers(dividend and divisor) from user and stores it in variable dividend and divisor. Then, quotient and remainder is calculated and stored in variable quotient and remainder. Operator / is used for calculation of quotient and % is used for calculating remainder. Learn more about divison(/) and modulo division(%) operator in C programming

C Program to Find ASCII Value of a Character

C Program to Find ASCII Value of a Character

Every character in C programming is given an integer value to represent it. That integer value is known as ASCII value of that character. For example: ASCII value of 'a' is 97. Here is the complete list of ASCII value of characters in C programming. When a character is stored in variable of type char, the ASCII value of character is stored instead of that character itself character itself. For example: If you try to store character 'a' in a char type variable, ASCII value of that character is stored which is 97.
In, this program user is asked to enter a character and this program will display the ASCII value of that character.

Source Code


/* Source code to find ASCII value of a character entered by user */

#include <stdio.h>
int main(){
    char c;
    printf("Enter a character: ");
    scanf("%c",&c);        /* Takes a character from user */
    printf("ASCII value of %c = %d",c,c);
    return 0;
}
Output

Enter a character: G
ASCII value of G = 71
Explanation

In this program, user is asked to enter a character and this character will be stored in variable c, i.e., the ASCII value of that character is stored in variable c. When, this value is displayed using conversion format string %c, the actual variable is displayed but, when this variable is displayed using format string %d, the ASCII value of that character is displayed.

Multiply two Floating Point Numbers

C Program to Multiply two Floating Point Numbers

Source Code:-



/*C program to multiply and display the product of two floating point numbers entered by user. */

#include <stdio.h>
int main( )
{
    float num1, num2, product;
    printf("Enter two numbers: ");
    scanf("%f %f",&num1,&num2);        /* Stores the two floating point numbers entered by user in variable num1 and num2 respectively */
    product=num1*num2;  /* Performs multiplication and stores it */
    printf("Product: %f",product);
    return 0;
}
Output

Enter two numbers: 2.4
1.1
Sum: 2.640000