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

Saturday, 26 August 2017

TRAIN RESERVATION

#include<stdio.h>
struct train
{
    int seats[100];
    char t_name[30];
    char dept_time[5];
    char source[30];
    char dest[30];
    char t_id[5];
}t[4];
void book(char nam[],char id[],char dt[],char src[],char dst[],int *s,int j)
{
    int ch,nb,i,n,k=1;
    while(k=1)
    {
    printf("Enter ur choice:\n1. Availability checking\n 2. Booking\n
3. Cancellation\n");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1:
        {
            printf("1.Train Name:%s\n 2.Train id:%s\n
 3.Train Departure time:%s\n 4.Source:%s\n 5.Destination:%s\n
6.Availabe Seats:%d\n",nam,id,dt,src,dst,*s);

            for(i=0;i<*s;i++)
            {
                if(t[j].seats[i]==0)
                {
                    printf("%d   ",i);
                }
            }
            break;
        }
        case 2:
        {
            printf("\nEnter the number of seats");
            scanf("%d",&nb);   
            printf("\nEnter the seat numbers");
            for(i=0;i<nb;i++)
            {
                scanf("%d",&n);
                t[j].seats[n]=1;
            }
        break;
        }   
        case 3:
        {
            printf("\nEnter the number of seats to be cancelled");
            scanf("%d",&nb);
            printf("\nEnter the seat numbers");
            for(i=0;i<nb;i++)
            {
                scanf("%d",&n);
                t[j].seats[n]=0;
            }
            break;
        }
    }
    printf("\nEnter 1 to continue");
    scanf("%d",&k);
    }
}
main()
{
    int i,j,st[4];
    char nm[30];
    for(j=0;j<4;j++)
    {
        for(i=0;i<100;i++)
        {
            t[j].seats[i]=0;
        }
    }   
    for(i=0;i<4;i++)
    {
        scanf("%s %s %s %s %s %d",t[i].t_name,t[i].t_id,t[i]
.dept_time,
t[i].source,t[i].
dest,&st[i]);
    }
    for(i=0;i<4;i++)
    {
        printf("%s\n %s\n %s\n %s\n %s\n %d\n",t[i].t_name,t[i].t_id,t[
i].dept_time,t[i].source,t[i].dest,st[i]);
    }
    printf("\nChoose the train");
    scanf("%s",nm);
    for(i=0;i<4;i++)
    {
        if(strcmp(nm,t[i].t_name)==0)
        {
                book(t[i].t_name,t[i].t_id,t[
i].dept_time,t[i].source,t[i].dest,
&st[i],i);
                break;
        }
    }
}

Given an integer, check whether it is a palindrome or not.

Input:
The first line of input contains an integer T denoting
the number of test cases. Then T test cases follow. 
Each test case consists of a single line. The first line
of each test case contains a single integer N to be 
checked for palindrome.

Output:
Print "Yes" or "No" (without quotes) depending on

whether the number is palindrome or not.

Constraints:
1 <= T <= 1000
1 <= N <= 100
00

Example:

Input:
3
6
167

55555

Output:
Yes
No

Yes


#include <stdio.h>
int main()
{
    int t;
    int flag;
    scanf("%d",&t);
    while(t--)
    {
        int n,remainder,originalInteger,reversedInteger=0;
        scanf("%d",&n);
        originalInteger = n;
        while( n!=0 )
        {
            remainder = n%10;
            reversedInteger = reversedInteger*10 + remainder;
            n /= 10;
        }
        if(originalInteger==reversedInteger)
            flag=1;
        else
            flag=0;
    if(flag==1)
        printf("Yes\n");
    else if(flag==0)
        printf("No\n");
    }
    return 0;
}

STUDENT RECORD

A file contains data as follows( Student name, marks in
3 subjects)
Shrikanth 20 50 60
Kiran 30 80 90
Find the student who has maximum average score.

Input:
The first line of input contains an integer T denotingthe number of test cases.
The first line of each test case is N,N is the number
of student.
The second line of each test case contains N input
Student name and marks in 3 subject.

Output:
Print the student who has maximum average scoreand maximum average score(in int).

Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 15
1 ≤ s ≤ 10
1 ≤ marks ≤ 100

Example:

Input:
2
2
Shrikanth 20 30 10 Ram 100 50 10
3
Adam 50 10 40 Suresh 22 1 56 Rocky 100 90 10

Output:
Ram 53

#include <stdio.h>
int main() {
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,max=0,i,a,b,c,m,u=0;
        scanf("%d",&n);
        char s[n][100];
       for(i=0;i<n;i++)
         {
                scanf("%s",s[i]);
                scanf("%d%d%d",&a,&b,&c);
                m=(a+b+c)/3;
                if(m>max)
                {
                    max=m;
                    u=i;
                }
        }
        printf("%s %d\n",s[u],max);
    }
    return 0;
}

