Wednesday, January 24, 2018

C Simple Puzzles

-------------------------------------------------------------------
How to print numbers from 1 to N without using any semicolon in C.
/ A recursive C program to print all numbers from 1
// to N without semicoolon
#include<stdio.h>
#define N 10
int main(int num)
{
   if (num <= N && printf("%d ", num) && main(num + 1))
    {
    }   
}
------------------------------------------------------------------
Find sum of two numbers without using any operator
int main()

{
printf("sum=%d",printf("%*c%*c",3,' ',4,' '));

return 0;

}
------------------------------------------------------------------
Write a one line C function to round floating point numbers
1.6
Algorithm: roundNo(num)
1. If num is positive then add 0.5.
2. Else subtract 0.5.
3. Type cast the result to int and return.

Example:
num = 1.67, (int) num + 0.5 = (int)2.17 = 2
num = -1.67, (int) num – 0.5 = -(int)2.17 = -2

Implementation:

/* Program for rounding floating point numbers */
# include<stdio.h>
int roundNo(float num)
{
    return num < 0 ? num - 0.5 : num + 0.5;
}

int main()
{
    printf("%d", roundNo(-1.777));
    getchar();
    return 0;
}
Run on IDE
Output: -2

Time complexity: O(1)
Space complexity: O(1)

Tuesday, January 23, 2018

Find Knight Propability of moves when inputing x,y positions. (Company Hakuna Matato)

/*   User input :  X and Y positions of Knight
      Out Put     :   2 steps
 */

# include <stdio.h>
# include <conio.h>
/* possible moves   total 8 moves can be possible*/
int xMove[8] = {  2, 1, -1, -2, -2, -1,  1,  2 };
int yMove[8] = {  1, 2,  2,  1, -1, -2, -2, -1 };
int prop_count(int x,int y)
{
int mcount=0;
int s;
printf("\nKnight Positions are\n");
for(s=0;s<=7;s++)
{
if( x+xMove[s]>=0 && x+xMove[s]<=7)
if( y+yMove[s]>=0 && y+yMove[s]<=7)
{
printf("\n%d,%d",x+xMove[s],y+yMove[s]);
mcount++;
}
}
return mcount;
}
main(){
int board[8][8];
int x,y;
clrscr();
printf("\nboard [0,0 is start and 7,7 is end]\nEnter X and Y position of?");
scanf("%d%d",&x,&y);
printf("\nTotal count of Moves : %d",prop_count(x,y));
}

Monday, January 22, 2018

Artificial Intelligence vs Machine Learning vs Deep Learning

First coined in 1956 by John McCarthy, AI involves machines that can perform tasks that are characteristic of human intelligence. While this is rather general, it includes things like planning, understanding language, recognizing objects and sounds, learning, and problem solving.
We can put AI in two categories, general and narrow. General AI would have all of the characteristics of human intelligence, including the capacities mentioned above. Narrow AI exhibits some facet(s) of human intelligence, and can do that facet extremely well, but is lacking in other areas. A machine that’s great at recognizing images, but nothing else, would be an example of narrow AI.
Artificial Intelligence is the broader concept of machines being able to carry out tasks in a way that we would consider “smart”.
Machine Learning is a current application of AI based around the idea that we should really just be able to give machines access to data and let them learn for themselves.
Deep learning is one of many approaches to machine learning. Other approaches include decision tree learning, inductive logic programming, clustering, reinforcement learning, and Bayesian networks, among others.
Deep learning was inspired by the structure and function of the brain, namely the interconnecting of many neurons. Artificial Neural Networks (ANNs) are algorithms that mimic the biological structure of the brain.
AI and IoT are Inextricably Intertwined
I think of the relationship between AI and IoT much like the relationship between the human brain and body.
Our bodies collect sensory input such as sight, sound, and touch. Our brains take that data and makes sense of it, turning light into recognizable objects and turning sounds into understandable speech. Our brains then make decisions, sending signals back out to the body to command movements like picking up an object or speaking.
All of the connected sensors that make up the Internet of Things are like our bodies, they provide the raw data of what’s going on in the world. Artificial intelligence is like our brain, making sense of that data and deciding what actions to perform. And the connected devices of IoT are again like our bodies, carrying out physical actions or communicating to others.
Unleashing Each Other’s Potential
The value and the promises of both AI and IoT are being realized because of the other.
Machine learning and deep learning have led to huge leaps for AI in recent years. As mentioned above, machine learning and deep learning require massive amounts of data to work, and this data is being collected by the billions of sensors that are continuing to come online in the Internet of Things. IoT makes better AI.
Improving AI will also drive adoption of the Internet of Things, creating a virtuous cycle in which both areas will accelerate drastically. That’s because AI makes IoT useful.

