Showing posts with label List of C Programs. Show all posts
Showing posts with label List of C Programs. Show all posts
Prime Number Program In C

Prime Number Program In C

Program to find whether a given number is Prime or Not


#include<stdio.h>

int main()
{
 /* Declaring variable for n=number, c=counter variable:holds 
 number of factor of 'n' */
 int n,c=0, i;
 
 // Inputting number
 printf("Enter number to check whether Prime or Not:");
 scanf("%d",&n);

 // Checking whether number valid or not
 if(n>0)
 {
  
 // Checking whether number is prime or not 
 for(i=1;i<=n;i++)
 {
  if(n%i==0)
    c=c+1;
 }
 
 if(c==2)
   printf("Prime Number");
 else
   printf("Not a Prime Number");
 }
 
 else 
   printf("Not a valid Number");

 return 0;
}

Program to print ( list ) all the prime number between 1 to n

#include<stdio.h>

int main()
{
 /* Declaring variable for n=number, c=counter variable:holds 
 number of factor of 'n' */
 int n,c, i,j;
 
 // Inputting number
 printf("Enter number till which you want to list prime number:");
 scanf("%d",&n);

 // Checking whether number valid or not
 
 if(n>0)
 {
  printf("List of Prime Numbers:");
 // Checking whether number is prime or not 
 for(i=1;i<=n;i++)
 {
  c=0;
  for(j=1;j<=i;j++)
  {
    if(i%j==0)
      c=c+1;
  }
 if(c==2)
   printf("%d ",i);
 }
 
 }
 else 
   printf("Not a valid Number");

 return 0;
}
Palindrome Program In C

Palindrome Program In C


C PROGRAM TO DETERMINE PALINDROME NUMBER

Back To Palindrome Program

#include<stdio.h>
int main()
{
  // variable for a=number, n=to copy number, d=digit, rev=reverse of number
  int a,n,d=0,rev=0;

  // Inputting number
  printf("Enter any number to determine whether it is Palindrome or not:");
  scanf("%d",&a);

  // copying number in n
  n=a;

  // Determining whether number is palindrome or not
  while(n!=0)
  {
    d=n%10;
    rev=rev*10+d;
    n=n/10;
  }
           
  // Displaying Whether number is palindrome or not
  if(a==rev)
    printf("Palindrome number");
  else
    printf("Not a Palindrome Number");

  return 0;
}


C PROGRAM TO PRINT(LIST) PALINDROME NUMBER FROM 10 TO n

Back To Palindrome Program

#include<stdio.h>
int main()
{
  /*declaring variable for num=user wish, i=acts as number, n=to copy number, 
  d=digit, rev=reverse of number */
  int num,n,d=0,rev,i;

  // Inputting number
  printf("Enter number till which you want to list Palindrome Number:");
  scanf("%d",&num);

  printf("List of Palindrome Number:");
  for(i=10;i<=num;i++)
  {
    // copying number in n
    n=i;
    rev=0;
    // Determining whether number is palindrome or not
    while(n!=0)
    {
       d=n%10;
       rev=rev*10+d;
       n=n/10
    }
           
    // Displaying Whether number is palindrome or not
    if(i==rev)
    printf("%d ",i);
  }

  return 0;
}

C PROGRAM TO FIND WHETHER A NUMBER IS PALINDROME OR NOT USING RECURSION

Back To Palindrome Program

#include<stdio.h>

// Declaring global variable r=reverse, d=digit
int r=0, d=0;

// Defining function with parameter n = number
int rev(int n)
{
/* Base condition : Any condition where a recursive function 
or method does not invoke itself. */
if(n==0)
return r;

// Continue calling function rev or function invoke itself
else
{
 // Extracting digit
 d=n%10;
 
 // Finding reverse
 r=(r*10+d);
 
 // function invoke itself
 rev(n/10);
}
}

int main()
{
// Declaring variable n = number
int n;

// Declaring variable "r" to hold the reverse number
int r;

// Inputting Number
printf("Enter the Number : ");
scanf("%d",&n);

// Calling function "rev" with actual parameter "n" passed to it
r=rev(n);

// Checking and Displaying if a Number is palindrome or Not
if(r==n)
printf("%d is a Palindrome Number ",n);

else
 printf("%d is not a Palindrome Number ",n);

return 0;
}


C PROGRAM TO ENTER A WORD AND CHECK WHETHER IT IS PALINDROME OR NOT USING STRING FUNCTION