Remove all characters from an alphanumeric string.

Input:
The first line of the input contains T denoting the 

number of testcases. First line of each test case will 
be an alphanumeric string.

Output:
 For each test case output will be a numeric string

 after removing all the characters.

Constraints:
1 <= T <= 30
1 <= size of string <= 100

Example:

Input:
1
AA123BB4

Output:
1234


#include <iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        char a[100];
        int i;
        cin>>a;
        for(i=0;a[i]!='\0';i++)
        {
            if(a[i]>='0'&&a[i]<='9')
                cout<<a[i];
        }
        cout<<"\n";
    }
    return 0;
}

Friday, 25 August 2017

Print numbers from 1 to n without the help of loops.

Input: 
The first line of the input contains T denoting the
number of testcases. First line of test case containnumber n.

Output: 
Numbers from 1 to n will be printed.

Constraints:
1 <=T<= 100
1 <=N<= 990

Example:
Input:1
10
Output:1 2 3 4 5 6 7 8 9 10
#include <stdio.h>
void print(int p)
{
     if(p>0)
     {
          print(p-1);                   
          printf("%d ", p);
     }
     return;
}
int main()
{
     int t,n;
     scanf("%d",&t);
     while(t--)
     {
     scanf("%d",&n);                 
     print(n);
     printf("\n");
     }
     return 0;
}

Given an unsorted array, find the minimum difference between any pair in given array.

Input:
The first line of input contains an integer T denoting
the number of test cases.
The first line of each test case is N, the size of array.

Second line of the test case is the Array.

Output:

Print the minimum difference between any two pairs.

Constraints:

1 <= T <= 30
1 < N <= 100
1 <= arr[i] <= 100000

Example:
Input:

2
5
2 4 5 7 9
10
87 32 99 75 56 43 21 10 68 49


Output:
1
6