LG Online Code examination date : 19-1-2018

# include <stdio.H>
# include <conio.H>

/* Robot is at position 0 and move the robot to a destination x(x>0).
Robot moves in 1,2,3,4,5 steps.Find the minimum number of steps to reach
the destination.
Ex:destination x=12
output: 3 steps     */

main(){
int i,dist=13;
int steps[5]={1,2,3,4,5};
int moves;
clrscr();
for( i=sizeof(steps)/2-1 ; i>=0;i--)
{
if(dist%steps[i]==0)
{
printf("\n%d step of %d moves",steps[i],dist/steps[i]);
break;
}
else
{
moves=dist/steps[i];
dist=dist%steps[i];
if(moves!=0)
printf("\n%d step of %d moves",steps[i],moves);
}
}
}

Sunday, January 21, 2018

LG soft Online Coding question : Dt 19-12-2018

/* Assign 1 to 26 for alpphabets then multiply  each char of each string and do sum for s1 and s2 . if the result is equal print CHOOSEN other wise NOT CHOOSEN

s1="AB"  , s2="AB"
o/p CHOOSEN
 x=1*2 ->2
y=1*2->2
if x==y    print CHOOSEN other wise  "NOT CHOOSEN"
*/

# include <stdio.h>
void solution(char*S,char*T)    {
int x=1,y=1
for(i=0;S[i]!=0;i++)
{
x*=S[i]-64;
}
for(i=0;T[i]!=0;i++)
{
y*=T[i]-64;
}

if((x%47)==(y%47))
printf("CHOOSEN");
else
printf("NOT CHOOSEN");

}

main(){
char s1[50],s2[50];
solution("ABC","ABC");
}

Friday, January 19, 2018

Coding test Question : read start and end numbers and print nos whose digits are unique

/*  i/p  :   (120,130)    o/p :  120 123 124 125 126 127 128 129 130 */

#include<stdio.h>
void main()
{
 int temp,sr,er,i,a[5],j,k,l,flag=0;
 clrscr();
 scanf("(%d,%d)",&sr,&er);
 for(i=sr;i<=er;i++)
 {
  temp=i;
  j=flag=0;
  while(temp!=0)
  {
   a[j]=temp%10;
   temp=temp/10;
   j++;
  }
  for(k=0;k<j;k++)
  {
   for(l=k+1;l<j;l++)
   {
    if(a[k]==a[l])
    {
     flag=1;
     break;
    }
   }
   if(flag==1)
   {
    break;
   }
  }
  if(flag!=1)
   printf("%d ",i);
 }
}

Saturday, January 6, 2018

Coding test question : 1) reverse a word 2) Calendar Ex date : 06-01-2018

public class reverseword {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="i love java and c";
int i=0;
String[] splt=s.split(" ");
String r="";
for(i=splt.length-1;i>0;i--)
{
r=r+splt[i]+".";
}
r+=splt[i];
System.out.println("::"+r.trim());
}
Req o/p :  c.and.java.love.i 

