c program to swap two numbers using third variable

>> Monday, October 31, 2011

Hello! This program is very important as it is mostly taught and asked in external, vivas and some time in
campus recruitment exams also. We are Swapping the value of variable 'a' and 'b' by using a third variable 'temp'. Lets look on this program that how it is done.



#include<stdio.h> #include<conio.h>
int main() {     int a,b,temp;    printf("enter two values\n");    printf("\na = ");    scanf("%d",&a);    printf("\nb = ");    scanf("%d",&b);    temp=a;    a=b;    b=temp;    printf("\na = %d\tb = %d ", a, b);    getch(); }

Output :



 C program to swap two numbers using third variable
C program to swap two number using third varaible 

Read more...

c program to find whether the given number is prime or not using Recursion ?

>> Friday, October 28, 2011


This C program uses recursion for finding whether the entered number is prime or not. A function is called recursive if the statement in a body calls the same functions. It uses the modulus operator ('%') to find the prime number.Lets have a look on this program. Function name is prime in this program and it calls prime() again till the condition is satisfied and till the parser is not return to the main function.




#include<stdio.h> #include<conio.h> int prime(int n) {     static int i=2;                                              if(n%i==0&&n!=2)            {            return(1);            }            if(i<n)            {            i++;            prime(n);                                    // recursion call            } } int main() {     int p;     printf("enter the number to check for prime number : ");     scanf("%d",&p);     int l=prime(p);                                      //function returns the value and puts in 'l' variable         if(l==1)                                            //conditions         printf("%d is not a prime number ", p);         else         printf("%d is a prime number ", p);     getch(); }

Here is output for the above program :

C program to find whether the entered number is prime or not using recursion
output : Find prime number using recursion


Read more...

c program to find and count the prime number upto given number ?


This program find the Prime number upto the number you desire and will also count the number of prime numbers. This program uses goto Statement which is very infrequently used but sometime desired at some places and so as here. So in this program will we also learn to use goto statement. Below is the program with its output, with prime number upto 100.
Have a look on it :


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

int main()
{
     int n=2, count=0;                  //count variable store the number of prime number
     int i, m;
     printf("enter the number upto which you want to find prime numbers : ");      
     scanf("%d",&m);
     printf("prime numbers are : ");
     while(n<m)
     {
             for(i=2; i<n; i++)
                      {
                      if(n%i==0)
                      goto t;                             // goto is referring to line or code 't'
                      }
             printf("\t%d ", n);
             count++;
             t : n++;                                    //when goto is executed it will directly jump to this statement
     }
     printf("\n\nNumber of prime numbers are %d",count);
     getch();
}
       
   

Here is the output :

c program to find the prime number upto a given number and count total number of prime numbers


Read more...

c program to find prime number efficiently in less time ?


This C program will find the given number is prime with less time, by testing whether the given number is a multiple of any integer between 2 and square root of that number. Lets take an example to understand this. Suppose the number is 21, then rather to check its divisibility from 2 to 20, just test it divisibility only by 2, 3, and 4.As square of 4 is 16 and that of 5 is 25 which is greater than 21. The below program will let you know how this is done.


#include<stdio.h> #include<conio.h> int main() {      int n;      int i,s=1,p=1;      printf("enter the number :");      scanf("%d",&n);      while(p<n)      {      s++;      p=s*s;      }      printf("\nDivisibility is checked till number %d \n\n",s-1);      for(i=2; i<s; i++)      {               if(n%i==0)                {                 printf("%d is not a prime number", n);                getch();                return (1);                }       } printf("%d is a prime number", n); getch(); }            

Here is the output :

C program to find prime number effeciently in less time and fast
 find prime number efficiently in less time


Read more...

c program to find whether the given number is prime or not ?


In this C program, we will input the number when program runs and find whether that entered number is prime or not. This program uses "%" or "modulus" operator which gives the remainder of two numbers when it is divided. For example : 7 % 2 = 1, as 1 is remainder when 7 is divided by 2. So if the number is not prime then its % operation will give output '0' otherwise it is prime. Let us see how this program is implemented.
#include<stdio.h>                     // Including library
#include<conio.h>
int main()                              
{
     int n;
     int i;
     printf(" enter the number : ");   
     scanf("%d",&n);                  
     for(i=2; i<n; i++)
     {
              if(n%i==0)              
               {
                printf("%d is not a prime number.", n);
               getch();                                                  // hold  the  screen to display the above message
               return (1);                                              // return statement  will return the value and terimate
               }
     }
printf("%d is a prime number.", n);
getch();
}
         
Lets see the output of this program
c program to find prime number, output for prime number
output : Program to find prime number 



Here’s the another  program to find whether  the given number is prime or not by using recursion function.


Read more...

About This Blog

This blog will contain c programs related to interview preparation, basic programs, operating system, graphics, data structure, algorithms implementation, compiler and porjects. C is one of the most widely used programming languages of all time. This blog is intended for engineer students and for the people who are preparing for their interview in IT sector.

Share and Save