Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Tyl Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

TYL- IAT- 3 Question Bank

1.An automobile company manufactures both a two wheeler (TW) and a four wheeler
(FW). A company manager wants to make the production of both types of vehicle
according to the given data below:
● 1st data, Total number of vehicle (two-wheeler + four-wheeler)=v
● 2nd data, Total number of wheels = W

The task is to find how many two-wheelers as well as four-wheelers need to


manufacture as per the given data.

Example :
Input :
200 -> Value of V
540 -> Value of W
Output :
TW =130 FW=70
Explanation:
130+70 = 200 vehicles
(70*4)+(130*2)= 540 wheels
Constraints :
● 2<=W
● W%2=0
● V<W
Print “INVALID INPUT” , if inputs did not meet the constraints.
The input format for testing :
The candidate has to write the code to accept two positive numbers separated by a new
line.
● First Input line – Accept value of V.
● Second Input line- Accept value for W.

#include<stdio.h>

int main() {
int V, W;
scanf("%d", &V);
scanf("%d", &W);

if (W % 2 != 0 || V >= W) {
printf("INVALID INPUT\n");
} else {
int TW, FW;
FW = (W - (2 * V)) / 2;
TW = V - FW;
printf("TW=%d FW=%d\n", TW, FW);
}

return 0;
}

2. C Program to generate Fibonacci Triangle


#include<stdio.h>
#include<stdlib.h>
int main(){
int a=0,b=1,i,c,n,j;
system("cls");
printf("Enter the limit:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
a=0;
b=1;
printf("%d\t",b);
for(j=1;j<i;j++)
{
c=a+b;
printf("%d\t",c);
a=b;
b=c;

}
printf("\n");
}
return 0;
}
3.C Program for Merge Sort
#include <stdio.h>
#include <stdlib.h>
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

int L[n1], R[n2];

for (i = 0; i < n1; i++)


L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int l, int r)


{
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);

merge(arr, l, m, r);
}
}

void printArray(int A[], int size)


{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
printf("Given array is \n");
printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
printf("\nSorted array is \n");
printArray(arr, arr_size);
return 0;
}
4.C Program to Add Two Complex Numbers
// C program to demonstrate
// addition of complex numbers
#include <stdio.h>

// define a structure for complex number


typedef struct complexNumber {
int real;
int img;
} complex;

// This function accepts two complex numbers


// as parameter and return addition of
// them.
complex add(complex x, complex y);

// Driver code
int main()
{
// Define three complex type numbers
complex a, b, sum;

// First complex number


a.real = 2;
a.img = 3;

// Second complex number


b.real = 4;
b.img = 5;

// Print first complex number


printf("\n a = %d + %di", a.real, a.img);

// print second complex number


printf("\n b = %d + %di", b.real, b.img);

// call add(a,b) function and


// pass complex numbers a & b
// as an parameter.
sum = add(a, b);
// Print result
printf("\n sum = %d + %di", sum.real, sum.img);

return 0;
}

// Complex add(complex x, complex y)


// function definition
complex add(complex x, complex y)
{
// Define a new complex number.
complex add;

// Add real part of a&b


add.real = x.real + y.real;

// Add Imaginary part of a&b


add.img = x.img + y.img;

return (add);
}

5.Write a C Program for the following

There are total n number of Monkeys sitting on the branches of a huge Tree. As
travelers offer Bananas and Peanuts, the Monkeys jump down the Tree. If every
Monkey can eat k Bananas and j Peanuts. If total m number of Bananas and p number
of Peanuts are offered by travelers, calculate how many Monkeys remain on the Tree
after some of them jumped down to eat.
At a time one Monkeys gets down and finishes eating and go to the other side of the
road. The Monkey who climbed down does not climb up again after eating until the
other Monkeys finish eating.
Monkey can either eat k Bananas or j Peanuts. If for last Monkey there are less than k
Bananas left on the ground or less than j Peanuts left on the ground, only that Monkey
can eat Bananas(<k) along with the Peanuts(<j).
Write code to take inputs as n, m, p, k, j and return the number of Monkeys left on the
Tree.
Where, n= Total no of Monkeys
k= Number of eatable Bananas by Single Monkey (Monkey that jumped down last
may get less than k Bananas)
j = Number of eatable Peanuts by single Monkey(Monkey that jumped down last
may get less than j Peanuts)
m = Total number of Bananas
p = Total number of Peanuts
Remember that the Monkeys always eat Bananas and Peanuts, so there is no
possibility of k and j having a value zero

#include <stdio.h>
int main ()
{
int n, k, j, m, p;
float atebanana = 0.0, atepeanut = 0.0;
scanf ("%d %d %d %d %d", &n, &k, &j, &m, &p);
if (n < 0 || k < 0 || j < 0 || m < 0 || p < 0)
{
printf ("INVALID INPUT");
}
else
{
if (k > 0)
{
atebanana = (float) (m / k);
m = m % k;
}
if (j > 0)
{
atepeanut = (float) (p / j);
p = p % j;
}
n = n - atebanana - atepeanut;
if ((m != 0) || (p != 0))
n = n - 1;
printf ("Number of Monkeys left on the Tree:%d", n);
}
return 0;
}

