Showing posts with label Coding puzzles. Show all posts
Showing posts with label Coding puzzles. Show all posts

Thursday, May 31

SELECTION SORT ~ CUSAT Previous Questions with Answers- C programming.( S1 S2)



Hi friends,

Here I'm posting a C program implementing Selection sort. It is specially dedicated to S1 S2 B tech students because Selection sort is one of the most frequently asking questions in "Computer  Programming" exams. Hope this will be Useful  4  you.!!! :)



#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j,temp,a[50]; 


printf("\nEnter how many elements=");
scanf("%d",&n); 


printf("\nEnter %d elements",n);//
Input the elements
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);




for(i=0;i<n-1;i++)//
Sorting the elements
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf("\nSorted Array:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}

}


Any doubt in the code? Comment it here..!!

Monday, May 28

CUSAT Previous Questions with Answers- C programming.( S1 S2)





Hi Friends,


here I'm posting one of the frequently asking question in CUSAT B tech C programming ( s 1 s2 ) and its solution:

Question:



Write a C program which prints all Armstrong numbers between 1 and 1000.


Solution:



#include<stdio.h>
main()
{
int number, temp, digit=0, dup;

printf("Printing all Armstrong numbers between 1 and 500:\n\n");

number = 1;

while (number <= 500)
{
temp=0;
dup=number;
while(dup>0)
{
digit=dup%10;
temp+=(digit*digit*digit);
dup=dup/10;
}
if (temp==number)
{
printf("\nAmstrong Number:%d", temp);
}
number++;
}
getch();
}
 

Monday, May 21

Generate Interesting Patterns Using C

Hi friends,

Look at the "+"  pattern generated by asterisks. Quite interesting na..!! :)
Lets have a look in a C program which generates exactly the same pattern.!!!!!

 main()
{
int i,j;
for ( i= 0; i < 11; ++i)
{
for ( j = 0; j < 11; ++j)
      if (j != 5 && i !=5 )
     printf(" "); 
      else printf ("* ");
printf("\n");
}
}



Saturday, March 17

Concatenate strings without using library functions


#include<stdio.h>


main()
{
      char first[20], second[20],result[40];
      int i,j;
      printf("Enter first string:");
      gets(first);
      printf("Enter second string:");
      gets(second);
   for(i=0;first[i]!='\0';i++)result[i]=first[i];
   for(j=0;second[j]!='\0';i++,j++)result[i]=second[j];
      
      printf("%s",result);


      }

Concatenate strings using library functions



#include<stdio.h>
#include<string.h>
main()
{
      char first[20], second[20],result[40];
      int i,j;
      printf("Enter first string:");
      gets(first);
      printf("Enter second string:");
      gets(second);
    
      for(i=0;i<strlen(first);i++)result[i]=first[i];
      for(j=0;j<strlen(second);i++,j++)result[i]=second[j];
      result[i]='\0';
    
      printf("%s",result);
  
      }

Fibonacci series using Recursion ~ C program


Here is a C program which prints a Fibonacci series up to a limit using recursion.





#include<stdio.h>


void fibonacci(int a, int b,int count)


{
     printf("%d\t",a+b);
     if(--count) fibonacci(b,a+b,count);
}


void main()
{
     printf("Enter limit:");
     int count;
     scanf("%d", &count);
      printf("0\t 1\t");
     fibonacci(0,1,count-2);
   
     }
    

Saturday, January 21

Convert Decimal Number to Binary Equivalent Using Linked list - C




Hi friends,


          In this post I'll explain "how  to convert a decimal number to binary equivalent using Linked list". Usually, we use array for this purpose, but arrays are created statically and memory wastage  may occur. Linked list is a  perfect solution for this as it is created dynamically and no there is no wastage of memory also.!!!!






Algorithm




  1. Initialize PREV_NODE and NODE as NULL.
  2. Input a number 'num'.
  3. if num<=0 go to step 9
  4. PREV_NODE=NODE
  5. add (num%2) to NODE->number
  6. NODE->next=PREV_NODE
  7. num=num/2
  8. go to step 3
  9. if NODE is NULL go to 13
  10. print NODE->number
  11. NODE=NODE->next 
  12. go to step 9
  13. End