#include <stdio.h>
int main()
{
    int t,size,i,j,a[100],temp,k;
    scanf("%d",&t);
    while(t--)
    {
            int min=9999;
        scanf("%d",&size);
        for(i=0;i<size;i++)
        {
            scanf("%d",&a[i]);
        }
        for(i=0;i<size;i++)
        {
            for(j=i+1;j<size;j++)
            {
              k=abs(a[i]-a[j]); 
              if(min>=k)
               min=k;
            }
        }
        printf("%d\n",min);
       // printf("\n");
    }
    return 0;

Given an array A[] of n numbers and another number x, determine whether or not there exist two elements in A whose sum is exactly x.

Input:The first line of input contains an integer T denotingthe number of test cases.
The first line of each test case is N and X,N is the 
size of array.
The second line of each test case contains N integers representing array elements C[i].

Output:
Print "Yes" if there exist two elements in A whose sum is exactly x, else "No" without quotes.

Constraints:1 ≤ T ≤ 100
1 ≤ N ≤ 200
1 ≤ C[i] ≤ 1000

Example:
 
Input:
2
6 16
1 4 45 6 10 8
5 10
1 2 4 3 6 
Output:
Yes
 
Yes
                                         C-Solution
 
#include <stdio.h>

int main()
{
    int test;
    scanf("%d",&test);
    while(test--)
    {
        int n,x,i,j,flag=0;
        scanf("%d %d",&n,&x);
        int a[n];
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(i=0;i<n;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(a[i]+a[j]==x)
                    flag=1;
            }
        }
        if(flag==1)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

Thursday, 24 August 2017

REVERSING ONLY CHARACTERS OF STRING

Given a string, that contains special character together with alphabets (‘a’ to ‘z’ and ‘A’to ‘Z’),
reverse the string in a way that special characters are not affected.

E.g.:
Input:
str = "a,b$c"

Output:
str = "c,b$a"

Note that $ and, are not moved anywhere.
Only sub-sequence "abc" is reversed

Input:
str = "Ab,c,de!$"

Output:
str = "ed,c,bA!$"
 
CODE: 

#include<stdio.h>
#include<string.h>
void main()
{
    char a[50],temp[50],r[50];
    scanf("%s",a);
    int i,j,k=0,l;
    l=strlen(a);
    for(i=0;a[i]!='\0';i++)
    {
        if(a[i]>=65&&a[i]<=90||a[i]>=97&&a[i]<=122)
            {
                temp[k++]=a[i];
            }
    }
    j=0;
    for(i=k-1;i>=0;i--)
    {
        r[j++]=temp[i];   
    }   
    j=0;
    for(i=0;a[i]!='\0';i++)
    {
        if(a[i]>=65&&a[i]<=90||a[i]>=97&&a[i]<=122)
            {
                a[i]=r[j++];
            }
    }
    for(i=0;i<l;i++)
    {
        printf("%c",a[i]);
    }
}

FIND INDEX OF SUBSTRING IN A STRING

Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function 
search(char pat[], char txt[]) that prints all occurrences of pat[] 
in txt[]. You may assume that n &gt; m.
E.g.:

1)
Input:
txt[] = "THIS IS A TEST TEXT"
pat[] = "TEST"

Output:
Pattern found at index 10

2)
Input:

txt[] = "AABAACAADAABAAABAA"
pat[] = "AABA"

Output:
Pattern found at index 0

#include<stdio.h>
#include<string.h>
void main()
{
    char original[30];
    char ori[30];
    int  count=0,count1=0,i,j;
    scanf("%[^\n]s",original);
    scanf("%s",ori);
    for(i=0;i<strlen(original);)
    {
        j=0;
        count=0;
        while(original[i]==ori[j])
        {
            count++;
            i++;
            j++;
        }
        int l2=strlen(ori);
        if(count==l2)
        {
            printf("Position %d",i-l2);
             break;
        }
        else
            i++;           
    }       
}

 OR

2)

 #include <stdio.h>
#include<string.h>
int main(void)
{
    char str1[100],str2[100],*ret;
    scanf("%[^\n]s",str1);
    scanf("%s",str2);
    ret=strstr(str1,str2);
    if(ret)
        printf("%d",ret-str1);
    else
        printf("Not found");
    return 0;
}

Given two string s1 and s2 how will you check if s1 is a rotated version of s2?

If s1 = “crazyforcode” then the following are some of its rotated versions:
“forcodecrazy”
“codecrazyfor”

Input Format:
Two strings S1 and S2.
Length(S1), Length(S2) > 1

Output Format:
Print "YES" or "NO" without double quotes.

Input:
crazyforcode
codeforcrazy

Output:
NO


#include<stdio.h>
#include<string.h>
void main()
{
    char str1[50];
    char str2[50];
    char temp[50];
    char *ptr;
    scanf("%s",str1);
    scanf("%s",str2);
    int len1,len2;
    len1=strlen(str1);
    len2=strlen(str2);
    if(len1!=len2)
        printf("No");
    temp[0]='\0';
    strcat(temp,str1);
    strcat(temp,str1);
    ptr=strstr(temp,str2);
    if(ptr!=NULL)
        printf("\nYes");
    else
        printf("\nNo");
}

