Friday, October 18, 2013

C Programming Prime Number Program

Prime Number Program In C Language

This is the program written in c language to take the user input and detect whether it is a prime number or not. there is a little learning curve in this program regarding the if statements so if you stick with the program and read the important outlines you will be able to understand properly.

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

 void main()
{
   int n,c;

   printf("Enter a number to check if it is prime\n");
   scanf("%d",&n);

   for ( c = 2 ; c <= n - 1 ; c++ )
   {
      if ( n%c == 0 )
      {
         printf("%d is not prime.\n", n);
     break;
      }                                       
      }

   if ( c == n )
   {   

printf("%d is prime.\n", n);
}
getch();
}



Note the important instruction which will be able to understand.

it means the loop will start from 2 and it will end the user input - 1 . It means it will end on n-1 .

We have take the user input in the variable "n" . and if the "c" is greater than n-1 the statement becomes false and it came from loop. After the end of first "if" statement the value of  will be c=n so we have given the second statement of "if" . which will print the number . The number will be equal to prime numbers

Monday, September 30, 2013

C Language Tutorial

How To Solve Quadratic Equations Using C language

Quadratic Equation Using C Language 

Now in this tutorial before going on code i have to sort out some things which will help the new bie programmers to do it . 

I have include three following files in the header like

#include<stdio.h>  -------------------> This is the standard input output file
#include<conio.h> -------------------> This is the console file for output on the screen
#include<math.h> --------------------> This is the math header file to solve mathematics function

Now there are escape sequences and format specifier in the c language that which you have to learn about that

int ----->  this is the keyword for integer type data.

Examples like int a,b;



float ----> this is the keyword for float type data

Example like float a,b;

Now if you want to display the code into the program you have to use format specifier to display the code

%d is the format specifier for integer type value

%f is the format specifier for float type value 

I have used different variable name in this program.


Through this code you can solve the quadratic equation


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


void main(void)
{


      float a,b,c,y,x,z,t,u,v,w;
clrscr();
      printf("*************** Quadratic Equation ***************************\n");
      printf(" \n Plz enter the value of a : ");
scanf("%f",&a);
      printf(" \n Please enter the value of b  :");
      scanf("%f",&b);
      printf(" \n Please enter the value of c :");
      scanf("%f",&c);
      x=pow(b,2);
      y=4*a*c;
      z=x-y;
      u=sqrt(z);
      t=-b+u;
      v=t/(2*a);
      printf("The Answer is =  %f",v);
      w=-b-u;
      printf("The Answer is = %f",w);
      getch();


}