The complete code in C is given here:




#include<stdio.h>


struct binary {
       int num;
       struct binary *next;
       };


struct binary* add( struct binary **curr, int n)
 {
  
   struct binary *temp;
   temp=(struct binary *)malloc(sizeof(struct binary));
   temp->num=n;
   temp->next=*curr;
   return temp;
      
 }
       


int main()
{
     int number, binary_num;
     struct binary *head= NULL;
     printf("ENTER A DECIMAL NUMBER  :   ");
     scanf("%d",&number);
     
     while(number>0)
     {
     binary_num=number%2;
     head =add(&head,binary_num);
     number/=2;
     }
  printf("EQUIVALENT BINRY NUMBER IS   :   ");  
  for(;head!=NULL;head=head->next)
     printf("%d",head->num);


}










Friends, I hope this post will be useful to you..!!!
Your valuable suggestions are always welcomed..!!!!Comment it here.!!


Saturday, September 17

Print a string without using any semicolon

Write a program to print a  string without using any semicolon on the program:


#include<stdio.h>
main()
{
      if(printf("Hello.."))
      {
      }

}




I want to know how this program can modified to print any string accepted through Keyboard..!!

If anyone know, feel free to comment it here..!!  :):):)

Letter Pattern

Write a program to print the following
         A
       B B
      CC CC
   DDD DDD





#include<stdio.h>
main()
{
      int i,j,n;
      printf("Enter Limit..  :  ");
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {
      for(j=0;j<(n-i);j++)
      printf(" ");
      if(i==0)putchar(65);
      for(j=0;j<i;j++)putchar(65+i);      printf(" ");
        for(j=0;j<i;j++)putchar(65+i);
        printf("\n");
        }
        getch();
        }
     

C program to print a Number pattern

Write a program to print the following pattern:
0

1 1
2 3 5
8 13 21 34



Hi buddy, Jst analyse the no.s in order, 0,1,1,2,3,5,8,13,21,34....
Yess!!!! this is a Fibonacci series.

0+1=1
1+1=2
1+2=3
2+3=5... and so on

Next requirement is to generate pattern. First line has 1 element, second line has 2 elements and so on. And total number of elements in whole pattern will be N*(N+1)/2 , if there are N number of lines.





#include<stdio.h>
main()
{
      int n,i,j,k=0;
      int a[20];
      printf("Enter the lmit..:   ");
      scanf("%d",&n);
      a[0]=0;
      a[1]=1;
      for(i=2;(i<n*(n+1)/2);i++)
      a[i]=a[i-1]+a[i-2];
      for(j=0;j<=n;j++)
     {
      for(i=0;i<j;i++)
      printf("%d\t",a[k+i]);
      k+=i;
      printf("\n");
      }
      getch();
}

C program to Print a number table

Write a program to display the following format:
Nosum
11
23
36
410
515

Logic:

First column contains natural numbers serially..
second column contains  ( Current first column element+ previous second column element)
2+1=3
3+3=6
4+6=10
5+10=15




#include<stdio.h>
main()
{
      int j=0,z,i,n;
      printf("Enter the Limit...  :  ");
      scanf("%d",&n);
      printf("\n----------------\n
printf("no\tsum");
printf("\n------------------\n");
     
      for(i=1;i<=n;i++)
      {
      printf("\n%d",i);
      j+=i;
      printf("\t%d",j);
      }
      getch();
      }
     
     
     





Reverse words in a string using Recursion

Write a program to perform the following to any input string:
     
       Input : Structure is a derived datatype
      output: erutcurtS si a devired epytatad

My Code:

#include<stdio.h>
#include<string.h>
int main()
{
    char line[20],word[10];
    int i,j=0,len;
    printf("Entera string :  ");
    gets(line);
    len=strlen(line);
    for(i=0;i<len;i++)
    {
    while(line[i]!=' '&&i<len)
    word[j++]=line[i++];
    for(j--;j>=0;j--)
    printf("%c",word[j]);
    printf(" ");

    j=0;
    }
    getch();
    return 1;
}


I hope this code was useful for u!!
Have a better program for ths prblm??
Feel free to comment it here..!!  :):):)