Given an array, find the number of subarrays whose sum is even.

Input : arr[] = {1, 2, 2, 3, 4, 1}
Output : 9

There are possible subarrays with even sum. The subarrays are
1) {1, 2, 2, 3} Sum = 8
2) {1, 2, 2, 3, 4} Sum = 12
3) {2} Sum = 2 (At index 1)
4) {2, 2} Sum = 4
5) {2, 2, 3, 4, 1} Sum = 12
6) {2} Sum = 2 (At index 2)
7) {2, 3, 4, 1} Sum = 10
8) {3, 4, 1} Sum = 8
9) {4} Sum = 4 
 
                                     C-Solution 


#include<stdio.h>
void main()
{
    int num,i,j,count=0,sum=0;
    scanf("%d",&num);
    int a[num];
    for(i=0;i<num;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<num;i++)
    {
        sum=0;
        for(j=i;j<num;j++)
        {


            sum=sum+a[j];
            if(sum%2==0)
                count++;
        }
    }
    printf("%d",count);
}

MIDDLE ELEMENT OF LINKED LIST

int getMiddle(struct Node *head)
{
    if(head==NULL)
        return -1;
    struct Node *fp,*sp;
    fp=head;
    sp=head;
    while(fp&&fp->next)
    {
        sp=sp->next;
        fp=fp->next->next;
    }
    return sp->data;
}

Given an array of distinct elements, rearrange the elements of array in zig-zag fashion in O(n) time.

The converted array should be in form       a < b > c < d > e < f.
 
Example: 
Input: arr[] = {4, 3, 7, 8, 6, 2, 1}
Output: arr[] = {3, 7, 4, 8, 2, 6, 1}

Input: arr[] = {1, 4, 3, 2}
Output: arr[] = {1, 4, 2, 3}


#include<stdio.h>
void zigzag(int[],int);
void main()
{
    int num,i;
    scanf("%d",&num);
    int a[num];
    for(i=0;i<num;i++)
        scanf("%d",&a[i]);
    zigzag(a,num);
    for(i=0;i<num;i++)
        printf("%d ",a[i]);
}
void zigzag(int a[],int num)
{
    int i,flag=1,temp;
    for(i=0;i<=num-2;i++)
    {
        if(flag)
        {
            if(a[i]>a[i+1])
            {
                temp=a[i];
                a[i]=a[i+1];
                a[i+1]=temp;

            }
        }
        else
        {
            if(a[i]<a[i+1])
            {
                temp=a[i];
                a[i]=a[i+1];
                a[i+1]=temp;
            }
        }
        flag=!flag;
    }
}

FIND IF ARRAY IS SUBARRAY OF ANOTHER

Given two arrays: arr1[0..m-1] and arr2[0..n-1]. Find whether arr2[] is a subset of arr1[] or not. Both the arrays are not in sorted order. It may be assumed that elements in both array are distinct.

Examples:
Input: arr1[] = {11, 1, 13, 21, 3, 7}, arr2[] = {11, 3, 7, 1}
Output: arr2[] is a subset of arr1[]

Input: arr1[] = {1, 2, 3, 4, 5, 6}, arr2[] = {1, 2, 4}
Output: arr2[] is a subset of arr1[]

Input: arr1[] = {10, 5, 2, 23, 19}, arr2[] = {19, 5, 3}
Output: arr2[] is not a subset of arr1[]


#include<stdio.h>
int issubarray(int a[],int b[],int m,int n)
{

int i,j;
int flag;
for(i=0;i<n;i++)
    {
        flag=0;
        for(j=0;j<m;j++)
        {
            if(b[i]==a[j])
            {
                flag=1;
            }
        }
        if(flag!=1)
        break;
       
    }
    if(flag==1)
        return 1;
    else
        return 0;
}
void main()
{
    int i,j,m,n;
    scanf("%d",&m);
    int a[m];
    for(i=0;i<m;i++)
        scanf("%d",&a[i]);
    scanf("%d",&n);
    int b[n];
    for(j=0;j<n;j++)
        scanf("%d",&b[j]);
    int x=issubarray(a,b,m,n);
    if(x)
        printf("Array 2 is a subset of Array1");
    else
        printf("Array 2 is not a subset of Array1");
}

