Technical C

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

From Logik Works to Everyone: 01:40 PM

#include<stdio.h>
int main(){
int n,x,y,z,c=0,k;
scanf("%d",&n);
for(k=0;k<n;k++){
scanf("%d %d %d",&x,&y,&z);
if(x+y+z>1)c++;
}
printf("%d\n",c);
}
From Logik Works to Everyone: 01:52 PM
3. A programmer is given with a string and he is asked to check the ,Availability
of smallest chunk of the character in the string .If the smallest chunk of
character is available ,the programmer will return the index of the chunk , in
other cases he will return the unavailable index .
Input Format :
The first line consists of the input string.
The second line consists of required smallest chunk.
Output Format :
If the smallest chunk is found, print its index. Else print ‘Character not found’.
Input1 :
INTEGRALS
S
Output1 :
8
Input2 :
XENOPHOBIC
%
Output2 :
Character not found
Input3 :
MAXI
I
Output3 :
3
From Logik Works to Everyone: 02:05 PM
#include<stdio.h>
#include<string.h>
int findLastIndex(char *str, char x)
{
int index = -1;
for (int i = 0; str[i]!='\0'; i++)
if (str[i] == x)
index = i;
return index;
}
int main()
{
char str[100];
char x ;
scanf("%s",str);
scanf(" %c",&x);
int index = findLastIndex(str, x);
if (index == -1)
printf("character not found");
else
printf("%d",index);
return 0;
}
From 19BF5A0408 to Everyone: 02:40 PM
No need time sir
Explain it
From Logik Works to Everyone: 02:41 PM
Input1 :
3+2+1
Ouput1 :
1+2+3
6. Rashmi the mathematician is a third year student at IIT. She is now learning the
addition operation. The teacher has written down the sum of multiple numbers.
Pupils should calculate the sum. To make the calculation easier, the sum only
contains numbers 1, 2 and 3. Still, that isn't enough for Rashmi. She is only
beginning to count, so she can calculate a sum only if the summands follow in non-
decreasing order. For example, she can't calculate sum 1+3+2+1 but she can
calculate sums 1+1+2 and 3+3.
You've got the sum that was written on the board. Rearrange the sum and and print
the sum in such a way that Rashmi can calculate the sum.
Input Format :
The first line contains a non-empty string s — the sum Rashmi needs to count.
String s contains no spaces. It only contains digits and characters "+". Besides,
string s is a correct sum of numbers 1, 2 and 3. String s is at most 100 characters
long.
Output Format :
Print the new sum that Rashmi can count.
Input2 :
1+1+3+1+3
Ouput2 :
1+1+1+3+3
From Logik Works to Everyone: 02:41 PM
Input3 :
2
Ouput3 :
2
From 18BF1A0463 to Everyone: 03:00 PM
what is the use of first for loop sir
From Logik Works to Everyone: 03:00 PM
#include <stdio.h>
int main(){
char a[100],temp;
scanf("%s",a);
int i,j,n=strlen(a);
for(i=0;i<n;i++){

for(j=0;j<n-2;j++)
{
if(a[j]>a[j+2])
{
if(a[j]=='+')
continue;
temp=a[j];
a[j]=a[j+2];
a[j+2]=temp;
}
}
}

for(i=0;i<n;i++){
printf("%c",a[i]);
}
}
From 18BF1A0463 to Everyone: 03:14 PM
#include<stdio.h>
int main()
{
int size;
scanf("%d",&size);
int arr[size];
int evensum=0,oddsum=0;
for(int i=0;i<size;i++){
scanf("%d",&arr[i])
if(arr[i]%2==0){
evensum= evensum+arr[i];
}
else {
oddsum = arr[i]+oddsum;
}
}
if(evensum>oddsum)
printf("EVEN");
else if (oddsum>evensum)
printf("ODD");
else
printf("TIED");
return 0;
}
From Logik Works to Everyone: 03:26 PM
#include<stdio.h>
int main()
{
int a[100],n,i,oddsum=0,evensum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
evensum=evensum+a[i];
}
else if(a[i]%2!=0)
{
oddsum=oddsum+a[i];
}
}
if(evensum>oddsum)
printf("even");

else if(oddsum>evensum)
printf("odd");

