Thursday, February 13, 2014

C Tutorial: Floyd Triangle

Floyd's triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner.

Source Code:

#include <stdio.h>

int main() {
int rows, a,  b, number = 1;

printf("Number of rows to print:");
scanf("%d",&rows);

for ( a = 1 ; a <= rows ; a++ ) {
for ( b = 1 ; b <= a ; b++ ) {
printf("%d ", number);
number++;
}
printf("\n");
}

}

Output is as:
                         

Tuesday, February 11, 2014

C-Tutorial Checking for Palindrome

Palindrome means a word, phrase, or sequence that reads the same backwards as forwards, e.g. madam or dad.

Source Code Example to check whether the Word is  Palindrome or not 

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

int main() 

{
    char one[200]={""};
    char two[200];

    printf("Enter the word you want to check.");
    scanf("%s",one);
  
    strcpy(two, one);
    strrev(two);
  
    if(strcmp(one, two) == 0)
        printf("The entered string %s is a palindrome.\n", one);
    else
        printf("The entered string %s is not a palindrome.\n", one);
  
    printf("The reverse of the string is %s.\n", two);
  }


Source Code Example to check whether the Number is  Palindrome or not

#include <stdio.h>

int main()
{
    int number, reverse = 0, temp;
   
    printf("Enter number:");
    scanf("%d", &number);
   
    temp = number;
   
    while( temp != 0 )
   {
        reverse = reverse * 10;
        reverse = reverse + temp%10;
        temp = temp / 10;
    }
   
    if ( number == reverse )
        printf("The number is a Palindrome number.\n");
    else
        printf("The number is not a Palindrome number.\n");
   
 }


Enjoy coding :)