Wednesday, 23 August 2017

FIND IF A STRING CAN BE MADE A PALINDROME

#include <stdio.h>
int main(void)
{
    char a[100];
    int hash[26],count=0,flag=0;
    scanf("%s",a);
    int i,len;
    len=strlen(a);
    for(i=0;i<26;i++)
        hash[i]=0;
    for(i=0;i<len;i++)
        hash[a[i]%97]++;
    if(len%2==0)
    {
        for(i=0;i<len;i++)
        {
            if(hash[i]>0)
            {
                if(hash[i]%2!=0)
                {
                    flag=1;
                    break;
                }
            }
        }
        if(flag==0)
            printf("Yes");
        else
            printf("No");
    }
    else
    {
        for(i=0;i<len;i++)
        {
            if(hash[i]>0)
            {
                if(hash[i]%2!=0)
                    count++;
                if(hash[i]%2==0)
                    flag=1;
            }
        }
        if(count==1&&flag==1)
            printf("Yes");
        else
            printf("No");
    }
    return 0;
}

BINARY SEARCH

#include <stdio.h>
int main(void)
{
 int num;
 scanf("%d",&num);
 int a[num];
 int key,i,flag=0,temp;
 for(i=0;i<num;i++)
  scanf("%d",&a[i]);
 scanf("%d",&key);
 int mid=num/2;
 if(a[mid]==key)
 {
  printf("Element found at index %d",mid+1);
  return 0;
 }
 else if(key<a[mid])
 {
for(i=0;i<mid;i++)
{
 if(a[i]==key)
 {
  flag=1;
  temp=i;
 }
}
}
else if(key>a[mid])
{
 for(i=mid+1;i<num;i++)
 {
  if(a[i]==key)
  {
   flag=2;
   temp=i;
  }
 }
}
 if(flag==1)
  printf("Element found at index %d",temp+1);
 else if(flag==2)
  printf("Element found at index %d",temp+1);
 else
  printf("Element not found");
 return 0;
}

REMOVE CONSECUTIVE WORDS IN A SENTENCE

#include <stdio.h>
#include<string.h>
int main(void)
{
    char str[1000],words[100][1000],temp[1000];
    int k=0,index=0,i;
    scanf("%[^\n]s",str);
    for(i=0;str[i];i++)
    {
        if(str[i]!=' ')
        {
            temp[k++]=str[i];
        }
        else
        {
            temp[k]='\0';
            strcpy(words[index],temp);
            index++;
            k=0;
        }
    }
    temp[k]='\0';
    strcpy(words[index],temp);
    for(i=0;i<=index-1;i++)
    {
        if(strcasecmp(words[i],words[i+1])!=0)
            printf("%s ",words[i]);
    }
    printf("%s",words[index]);
return 0;
}

CREATING AN ARRAY LIST TO STORE ONLY STRINGS IN JAVA


/*Program to Create an ArrayList which will be able to store only Strings */

package ListPackage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Eg2 {
public void printAll() /* Function to print the data */
{
Scanner ip = new Scanner(System.in);
ArrayList<String> al = new ArrayList<String>(); /* Creating an ArrayList */
System.out.println("Enter the number of datas to be entered:\n");
int num = ip.nextInt();
for (int i = 0; i < num; i++) {
String s = ip.next();
al.add(s);
}
System.out.println("\nThe entered strings are:\n");
Iterator<String> i = al.iterator();

 /* Iterator to iterate & display the array list contents */
while (i.hasNext()) {
System.out.println(i.next());
}
}
public static void main(String a[]) {
Eg2 obj = new Eg2();/* Instantiating the class */
obj.printAll();
}

}