Back To Palindrome Program


// palindrom - When word = reverse of word then it is called palindrome
// Example : madam - palindrome, Madam - not palindrome, MadaM - palindrome

#include<stdio.h>
#include<string.h>

int main()
{
 // Declaring variable str=string 
 char word[50], word1[50];
 
 // Inputing string
 printf("Enter any word : ");
 scanf("%s",&word);

 // Copying word to word1
 strcpy(word1,word);
 
 // checking if palindrome or not
 if(strcmp(word,strrev(word1))==0)
 {
  printf("Entered word is a palindrome ");
 }
 
 else
  printf("Entered word is not palindrome ");

 return 0;
}

PALINDROME PROGRAM IN C WITHOUT USING STRING FUNCTION

Back To Palindrome Program

#include<stdio.h>

int main()
{
 // Declaring variable str=string 
 char word[50], revword[50];
 
 /* Declaring variable i=to iterate loop, l=length, 
 c=count the number of matched character */
 int i, j, l=0, c;
 
 // Inputing string
 printf("Enter any word : ");
 scanf("%s",&word);

 // finding length
 while(word[l]!='\0')
 l++;
 
 // Reversing string
 j=0;
 for(i=l-1;i>=0;i--)
 {
  revword[j]=word[i];
  j++;
 }
 revword[j]='\0';
 
 //checking if palindrome or not
 c=0;
 for(i=0;word[i]!='\0';i++)
 {
  if(revword[i]==word[i])
  c++;
 }
 
 if(c==l)
 {
  printf("Word is a palindrome ");
 }
 
 else
  printf("Word is not palindrome ");
 
 return 0;
}
Leap Year Program In C

Leap Year Program In C

Leap Year Program In C using if-else

// Leap year program in c using if-else
#include<stdio.h>

int main()
{
 // Declaring variable for y=year
 int y;
 
 // Inputing year
 printf("Enter year to check leap year or not: ");
 scanf("%d",&y);
 
 // Determining and displaying whether leap year or not
 if(y%4==0)
 {
  if(y%100==0 && y%400!=0)
  printf("Century year, But not a leap year");
  else
  printf("Leap year");
 }
 else
 printf("Not a Leap Year");

 return 0;
}

Leap Year Program In C Using Ternary

// Leap year program in c using ternary
#include<stdio.h>

int main()
{
 // Declaring variable "y" to input year and "leap" to determine leap year
 int y,leap;
 
 // Inputting year
 printf("Enter any year : ");
 scanf("%d",&y);
 
 // Checking if leaf year or not
 leap=(y%400==0)?:(y%100!=0)?(y%4==0)?1:0:0;

 if(leap==1)
 printf("The given year is leap year");
 else
 printf("The given year is not leap year");

 return 0;
}
C PROGRAM TO FIND FIBONACCI SERIES

C PROGRAM TO FIND FIBONACCI SERIES

WHAT IS FIBONACCI SERIES

Fibonacci series : Fibonacci series is one in which the nth term is sum of (n-1)th term and (n-2)th term. The first two numbers of series are 0 and 1.
For example: 0 1 1 2 3 5 8 13 21 and so on

Now we can write a c program to find fibonacci series in the following ways :

PROGRAM TO FIND FIBONACCI SERIES USING LOOPS

//C PROGRAM TO PRINT FIBONACCI SERIES USING LOOPS
 
 #include<stdio.h>
 
 int main()
 {
  /* Declaring variable for n=number, f=first, s=second, 
  t=third number, i=to iterate the loop */
  int n,f,s,t,i;
  
  // Inputing number till where fibonacci series is to be printed
  printf("Enter the number till which you want to print fibonacci series:");
  scanf("%d",&n);
  
  // Determining and printing fibonacii series
  printf("Fibonacci Series: ");
  
  f=0;
  s=1;
  printf("%d %d ",f,s);
  for(i=3;i<=n;i++)
  {
   t=f+s;
   printf("%d ",t);
   f=s;
   s=t;
  }
  
  return 0;
 }

C PROGRAM TO FIND FIBONACCI SERIES USING FUNCTION

//C PROGRAM TO FIND FIBONACCI SERIES USING FUNCTION
#include<stdio.h>

void fibonacci(int n);
int main()
{
  int n;

  // Inputing number till where fibonacci series is to be printed
  printf("Enter the number till which you want to print fibonacci series:");
  scanf("%d",&n);
  
  fibonacci(n);
  return 0;
}

