Showing posts with label Puzzles Program. Show all posts
Showing posts with label Puzzles Program. Show all posts

Write a Program to check for Magic Squares in c language

Magic Squares
In any magic square, the numbers in every row, column, and diagonal all have the same magic sum.
Now, your challenge is to create a program that would check to see if the series of numbers
below is a magic square.Make sure you take into account all of the possibilities!!

13          8         12     1
2          11         7      14
3          10         6      15
16         5          9      4 

Ans :

#include<stdio.h>

int magic[][4] = {13,8,12,1,2,11,7,14,3,10,6,15,16,5,9,4};

int CheckRow() {
    int i, j, firstRow = 0, sum = 0;
    for(i = 0; i<4; i++) {
    firstRow += magic[0][i]; // find total of first row
    }
    for(i = 1; i<4; i++) {
    sum = 0;
    for(j = 0; j<4; j++) {
        sum += magic[i][j];
    }
    if(sum != firstRow) return 0;
    }
    return firstRow;
}

int CheckColumn(int presum) {
    int i, j, sum = 0;
    for(i = 0; i<4; i++) {
    sum = 0;
    for(j = 0; j<4; j++) {
        sum += magic[j][i];
    }
    if(sum != presum) return 0;
    }
    return 1;
}


int CheckDiagonal(int presum) {
    int i,sum = 0, c = 3;
    for(i = 0; i<4; i++) sum += magic[i][i]; //upper left to lower right 0,0 to 3,3
    if(sum != presum) return 0;
    sum = 0;
    for(i = 0; i<4; i++) {
    sum += magic[i][c--]; // upper right to lower left, 0,3 to 3,0
    }
    if(sum != presum) return 0;
    return 1;
}

void Find() {
    int presum = CheckRow();
    if(presum == 0) {
    printf("Not a magic square\n");
    return;
    }
    if(CheckColumn(presum) == 0) {
    printf("Not a magic square\n");
    return;
    }
    if(CheckDiagonal(presum) == 0) {
    printf("Not a magic square\n");
    return;
    }
    printf("Magic square\n");
}
void main() {
    Find();
    getch();
}

Write a program to display Well Ordered Numbers | CTechnotips

 Well Ordered Numbers The number 138 is called well-ordered because the digits in the number (1,3,8) increase from left to right (1 < 3 < 8). The number 365 is not well-ordered because 6 is larger than 5. Write a program that will find and display all possible three digit well-ordered numbers. Report the total number of three digit well-ordered numbers.

Ans :

void main() {
    int i, j, k, total = 0;
    for(i = 1; i<8; i++) {
    for(j = i+1; j<9; j++) {
       for(k = j+1; k<10; k++) {
        printf("%d%d%d ",i,j,k);
        total++;
        if(total % 8 == 0) printf("\n");
       }
    }
    }
    printf("\n");
    printf("Total Well Ordered Numbers of 3 digits are: %d\n",total);
    getch();
}

Write a program in c which takes Roman numbers as input and convert it equivalent Decimal number

#include<stdio.h>

#define maxn 10

int value[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
char Roman[][3] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};

//MCDXLVIII = 1448

int Find(char key[]) {
    int i;
    for(i = 12; i>=0; i--) {
        if(!strcmp(Roman[i],key)) return value[i];
    }
    return 0;
}

int Convert(char roman[]) {
    int length = strlen(roman) - 1;
    int i, val, sum = 0, find;
    char tempRoman[4];

    for(i = length; i>=0; ) {
    find = 0;
    if(i >= 1) {
        tempRoman[0] = roman[i-1];
        tempRoman[1] = roman[i];
        tempRoman[2] = NULL;
        val = Find(tempRoman);
        if(val > 0) {
            sum += val;
            find = 1;
            i -= 2;
           }
      }
    if(find == 0) {
        tempRoman[0] = roman[i];
        tempRoman[1] = NULL;
        sum += Find(tempRoman);
        i--;
    }
    }
    return sum;
}

void main() {
 char roman[100];
 clrscr();
 printf("\n Insert a valid Roman number, all characters must be upper case \n lx is an invalid input where LX is a valid input \n Press Clt+z or Clt+c to exit from the program \n Input range: up to 3999 ( MMMCMXCIX ) \n\n");

 while(scanf("%s",roman) != EOF)  {
  printf("%d\n",Convert(roman));
 }
    getch();
}

Find the largest number among an integer set without using -if condition, bit operations,<,>,>>,<<,? Operators and also without using any library function except scanf and printf.

#include<stdio.h>

int a[100];

int ABS(int n) { // This is the most important function, return abs value of n
    int up = 0, down = 0, count = 0;
    for(count = 0; up != n && down != n; count++) { // finding abs value
    down--;
    up++;
    }
    return count;
}
int getMax(int x, int y) { // this function returns largest value among x and y
    int res;
    int d=x-y;
    d=ABS(d);
    res=( x - y ==-d) * y +  ( x - y == d) * x; // calculating max between x and y
    return res;
}
void  main()
{
    int n, i, max = -1000000;
    clrscr();
    printf("\nEnter how many number you want to check : - ");
    scanf("%d",&n);
    printf("\n\nPlease Enter %d numbers : \n\n",n);
    for(i = 0; i<n; i++) {
    scanf("%d",&a[i]); // >= -1000 && <= 1000
    }
    for(i = 0; i<n; i++) {
    max = getMax(max, a[i]);
    }
    printf("\nMaximum number is : - %d\n",max);
    getch();
}

Write a program to convert string to integer without library function.

#include<stdio.h>

char input[100];

int Convert() {
    int sum = 0, i, p = 1, digit;
    for(i = strlen(input) - 1; i>=0; i--) {
        digit = input[i] - '0';
        sum += digit*p;
        p *= 10;
    }
    return sum;
}
void main() {
    int n;
    clrscr();
    printf("\n Enter Number (Input as String) : - ");
    scanf("%s",input);
    n = Convert();
    printf("\n Integer Number ( Not String ): - ");
    printf("%d\n",n);
    getch();
}

Write a program to convert integer to string (character array) without library function.

#include<stdio.h>

char String[100];

int IntToString(int n) {
    int k = 0;
    if(n == 0) {
    return 0;
    }
    k = IntToString(n/10);
    String[k] = n%10 +'0';
    return k+1;
}

void main() {
    int n, k = 0;
    clrscr();
    printf("\n Please Enter Integer Number : - ");
    scanf("%d",&n);
    if(n == 0) {
    String[0] = 0+'0';
    String[1] = NULL;
    }
    else {
    k = IntToString(n);
    String[k] = NULL;
    }
    printf("\n\n Integer Number Converted into String is : - ");
    printf("%s\n",String);
    getch();
}

Write a program to accept 10 numbers and print it in reverse order.(Do not use array)‌

int main()
{
static int n=10;
if(n--==0)
return 0;
else
{
int x;
scanf("%d",&x);
main();
printf("%d\n",x);
}
return 0;
}



*****************************  Or   *****************************


// Using Pointer pointer


#include<stdio.h>
 

void main()
{
 

int *ptrint;
int ptr;
int i;

clrscr();

ptrint = &ptr;

printf("\nEnter 10 numbers :\n");

for(i=0;i<10;i++)
{
scanf("%d",ptrint);
ptrint++;

}
ptrint--;
printf("\n10 Numbers in reverse order.\n");
for(i=0;i<10;i++)
{
printf("%d\n",(int)*ptrint);
ptrint--;
}
getch();

}