Tuesday, July 31, 2018

C program for find square value

This is an example of how to find square value of a number using function. 



#include<stdio.h>
float square ( float x );

int main( )
{
    float m, n ;
    printf ( "\nEnter some number for finding square \n");
    scanf ( "%f", &m ) ;

    n = square ( m ) ;
    printf ( "\nSquare of the given number %f is %f",m,n );
}

float square ( float x )
{
    float p ;
    p = x * x ;
    return ( p ) ;
}

Output:

Monday, July 30, 2018

C program for leap year

Here,I used below C program to find whether a year is leap year or not. It's working fine.

Example#1:

#include <stdio.h>

int main()

{

  int year;

  printf("\nPlease enter a year to check whether it is a leap year or not:");

  scanf("%d", &year);



  if ( year%400 == 0)

    printf("\n%d is a leap year", year);

  else if ( year%100 == 0)

    printf("\n%d is not a leap year", year);

  else if ( year%4 == 0 )

    printf("\n%d is a leap year", year);

  else

    printf("\n%d is not a leap year", year);

  return 0;


}

Output:


Example#2:

#include<stdio.h>
int main()
{
int year;
printf("Enter an year : ");
scanf("%d",&year);
if(year%4==0)
{
printf("Leap year");
}
else
{
printf("Not a leap year");
}
}

Output:

C program for fibonacci series

Fibonacci series?
First two numbers of the fibonacci series is 0 and 1. From third number on words , the series eill be the sum of previous two numbers.



#include <stdio.h>
int main()
{
   int f1=0, f2=1, fib_ser, cnt=2, lmt;

   printf("Please enter the limit of the Fibonacci series :");
   scanf("%d",&lmt);
   printf("\nFibonacci series is: \n%d \n%d \n",f1,f2);

   while (cnt < lmt)
   {
      fib_ser=f1+f2;
      cnt++;
      printf("%d\n",fib_ser);
      f1=f2;
      f2=fib_ser;
   }
   return 0;
}

Output:

Sunday, July 29, 2018

C program How to make a simple calculator;

This is a very simple calculator example with switch statement.....

The * operator entered by the user is stored in the operator variable. And, the two operands, 50 and

70 are stored in variables firstNumber and secondNumber respectively.

Since, the operator * matches the case case '*':, the control of the program jumps to

printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber * secondNumber);

This statement calculates the product and displays it on the screen.

Finally, the break; statement ends the switch statement.



#include <stdio.h>
int main() {

    char operator;
    double firstNumber,secondNumber;

    printf("Enter an operator (+, -, *,): ");
    scanf("%c", &operator);

    printf("Enter two operands: ");
    scanf("%lf %lf",&firstNumber, &secondNumber);

    switch(operator)
    {
        case '+':
            printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber + secondNumber);
            break;

        case '-':
            printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber - secondNumber);
            break;

        case '*':
            printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber * secondNumber);
            break;

        case '/':
            printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber / secondNumber);
            break;

        // operator doesn't match any case constant (+, -, *, /)
        default:
            printf("Error! operator is not correct");
    }

    return 0;
}

Output:


C program to find the largest number among the three numbers


In this example, the largest number among three numbers (entered by the user) is found using three different methods.
Largest number among three numbers using if, if...else and nested if...else statement. 

//Example1

This program uses only if statement to find the largest number.

#include <stdio.h>
int main()
{
    double n1, n2, n3;

    printf("Enter three different numbers: ");
    scanf("%lf %lf %lf", &n1, &n2, &n3);

    if( n1>=n2 && n1>=n3 )
        printf("%.2f is the largest number.", n1);

    if( n2>=n1 && n2>=n3 )
        printf("%.2f is the largest number.", n2);

    if( n3>=n1 && n3>=n2 )
        printf("%.2f is the largest number.", n3);

    return 0;
}


Output:





//Example2
 
This program uses if...else statement to find the largest number.


#include <stdio.h>
int main()
{
    double n1, n2, n3;

    printf("Enter three numbers: ");
    scanf("%lf %lf %lf", &n1, &n2, &n3);

    if (n1>=n2)
    {
        if(n1>=n3)
            printf("%.2lf is the largest number.", n1);
        else
            printf("%.2lf is the largest number.", n3);
    }
    else
    {
        if(n2>=n3)
            printf("%.2lf is the largest number.", n2);
        else
            printf("%.2lf is the largest number.",n3);
    }

    return 0;
}



Output:



//Example3

Though, the largest number among three numbers is found using multiple ways, the output of all these program will be same.

#include <stdio.h>

int main()
{
    double n1, n2, n3;

    printf("Enter three numbers: ");
    scanf("%lf %lf %lf", &n1, &n2, &n3);

    if( n1>=n2 && n1>=n3)
        printf("%.2lf is the largest number.", n1);

    else if (n2>=n1 && n2>=n3)
        printf("%.2lf is the largest number.", n2);

    else
        printf("%.2lf is the largest number.", n3);

    return 0;
}


Output:

C program for find square value

This is an example of how to find square value of a number using function.  #include<stdio.h> float square ( float x ); int ma...