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:

Saturday, July 28, 2018

Example of reverse number

In this program!
This program takes an integer input from the user. Then the while loop is used until n != 0 is false.
In each iteration of while loop, the remainder when n is divided by 10 is calculated and the value of n is reduced by times.


#include <stdio.h>
int main()
{
    int n, reversedNumber = 0, remainder;

    printf("Enter an integer: ");
    scanf("%d", &n);

    while(n != 0)
    {
        remainder = n%10;
        reversedNumber = reversedNumber*10 + remainder;
        n /= 10;
    }

    printf("Reversed Number = %d", reversedNumber);

    return 0;
}

Output:


Example of check Even or Odd number


In this C program to check whether a number is Even or Odd.
In this example , if....else statement is used to check whether a number entered by the user is Even or Odd.

An even number is an integer that is exactly divisible by 2. Example: 0, 8, -24

An odd number is an integer that is not exactly divisible by 2. Example: 1, 7, -11, 15


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

    printf("Enter an integer: ");
    scanf("%d", &number);

    if(number % 2 == 0)
        printf("%d is even.", number);
    else
        printf("%d is odd.", number);

    return 0;
}

Output:

Example of swap between two floating number

In this program, i used temporary variables.
fnum , snum and twonum  are variables in this program.In below the program twonum is assigned the value of fnum .
Then the value of  fnum is assigned to snum .Finally , the  twonum is assigned to snum which completes the swapping process.




#include <stdio.h>
int main()
{
      double fnum, snum, twonum;

      printf("Enter first number: ");
      scanf("%lf", &fnum);

      printf("Enter second number: ");
      scanf("%lf",&snum);

      twonum = fnum;
      fnum = snum;
      snum = twonum;

      printf("\nAfter swapping, fnum = %.2lf\n", fnum);
      printf("After swapping, snum = %.2lf", snum);

      return 0;
}


Output:

Addition two integer number

In this program user is asked to enter two integers.When users entered two number in variables firstNumber and secondNumber rightly. Then will be done using  scanf() function.
And then this two variables are added using + operator and the result is d in sumOfTwoNumbers.

So, lets see the program and also see the output in below of the program.


#include <stdio.h>
int main()
{
    int firstNumber, secondNumber, sumOfTwoNumbers;

    printf("Enter two integers: ");


    scanf("%d %d", &firstNumber, &secondNumber);


    sumOfTwoNumbers = firstNumber + secondNumber;


    printf("%d + %d = %d", firstNumber, secondNumber, sumOfTwoNumbers);

    return 0;
}

Output:


Friday, July 27, 2018

How to find factors of an integer number.


//Here i entered(120) a positive integer number to find the factors of the entered positive integer number.The output in below.

#include <stdio.h>
int main()
{
    int number, i;

    printf("Enter a positive integer: ");
    scanf("%d",&number);

    printf("Factors of %d are: ", number);
    for(i=1; i <= number; ++i)
    {
        if (number%i == 0)
        {
            printf("%d ",i);
        }
    }

    return 0;
}

Output:



Prime Number In C program

//To check prime number see the code in c program.

#include <stdio.h>
int main()
{
    int n, i, flag = 0;

    printf("Enter a positive integer: ");
    scanf("%d",&n);

    for(i=2; i<=n/2; ++i)
    {
        // condition for non-prime number
        if(n%i==0)
        {
            flag=1;
            break;
        }
    }

    if (flag==0)
        printf("%d is a prime number.",n);
    else
        printf("%d is not a prime number.",n);

    return 0;
}

Output:
Here in the output is showing 19 in a prime number.So, we can see the code is works perfectly.

Hello World

This is my first program in c language.

#include<stdio.h>

int main()
{
    printf("Hello World!");
    return 0;
}

Outpot:

Programming in C Example. Pattern program

This is my first program.Example with output screenshot .

#include <stdio.h>
int main()
{
    int i, j, row;

    printf("Enter any number of row: ");
/* Here i entered the number of 5 and the output in the screen see below */
    scanf("%d",&row);

    for(i=1; i<=row; ++i)
    {
        for(j=1; j<=i; ++j)
        {
            printf("%d ",j);
        }
        printf("\n");
    }
    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...