6.Program in C to print highest frequency character in String with count


#include <stdio.h>
#include <string.h>

int main()
{
char str[100], result;
int i, len;
int max = 0;
int freq[256] = {0};
printf("C Program to Find Maximum Occurring Character in a String \n");
printf("Please Enter a String : ");
fgets(str, 100, stdin);
len = strlen(str);
for(i = 0; i < len; i++)
{
freq[str[i]]++;
}
for(i = 0; i < len; i++)
{
if(max <= freq[str[i]])
{
max = freq[str[i]];
result = str[i];
}
}
printf("\n Maximum Occurring Character in a String %s is '%c' %d times", str, result, max);
return 0;
}

7. C program to check if strings are anagrams or not


Two strings will be anagrams of each other if and only if they contain the same number of
characters; the order of the characters doesn't matter. That is, if two strings are anagrams of
each other, then one string can be rearranged to form the other string.
int main()
{
char str1[20], str2[20];
int len, len1, len2, i, j, found=0, not_found=0;
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
len1 = strlen(str1);
len2 = strlen(str2);
if(len1 == len2)
{
len = len1;
for(i=0; i<len; i++)
{
found = 0;
for(j=0; j<len; j++)
{
if(str1[i] == str2[j])
{
found = 1;
break;
}
}
if(found == 0)
{
not_found = 1;
break;
}
}
if(not_found == 1)
printf("\nStrings are not Anagram");
else
printf("\nStrings are Anagram");
}
else
printf("\nBoth string must contain same number of character to be an Anagram Strings");
return 0;
}

8.C program to perform right rotation in array by 2 positions


#include <stdio.h>

int main()
{
//Initialize array
int arr[] = {1, 2, 3, 4, 5};
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
//n determine the number of times an array should be rotated
int n = 3;

//Displays original array


printf("Original array: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}

//Rotate the given array by n times toward right


for(int i = 0; i < n; i++){
int j, last;
//Stores the last element of the array
last = arr[length-1];

for(j = length-1; j > 0; j--){


//Shift element of array by one
arr[j] = arr[j-1];
}
//Last element of array will be added to the start of array.
arr[0] = last;
}

printf("\n");

//Displays resulting array after rotation


printf("Array after right rotation: \n");
for(int i = 0; i< length; i++){
printf("%d ", arr[i]);
}
return 0;
}

9.Program to convert time from 12 hour to 24 hour format


// C++ program to convert 12 hour to 24 hour
// format
#include <stdio.h>

int main() {
// declare variables
int h, m, am_pm, h1, m1;

// take input
printf("Enter the time in 12-hour format (hh:mm am/pm): ");
scanf("%d:%d %c", & h, & m, & am_pm);

// minute value will remain same


m1 = m;

// copy hour value


h1 = h;

// Adjust the hour


if (am_pm == 'a' || am_pm == 'A') { // if it is am / AM
// if hour = 12, then it is 0 in 24 hour format
if (h1 == 12) {
h1 = 0;
}
// display result
printf("%02d:%02d a.m is same as %02d:%02d in 24-hour format\n", h, m, h1, m1);
} else if (am_pm == 'p' || am_pm == 'P') { // if it is pm / PM
// if hour not equals 12, then add 12 in 24 hour format
if (h1 != 12) {
h1 += 12;
}
// display result
printf("%02d:%02d p.m is same as %02d:%02d in 24-hour format\n", h, m, h1, m1);
}

return 0;
}

10. C program to compare if the two matrices are equal or not


#include <stdio.h>
#include <conio.h>
main(){
int A[10][10], B[10][10];
int i, j, R1, C1, R2, C2, flag =1;
printf("Enter the order of the matrix A");
scanf("%d%d", &R1, &C1);
printf("Enter the order of the matrix B");
scanf("%d%d", &R2,&C2);
printf("Enter the elements of matrix A");
for(i=0; i<R1; i++){
for(j=0; j<C1; j++){
scanf("%d",&A[i][j]);
}
}
printf("Enter the elements of matrix B");
for(i=0; i<R2; i++){
for(j=0; j<C2; j++){
scanf("%d",&B[i][j]);
}
}
printf("MATRIX A is ");
for(i=0; i<R1; i++){
for(j=0; j<C1; j++){
printf("%3d",A[i][j]);
}
printf(" ");
}
printf("MATRIX B is ");
for(i=0; i<R2; i++){
for(j=0; j<C2; j++){
printf("%3d",B[i][j]);
}
printf(" ");
}
/* Comparing two matrices for equality */
if(R1 == R2 && C1 == C2){
printf("Matrices can be compared");
for(i=0; i<R1; i++){
for(j=0; j<C2; j++){
if(A[i][j] != B[i][j]){
flag = 0;
break;
}
}
}
}
else{
printf(" Cannot be compared");
exit(1);
}
if(flag == 1 )
printf("Two matrices are equal");
else
printf("But,two matrices are not equal");
}

You might also like