C program to find Size of a structure without using sizeof() operator

Write a program to find the size of following structure without using size of operator

struct ABC
 {
 int a;
 float b;
 char c;
 };



Hai Viewer, confused?? Hmm.. calculating the size of a datatype without using sizeof() operator seems to be weird. But pointers will help you to solve this problem.  First u have to initialize a null pointer on intended datatype .You know that in 'structure ' separate memory will be allocated to each element. So, on incrementing that pointer variable, the value of that pointer will be incremented by size of that structure . That is our Goal..!!!! :)
Since we have initialized pointer with 0 we simply need to print the current pointer value..!!



solution

#include<stdio.h>
struct  ABC
{
    int a;

    float b;
    char c;
};
int main()
{

    struct ABC *ptr=(struct ABC *)0;
    ptr++;
printf("%d",ptr);
getch();
return 1;
}



I hope this code was useful for u!!
Have a better program for ths prblm??
Feel free to comment it here..!!  :):):)





C program to Print a Non-Fibonacci series

Write a C program to generate non-fibonacci series to given Limit:

#include<stdio.h>
#include<conio.h>

main()
{
 int n,a,b,c,d,x;

 a=0;
 b=1;
 c=0;
 printf("Enter the upper range of the series:");
 scanf("%d",&n);

 while(c<=n)
 {
  c=a+b;
  a=b;
  b=c;
  d=a+b;

  for(x=c+1;x<d;x++)
  {
   if(x<=n)
    printf("%d",x);
   else
    break;
  }
 }
 getch();
}


I hope this code was useful for u!!
Have a better program for ths prblm??
Feel free to comment it here..!!  :):):)

C program to Reverse a string using recursion

Write a program to reverse any string using recursion (Maximum 15 steps)

Eg: Input   :  Bjarne Stroustrup developed C++
     Output  : ++C depoleved purtsuortS enrajB


This is one of the common question asked in technical contests and interviews.!! it checks ur knowledge about recursion and string. We recursively calls itself until encountering endl character. This fragment plays the vital role: 

{
if (current character != endl) process( next caharacter);
print(current character); 
}


My code:

#include<stdio.h>
char line[20];
void print(int i)
{
     if(line[i]!='\0')
     print(i+1);

     printf("%c",line[i]);
}
main()
{
      printf("Enter a  string..:   ");
      gets(line);
      print(0);
      getch();
}
 




I hope this code was useful for u!!
Have a better program for ths prblm??
Feel free to comment it here..!!  :):):)

Coding Contest- Matrix multiplication

PROBLEM : Write a program to multiply two 3 x 3 matrices where resulting matrix should produce
  • If cell element is even '0'
  • If cell element is odd '1'
eg: If resulting matrix of multiplication is

My code:


#include<stdio.h>
main()
{
      int a[3][3],b[3][3],c[3][3],d[3][3];
      int i,j,k;
      printf("Enter First matrix:...\n");
      for(i=0;i<3;i++)
      for(j=0;j<3;j++)
      scanf("%d",&a[i][j]);
       printf("Enter Second matrix:...\n");
      for(i=0;i<3;i++)
      for(j=0;j<3;j++)
      scanf("%d",&b[i][j]);
     
      for(i=0;i<3;i++)
      for(j=0;j<3;j++)
      {
      c[i][j]=0;
      for(k=0;k<3;k++)
      c[i][j]+=a[i][k]*b[k][j];
      }
      for(i=0;i<3;i++)
      for(j=0;j<3;j++)
      {
      if(c[i][j]%2)d[i][j]=1;
      else d[i][j]=0;
      }
      printf("\n Product is..\n");
      for(i=0;i<3;i++)
      {
      for(j=0;j<3;j++)
      printf("%d\t",c[i][j]);
      printf("\n");
      }
       printf("\n Response is..\n");
      for(i=0;i<3;i++)
      {
      for(j=0;j<3;j++)
      printf("%d\t",d[i][j]);
      printf("\n");
      }
     
      getch();
}