-------------------------------------------------------------------------------------------------------------------
i/p day,month and Yr , o/p  dayWeek
-------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
    #include<conio.h>
    #include<math.h>
    int main(){
                  int dat, mont, years;
                  printf("Enter the year : ");
                  scanf("%d", &years);
                  printf("\n Enter the month : ");
                  scanf("%d", &mont);
                  printf("\n Enter the date : ");
                  scanf("%d", &dat);
                  weekday(dat, mont, years);
                  return 0;
                  }
    int weekday(int date, int month, int year) {
           int dayWeek, yr, yd;
           yr = year % 100;
           yd = year / 100;
           printf("\nThe Date Given is : %d / %d / %d \n\n", date, month, year);
           dayWeek = 1.25 * yr + findm(month, year) + date - 2 * (yd % 4);
           dayWeek = dayWeek % 7;
           switch (dayWeek){
           case 0:  printf("Day of Week of the Date is : Saturday");
                        break;
           case 1:  printf("Day of Week of the Date is : Sunday");
                        break;
           case 2:  printf("Day of Week of the Date is : Monday");
                        break;
           case 3:  printf("Day of Week of the Date is : Tuesday");
                        break;
           case 4:  printf("Day of Week of the Date is : Wednesday");
                        break;
           case 5:  printf("Day of Week of the Date is : Thursday");
                        break;
           case 6:  printf("Day of Week of the Date is : Friday");
                        break;
           default:  printf("The Given input data is wrong");
           }
           return 0;
           }
    int findm(int months, int yearss){
           int findmonth, leapyr;
           if ((yearss % 100 == 0) && (yearss % 400 != 0))
           leapyr = 0;
           else if (yearss % 4 == 0)
           leapyr = 1;
           else
           leapyr = 0;
           findmonth = 3 + (2 - leapyr) * ((months + 2) / (2 * months))
+ (5 * months + months / 9) / 2;
           findmonth = findmonth % 7;
           return findmonth;
    }

Wednesday, January 3, 2018

C Question bank for RISE-TT

1)Factorial of given number.
2)Amstrong(153) / perfect (6)/ palindrom

number test.
3) reverse a number.
4)print fibonacci numbers upto the given

range.
5)Program to find power of given number.(use

loops)
6)find duplicate numbers in an array
7)insert or delete a number in an array
8)sort array elements.
9)find the max and min value of an array
10)write a  program to convert dec to bin
11)write a program to conver binary to dec
12)read a string and find the given  string .
13)read 3x3 matrix and print diagonal sum
14)find a key value in double dim 3x3 array
15)Read a string 'i love java' and
    print 'java love i'
16) generate  reverse fibonacci series from
     the givebn range
17)accept a string and insert another string

at spec pos
18)accept 5 strings and copy all strings to

another strinmg
20)accept 5 strings and sort all strings





Inserting a string in another string at specific position

# include <stdio.h>
# include <conio.h>
# include <string.h>
main()
{
char str[50];
char istr[10];
char nstr[60];
int pos,i,j,k=0;
puts("\nEnter a long strings?");
gets(str);
puts("\nEnter  string to insert?");
gets(istr);
puts("\nEnter  pos?");
scanf("%d",&pos);
/* copy string up to pos */
for(i=0;i<pos;i++)
{
nstr[i]=str[i];
}
k=i;
/* copy istr to nstr */
for(j=0;istr[j]!=0; j++)
{
nstr[i]=istr[j];
i++;
}
/* copy rest of str to nstr */
       for(; str[k]!=0;k++,i++)
       {
       nstr[i]=str[k];
       printf("\n%c",str[k]);
       }
nstr[i]=0;
puts(nstr);
}
















Finding string in another string

# include <stdio.h>
# include <conio.h>
# include <string.h>
main(){     /* finding fstr in str */
char str[]="i love c , i love cpp  and i love java";
char fstr[]="love";
int i,j,k,L,flag=0;
clrscr();
for(L=0;str[L]!=0;L++);

for(i=0;i<L;i++)
{
for(k=0,j=i;fstr[k]!=0;k++,j++)
{
if(str[j]!=fstr[k])
break;
}
if(k==strlen(fstr))
printf("\n'%s' found at %d",fstr,i+1,flag++);
}
if(flag==0)
printf("\nNot found");
else
printf("\n'%s' Found %d times",fstr,flag);

}