facebook like button

16 July, 2011

ABCD pattern2

The need:
This program was asked by a reader.
The code:
---------------------------------------------------------
#include<stdio.h>
main()
{
    char z;
    int j,i,k;
    printf("Enter the number of rows..(1 to 26)\t");
    scanf("%d",&k);
    if(k<1||k>26)
    {
        printf("\nThe number entered was not in range of 1 to 26\n");
        printf("exiting...\n");
        exit(0);
    }
    printf("\n\n");
    for (i=0;i<k;i++)
    {
        z = 'A';
        for (j=0;j<k;j++)
        {   
            if(j<k-i)
            printf ("%c  ",z);
            else
            printf("_  ");
            z++;
        }
        z--;
        for (j=k;j>0;j--)
        {   
            if(j<=k-i)
            printf ("%c  ",z);
            else
            printf("_  ");
            z--;
        }
    printf("\n\n");
    }
}
--------------------------------------------------------- 

Output:
output of the program will be some thing like this:
sample output when input 8 is given.
A  B  C  D  E  F  G  H  H  G  F  E  D  C  B  A  

A  B  C  D  E  F  G  _  _  G  F  E  D  C  B  A  

A  B  C  D  E  F  _  _  _  _  F  E  D  C  B  A  

A  B  C  D  E  _  _  _  _  _  _  E  D  C  B  A  

A  B  C  D  _  _  _  _  _  _  _  _  D  C  B  A  

A  B  C  _  _  _  _  _  _  _  _  _  _  C  B  A  

A  B  _  _  _  _  _  _  _  _  _  _  _  _  B  A  

A  _  _  _  _  _  _  _  _  _  _  _  _  _  _  A  

15 July, 2011

program to reverse the digits of a given natural number

The need:
    This program was asked me by a reader of this blog.
The code:
-------------------------------------------------------

#include<stdio.h>
main()
{
    int number1,i,number2=0;
    printf("enter a natural number.\n");
    scanf("%d",&number1);
    if(number1<1)
    {
        printf("the number entered was not a natural number.\n");
        exit(0);
    }
    i=number1;
    while(i>0)
    {
        number2=10*number2+i%10;
        i=i/10;
    }
    printf("the reverse of %d number is %d\n",number1,number2);
}

-------------------------------------------------------

Calculating sum of digits of a natural number

The need:
    This program was asked me by a reader of this blog.
The code:
-------------------------------------------------------

#include<stdio.h>
main()
{
    int number,i,sum=0;
    printf("enter a natural number.\n");
    scanf("%d",&number);
    if(number<1)
    {
        printf("the number entered was not a natural number.\n");
        exit(0);
    }
    i=number;
    while(i>0)
    {
        sum+=i%10;
        i=i/10;
    }
    printf("sum of digits of %d is %d\n",number,sum);
}

-------------------------------------------------------

To print all prime numbers between two given numbers m and n

The need:
    This program was asked me by a reader of this blog. This program takes 2 numbers from the user and prints all the prime numbers between them (inclusive both the numbers).
The code:
-------------------------------------------------------
#include<stdio.h>
#include<math.h>
int isPrime(int );
main()
{
    int number1,i,number2;
    printf("enter a natural number.\n");
    scanf("%d",&number1);
    printf("enter second natural number.\n");
    scanf("%d",&number2);
    if(number1<1||number2<1)
    {
        printf("one of the numbers entered was not a natural number.\n");
        exit(0);
    }
    printf("the prime numbers between %d and %d are =>\n\n",number1,number2);
    for(i=number1;i<=number2;i++)
    {
        if(isPrime(i))
        {
            printf("%d\t",i);
        }
    }
    printf("\n\n");
}

int isPrime(int x)            //this function returns one if the
{                            //input number x is prime else returns 0
    int i,flag=1;
    if(x==1)   //people say 1 is not a prime number
     return 0;
    else if(x==2)  //people say 2 is a prime number
     return flag;
    else    //just following the definition for the number greater than 2
    for(i=2;i<=sqrt(x);i++)
    {
        if(x%i==0)
        {
            flag=0;
            break;
        }
    }
    return flag;
}

------------------------------------------------------- 

Remarks:
  If you are using linux/unix to compile this program, add '-lm'(without quotes) after the compile command. Ex. : if the file name is file.c, give the command:
"cc file.c -lm"(without quotes)
instead of only "cc file.c"

14 July, 2011

program 67: doubly linked list

The need:
      This program was written to learn how to use a doubly linked list. But the question come, "why is doubly linked list needed???" First of all I want to tell that the name doubly linked list comes from the fact that this linked is in deed connected doubly. Meaning is that each node has the address of its previous and next node unlike singly connected linked lists in which each node had address of its next node. So in singly connected linked lists we can traverse in only forward direction while in doubly connected inked list we can traverse in both directions. This is the need and plus point of doubly connected linked lists.




The code:
-----------------------------------------------------

// This a basic program to make a doublly linked list and to do some operation on it
#include<stdio.h.>
#include<stdlib.h>
// defining data-type node
struct linked_list
{
int num;
struct linked_list *next,*prev ;
} ;
typedef struct linked_list node ;
//declaration of functions
void create_list(node *p);
void sort(node *p);
void print(node *list);
void exchange(int *,int *);
main()
{
    int i;
    node *head ;
    head=(node *)malloc(sizeof(node));
    head->prev=NULL;
    printf("Enter a element numbers .\n");
    printf("Type -999 to end :\n");
    create_list(head);
    printf("The list created Successfully .\n");
    printf("The list is .\n");
    print(head);
    sort(head);
    printf("\nThe sorted list is .\n");
    print(head);
}
void create_list(node *list)
{
    scanf("%d",&list->num);
    if(list->num==-999)
        list->next=NULL;
    else
    {
        list->next=(node *)malloc(sizeof(node));
        list->next->prev=list;
        create_list(list->next);         //recursion
    }
}

void exchange(int *s1,int *s2)
{
    int temp;
    temp =*s1;
    *s1=*s2;
    *s2=temp;
}

void print(node *list)
{
    if(list->next!=NULL)
    {
    printf("%d-->",list->num);
    if(list->next->next==NULL)
        printf("END\n");
    print(list->next); //recursion
    }
}

void sort(node *list)
{
    node *p,*q;
    for(p=list;(p->next)!=NULL;p=p->next)
    for(q=list;(q->next->next)!=NULL;q=q->next)
    {
    if(q->num > q->next->num)
        exchange(&(q->num),&(q->next->num));
    }
}

-----------------------------------------------------
The approach:
    The approach is same as singly linked list. See there is a slight change in the structure of the datatype node and to keep track of *prev there are slight changes made in the UDFs also.