Friday 27 November 2015

Program to check whether a number is Palindrome or not

//Program to check whether a number is Palindrome or not.
#include<stdio.h>
#include<conio.h>

void main()
{
      int  number,reverse=0, temp; /* 'reverse' is  used to store the reverse of a number and 'temp' to store the number for calculations*/
      int rem=0; // used to store the reminder of the number

      clrscr(); // for clearing the console screen

      printf("Enter the number : ");
      scanf("%d",&number);
      temp = number; //copies the value of 'number' in 'temp'

// logic for palindrome (the reverse of the number is equal to the number)

      while(temp != 0) // checking whether 'temp' is equal to 0 or not
      {
         rem=temp%10; // calculating the reminder of the number 
     reverse=reverse*10+rem; // calculating the reverse of the number
     temp=temp/10; // dividing the number by 10
      } // end of while loop

       
 if(reverse==number) /* Checking if number entered by user and it's reverse number is equal. */
     printf("Number is Palindrome");
     else
     printf("Number is not Palindrome");

      getch(); // for holding the console screen
}

Wednesday 25 November 2015

Program which inputs string and print it, using library function 'gets()'.

//Program which inputs a string and print it, using library function 'gets()'.

#include<stdio.h> // to load standard input output library
#include<conio.h> // to load console input output library

void main() // execution of any program starts from here
{
      char str[100]; // an array of 'char' type having 100 as its size
clrscr(); // used to clear the console screen

      printf("Enter a string: \n"); // function prints 'Enter a string' on the console screen
      gets(str); // function which inputs strings having spaces between them

      printf("String entered: %s",str); // function prints the strings entered by user
      getch(); // to hold the output screen
}

Program to find the Factorial of a number.

//Program to find the Factorial of a number.
#include<stdio.h>
#include<conio.h>

void main()
{
      int number,i; // variable 'number' is used to take input from user 
      int factorial; // integer variable used to store the factorial of a number

// Memory allocated is depended on the type of compiler

      clrscr(); // clearing the console screen (part of 'conio.h' header file)

      printf("Enter the number: "); //asks for a integer value
      scanf("%d",&number); // stores the integer value in variable 'number'
      

// logic for factorial of a number

      factorial=1; // initalizes 'factorial' with 1
      for(i=number ; i>0 ; i--) /* here the loop starts from number because the factorial of a number is in the form : number*(number-1)*(number-2)........*/
      {
      factorial = factorial*i; // calculates the factorial of a number
      }// end of i loop

      printf("The factorial of  %d is:%d",number,factorial); // prints the factorial of the number      
     
     getch(); // holding the output screen
}

Program to take string input from user and print it on the screen without built-in functions

//Program to take string input from user and print it on the screen without built-in functions

#include<stdio.h>

#include<conio.h>


void main()

{

      int n; // for string size

      char str[50]; // declares an character array and allocates memory to it

      int i; // integer variable used for looping

// Memory allocated is depended on the type of compiler


      clrscr(); // clearing the console screen (part of 'conio.h' header file)


      printf("Enter number of characters to be entered: ");

      scanf("%d",&n);

      printf("Enter input string: "); // asks for a string value

      for(i=0; i<=n; i++) // loop to take input step by step

      {

            scanf("%c",&str[i]); //stores value at the respective index

      } //printing the value stored in the respective character array


      printf("\nInputted string is:" ); // \n is used for printing from a new line

      for(i=0; i<=n; i++) // loop to take input step by step

      {

            printf("%c",str[i]); //stores value at the respective index

      }

      getch(); // for holding the output screen

}

Program to print Fibonacci series (0,1,1,2,3,5,8,....................n terms)

//Program to print Fibonacci series (0,1,1,2,3,5,8,....................n terms)
#include<stdio.h>
#include<conio.h>

void main()
{
      int a=0, b=1, c, i, n; /* 'a' and 'b' can be taken as input if the series has to be start from a certain point */
      clrscr(); // for clearing the console screen
      printf("Enter the number of terms to be printed: ");
      scanf("%d",&n);
      printf("\nThe series is: %d%d\n",a,b); // prints the initial values ,i.e., 0 and 1

// logic for the series (each term is the sum of its preceding two terms)

      for(i=0; i<n-2; i++) // here 'n-2' is taken because the initial two values are printed separately
      {
            c=a+b;
            printf("%d",c);
            a=b;
            b=c;
      } // end of i loop

      getch(); // for holding the console screen
}

Saturday 21 November 2015

Program to take input from user and print it on the screen.

//Program to take different types of input from user and print it on the screen
#include<stdio.h>
#include<conio.h>

void main()
{
      int a; // declares an integer variable and allocates memory to it
      float b; // declares a character variable and allocates memory to it
      char c; // declares an decimal variable and allocates memory to it

// Memory allocated is depended on the type of compiler

      clrscr(); // clearing the console screen (part of 'conio.h' header file)

      printf("Enter integer value: "); //asks for a integer value
      scanf("%d",&a); // stores the integer value in variable 'a'
      printf("\nEnter decimal value: "); //asks for a decimal value (upto 6 decimal places only)
      scanf("%f",&b); // stores the decimal value in variable 'b'
      printf("\nEnter character value: "); //asks for a character value (only a single character)
      scanf("%c",&c); // stores the character value in variable 'c'

//printing the value stored in the respective variables

      printf("\nInputted integer is: ",a); // "\n" is used for printing from a new line
      printf("\nInputted decimal is: ",b);
      printf("\nInputted character is: ",c);

      getch(); // holding the output screen
}

Program to print Hello World

// Program to print any text/string on the output screen
#include<stdio.h> // to load the standard input output library
#include<conio.h> // to load the console input output library

void main() // execution of any program starts from here
{
      clrscr(); //used to clear the console screen
      printf("Hello World"); // printf() is used to print on screen
      getch(); // to hold the output screen
}