else
printf("tied");
return 0;
}
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
int main()
{
int p=0;
char s[100] ;
scanf("%s",s);
for (int i = 0; i < strlen(s) - 2; i++)
{
if (s[i] != s[i + 2]) {
p=1;
}
}
if(p==0)
printf ("Yes");
else
printf("No");
}
From Logik Works to Everyone: 03:36 PM
6. There are two students who are in initial stage of writing words, so as a
programmer you are requested to check the word written by them if the word
contains alternate characters we need to return yes otherwise no ?
Input Format :
The first line contains a character array.
The character array consists of characters from the complete character set.
Output Format :
In the given string if the characters are repeated then print Yes else
print No.
Constraints:
1<=len1<=1000
Input1 :
ABABAB
Output1 :
YES
Input2 :
XYZ
Output2
NO:
From Logik Works to Everyone: 03:54 PM
#include<stdio.h>
#include<string.h>
int main()
{
char str[100],text[100];
int i=0,j=0;
gets(str);
i=strlen(str);
while(i>0){
text[j]=str[--i];
++j;
}
text[j]='\0';
for(i=0;text[i]!='\0';i++)
{
if(text[i+1]==' ' || text[i+1]==NULL)
{
for(j=i;j>=0 && text[j]!=' ';j--)
printf("%c",text[j]);
printf(" ");
}

}
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------
From Logik Works to Everyone: 09:40 AM
Solution 1:
#include<stdio.h>
#include<stdio.h>
int main()
{
char str[100];
int i,length;
int flag=0;
scanf("%s",str);
length=strlen(str);
for(i=0;i<length;i++){
if(str[i]!=str[length-i-1])
{
flag=1;
break;
}
}
if(flag)
printf("0");
else
{
printf("1");}
return 0;
}
From Logik Works to Everyone: 09:50 AM
Solution 1:
#include<stdio.h>
#include<stdio.h>
int main()
{
char str[100];
int i,length;
int flag=0;
scanf("%s",str);
length=strlen(str);
for(i=0;i<length;i++){
if(str[i]!=str[length-i-1])
{
flag=1;
break;
}
}
if(flag)
printf("0");
else
{
printf("1");}
return 0;
}
8. Sandhya is a great programmer. She works in multinational company. She has been
working for past 2.5 years and is expecting a hike in salary. So, her manager gave
her a task and a stipulation that if she completes the code within 1 day, She will
get hike in salary. Help Sandhya in the task to get salary hike.
[HINT- Sandhya is good with strings and reversal techniques]
Input Format :
The input contains a string.
Output Format :
Print 1 if the string is a palindrome, else print 0.
Input1 :
MADAM
Output1 :
1
Input2 :
BANANA
Output2 :
0
----------------2nd method-----------
#include<stdio.h>
#include<string.h>
int main()
{
char a[100],b[100];
gets(a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
{
printf("1");
}
else
{
printf("0");
}
return 0;
}
-----------------------------------------------------------------------------------
-------------------
7.Priyanka is strong lady and she is running a training academy,inorder to conspire
priyanka there are some spy agents working to take her students ,she came to know
this background conspiracy so she encrypted the student names with some identities
and each student will get unique identities so can you help her to say
Whether this identies will be accessed by third parties or not?
Input Format:
The only line of the input contains number of test cases the name string s
consisting alphabets
Output Format:
Print unique id for given string
Constraints:
(1<=|s|<=100000)
Input:
2
Maxi
Arif
Output:
399
386
Input:
1
shubham
Output:
744
Input:
1
Abi
Output:
268
-----------------------------
#include<stdio.h>
int main(){
int sum;
int t,i;
scanf("%d",&t);
while(t--){
char a[100];
scanf("%s",a);
sum=0;
for(i=0;a[i];i++){
sum=sum+a[i];
}
printf("%d\n",sum);
}
}
-----------------------------------------------------------------------------------
--------------------------------
Input2 :
Anis works at a programmer in MNC, Anis projects works on processing non vowel
data. So the inputs given to
#include <stdio.h>
#include <string.h>

int main()
{
char s[100], t[100];
int i, j;
scanf("%[^\n]s",s);

for(i = 0; s[i] != '\0'; i++)


{
for(j=0;s[j]!='\0';j++)
{

if(s[j]=='A' || s[j]=='E' || s[j]=='I' || s[j]=='O' || s[j]=='U' ||


s[j]=='a' || s[j]=='e' || s[j]=='i' || s[j]=='o' || s[j]=='u')
{
for(int k=j;s[k]!='\0';k++)
{
s[k]=s[k+1];
}
}
}
}
printf("%s",s);
return 0;
}
-----------------------------------------------------------------------------------
------------------------------------------------------------------
Given a string  sentence which consists of both uppercase & lowercase alphabets
and words are separated by empty space characters. Write a program to return the
length of the last word in the string. If string is empty return “-1” 
Input Format :
Input consists of a string.
Output Format :
Input1 :
India is my Country
Output1 :
7
Input2 :
Siddu is passionate about his work
Output2 :
4
#include<stdio.h>
int main(){
char a[100];
scanf("%[^\n]s",a);
int i,c=0,j;
for(i=0;a[i];i++);
for(j=i-1;j>=0;j--){
if(a[j]!=' '){
c++;}
else
break;
}
printf("%d",c);
}
-----------------------------------------------------------------------------------
------------------------------------------
10. Harish is designing a
Input 1:
Input 2:
Input 3:
#include <stdio.h>
#include <string.h>

int main()
{
char str[100];
int i, j, k;
gets(str);
for(i = 0; i < strlen(str); i++)
{
for(j = i + 1; str[j] != '\0'; j++)
{
if(str[j] == str[i])
{
for(k = j; str[k] != '\0'; k++)
{
str[k] = str[k + 1];
}
}
}
}

printf("%s", str);
return 0;
}
-----------------------------------------------------------------------------------
-------------------------------
Raju is working on a project
#include <stdio.h>
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
if(i==j && A[i][j]!=1)
{
isIdentity = 0;
}
else if(i!=j && A[i][j]!=0)
{
isIdentity = 0;
}
}
}
if(isIdentity == 1)
{
printf("\nThe given matrix is an Identity Matrix.\n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("%d ", A[i][j]);
}
printf("\n");
}
}
else
{
printf("The given matrix is not Identity Matrix");
}
return 0;
}
-----------------------------------------------------------------------------------
---------------------
#include<stdio.h>
void swap(int *, int *);
void selection_sort(int [],int );

