Wednesday, December 31, 2014

Solve the Magic Squares Puzzle in c pgm



Solve the Magic Squares Puzzle without using Recursion

#include 
#include 
void magicsq(int, int [][10]);
 
int main( )
{
    int size;
    int a[10][10];
    clrscr();
    printf("Enter the size: ");
    scanf("%d", &size);
    if (size % 2 == 0)
    {
        printf("Magic square works for an odd numbered size\n");
    }
    else
    {
        magicsq(size, a);
    }
    return 0;
}
 
void magicsq(int size, int a[][10])
{
    int sqr = size * size;
    int i = 0, j = size / 2, k;
 
    for (k = 1; k <= sqr; ++k) 
    {
        a[i][j] = k;
        i--;
        j++;
 
        if (k % size == 0) 
        { 
            i += 2; 
            --j; 
        }
        else 
        {
            if (j == size) 
            {
                j -= size;
            }
            else if (i < 0)
            {
                i += size;
            }
        }
    }
    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size; j++)
        {
            printf("%d  ", a[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    getch();
}

Output
 
Enter the size: 6
Magic square works for an odd numbered size
 
$ a.out
Enter the size: 5
17  24  1  8  15  
23  5  7  14  16  
4  6  13  20  22  
10  12  19  21  3  
11  18  25  2  9

First Capital Letter in a c pgm



Find the First Capital Letter in a String without using Recursion

#include 
#include 
#include 
#include 
 
char caps_check(char *);
 
int main()
{
    char string[20], letter;
    clrscr();
    printf("Enter a string to find it's first capital letter: ");
    scanf("%s", string);
    letter = caps_check(string);
    if (letter == 0)
    {
        printf("No capital letter is present in %s.\n", string);
    }
    else
    {
        printf("The first capital letter in %s is %c.\n", string, letter);    }
        return 0;
    }
    char caps_check(char *string)
    {
        int i = 0;
        while (string[i] != '\0')
        {
            if (isupper(string[i]))
            {
                return string[i];
            }
            i++;
        }
        return 0;
    }

Output
 
Enter a string to find it's first capital letter: prOgraMmInG
The first capital letter in prOgraMmInG is O.

Solve Tower-of-Hanoi Problem using Recursion

#include 
#include  
void towers(int, char, char, char);
 
int main()
{
    int num;
    clrscr();
    printf("Enter the number of disks : ");
    scanf("%d", &num);
    printf("The sequence of moves involved in the Tower of Hanoi are :\n");
    towers(num, 'A', 'C', 'B');
    return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
    if (num == 1)
    {
        printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
        return;
    }
    towers(num - 1, frompeg, auxpeg, topeg);
    printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
    towers(num - 1, auxpeg, topeg, frompeg);
    getch();
}

Output
 
Enter the number of disks : 3
The sequence of moves involved in the Tower of Hanoi are :
 
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C

 

Search for an Element in c pgm



Search for an Element in the Linked List without using Recursion

#include 
#include 
#include 
 
struct node
{
    int a;
    struct node *next;
};
 
void generate(struct node **, int);
void search(struct node *, int);
void delete(struct node **);
 
int main()
{
    struct node *head = NULL;
    int key, num;
    clrscr();
    printf("Enter the number of nodes: ");
    scanf("%d", &num);
    printf("\nDisplaying the list\n");
    generate(&head, num);
    printf("\nEnter key to search: ");
    scanf("%d", &key);
    search(head, key);
    delete(&head);
 
    return 0;
}
 
void generate(struct node **head, int num)
{
    int i;
    struct node *temp;
 
    for (i = 0; i < num; i++)
    {
        temp = (struct node *)malloc(sizeof(struct node));
        temp->a = rand() % num;
        if (*head == NULL)
        {
            *head = temp;
            temp->next = NULL;
        }
        else
        {
            temp->next = *head;
            *head = temp;
        }
        printf("%d  ", temp->a);
    }
}
 
void search(struct node *head, int key)
{
    while (head != NULL)
    {
        if (head->a == key)
        {
            printf("key found\n");
            return;
        }
        head = head->next;
    }
    printf("Key not found\n");
}
 
void delete(struct node **head)
{
    struct node *temp;
 
    while (*head != NULL)
    {
        temp = *head;
        *head = (*head)->next;
        free(temp);
    }
}

Output
 
Enter the number of nodes: 10
 
Displaying the list
3  6  7  5  3  5  6  2  9  1  
Enter key to search: 2
key found