void fibonacci(int n)
{
  /* Declaring variable for f=first, s=second, 
  t=third number, i=to iterate the loop */
  int f,s,t,i;
  
  // Determining and printing fibonacii series
  printf("Fibonacci Series: ");
  
  f=0;
  s=1;
  printf("%d %d ",f,s);
  for(i=3;i<=n;i++)
  {
   t=f+s;
   printf("%d ",t);
   f=s;
   s=t;
  }
}

C PROGRAM TO FIND FIBONACCI SERIES USING RECURSION

//WRITE A C PROGRAM TO FIND FIBONACCI SERIES USING RECURSION
#include<stdio.h>

int fibonacci(int n);

  /* Declaring variable for f=first, s=second, 
  t=third number, i=to iterate the loop */
  int f,s,t,i;
  f=0;
  s=1;

int main()
{
   int n;
  // Inputing number till where fibonacci series is to be printed
  printf("Enter the number till which you want to print fibonacci series:");
  scanf("%d",&n);
  
  printf("Fibonacci Series: ");
  printf("%d %d ",f,s);
  
  fibonacci(n);
  return 0;
}

int fibonacci(int n)
{
 if(n==2)
 {
    return 0;
 }
 
 else
 {
    t=f+s;
    printf("%d ",t);
    f=s;
    s=t;
    fibonacci(n-1);
 }
}
Note : You can write much better code than these. So view these example and try to program by yourself. Use your own logic. This would improve your programming skills.
Reverse String In C / Reversing String In C

Reverse String In C / Reversing String In C

Reversing string in C / Reverse String In C seems to be a daunting task for the newbies but after reading this post you can easily reverse string in C.

What Is Reverse String In C

Reverse string in c is basically taking input( i.e sentence ) from console and then display the sentence from last character to first character.

The following Program to reverse string in c accepts a string from the user and print the reverse string. For example consider the following input from the user :

Input String : The site name is comp-psyche.com
Output String : moc.ehcysp-pmoc si eman etis ehT

There are various ways to reverse string in c. You can either use string function to reverse string in c or you can reverse string in c without using c string function.
In the following program we will reverse string in c using string function streev()


#include<stdio.h>
#include<string.h>

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);

 // Printing string and reversed string
 printf("String : %s\n",str);
 printf("Reversed string : %s",strrev(str));

 return 0;
}

REVERSE STRING IN C WITHOUT USING STRING FUNCTION

In the following program we will reverse string in c without using string function



int main()
{
 // Declaring variable str=string, revstr[50]=reverse string
 char str[50], revstr[50];
 
 // Declaring variable i=to iterate loop, l=length
 int i, j,l=0;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);

 // finding length
 while(str[l]!='\0')
 l++;
 
 // Reversing string
 j=0;
 for(i=l-1;i>=0;i--)
 {
  revstr[j]=str[i];
  j++;
 }
 revstr[j]='\0';
 
 // Printing string and reversed string
 printf("String : %s\n",str);
 printf("Reversed string : %s",revstr);

}

REVERSE STRING IN C USING RECURSION

In the following program we will reverse string in c using recursion


// Write a c program to reverse a string using recursion
#include<stdio.h>
void reverse (int index, char *str );

int main (void)

{

   char str[100];
   printf ("Enter any string : "); 
   gets(str);
 reverse (strlen(str) , str );
 return 0;

}

void reverse (int index, char *str )

{

  if (--index < 0 )

  {
       return ;
  }

  else
  {
        putchar ( *(str + index) ) ;  
        reverse (index, str) ;
  }

}

REVERSE STRING IN C USING POINTER

In the following program we will reverse string in c using pointer

// Write a C program to reverse a string using pointer
#include<stdio.h>
int main()
{
    
    // Declaring variable str = string and revstr = to store the revere string
    char str[50];
    char revstr[50];
    
    char *strptr = str; // strptr - holds the first position address of str
    char *revptr = revstr; // revptr - holds the first position address of revstr
    int i=-1;

    // Inputting string
    printf("Enter any string : ");
    gets(str);
   
    // strptr is pointed to the last position address
    while(*strptr)
    {
        strptr++;
        i++;
    }

    // string stored in str is copied to revstr
    while(i>=0)
{
       strptr--;
       *revptr = *strptr;
       revptr++;
       --i;
    }

    *revptr='\0';
  
    printf("Reverse of string is : %s",revstr);
  
    return 0;
}