int main()
{
int n,i;
int a[10];
scanf("%d", &n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
selection_sort(a, n);
for(i=0;i<n;printf("%d ",a[i]),i++);
return 0;
}

void swap(int *n1, int *n2)


{
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
//printf("%d %d\n", *n1,*n2);
}

void selection_sort(int arr[], int size)


{
int i,j, min;
for(i=0; i<size-1; i++)
{
min = i;
for(j=i+1; j<size;j++)
{
if(arr[j]<arr[min])
{
min = j;
}
}
swap(&arr[min], &arr[i]);
}
}
-----------------------------------------------------------------------------------
----------------
#include<stdio.h>

void insertion_sort(int [],int );

int main()
{
int n,i;
int a[10];
scanf("%d", &n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
insertion_sort(a,n);
for(i=0;i<n;printf("%d ",a[i]),i++);
return 0;
}

void insertion_sort(int arr[], int size)


{
int i, j, key;
for(i=1;i<size;i++)
{
key = arr[i];
j = i-1;

while(j>=0 && arr[j]>key)


{
arr[j+1]=arr[j];
j--;
}
arr[j+1] = key;
}
}
-----------------------------------------------------------------------------------
------------------------------------------------------------------------------
#include <stdio.h>

int binarySearch(int arr[], int l, int r, int x)


{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;

if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);

return binarySearch(arr, mid + 1, r, x);


}
return -1;
}

int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = 5;
int key = 80;
int result = binarySearch(arr, 0, n , key);
(result == -1) ? printf("Element is not present in array"): printf("Element is
present at index %d",result);
return 0;
}
--------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
#define MAX 20

void merge_sort(int [],int,int);


void merge(int[],int,int,int);
void main()
{
int i,n;
int a[MAX];
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
merge_sort(a,0,n-1);
printf("\n sorted array is");
for(i=0; i<n; i++)
{
printf("%d ",a[i]);
}
}
void merge_sort(int a[],int b,int e)
{
int m;
if(b<e)
{
m=(b+e)/2;
merge_sort(a,b,m);
merge_sort(a,m+1,e);
merge(a,b,m,e);
}
}
void merge(int a[],int b,int m, int e)
{
int i=b;
int j=m+1;
int temp[MAX];
int index=b;
int k;
while((i<=m)&&(j<=e))
{
if(a[i]<a[j])
{
temp[index]=a[i];
i++;
}
else
{
temp[index]=a[j];
j++;
}
index++;
}
if(i<=m)
{
while(i<=m)
{
temp[index]=a[i];
i++;
index++;
}
}
else if
-----------------------------------------------------------------------------------
--
#include<stdio.h>
#define max 10

int partition(int [],int ,int );


void quick_sort(int [],int ,int );
void swap(int *,int *);

void main()
{
int a[max],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
printf("\n\tSorted array is:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

void quick_sort(int a[],int l,int u)


{
int t;
if(l<u)
{
t=partition(a,l,u);
quick_sort(a,l,t-1);
quick_sort(a,t+1,u);
}
}

int partition(int a[],int b,int e)


{
int i=0;
int pi=0;
int pivot=a[e];
for(i=0;i<e;i++)
{
if(a[i]<pivot)
{
swap(&a[i],&a[pi]);
pi++;
}
}
swap(&a[pi],&a[e]);
return pi;
}

void swap(int *x,int *y)


{
int t;
t=*x;
*x=*y;
*y=t;
}
---------------------------------------------------------------------------
PREFIX

You might also like