Friday, 23 August 2019

Find Product Hacker Earth Solution in C,Let Us Understand Computer Hacker Earth Solution in C,Count Numbers Hacker Earth Solution in C

#include <stdio.h>
#define ma 1000000007
int main(){
int num,i,a;
unsigned long long int ans=1;
scanf("%d", &num);

for(i=0;i<num;i++){
   scanf("%d", &a);
   ans=(ans*a)%ma;
}
printf("%d", ans);

}

===============or=================
#include <stdio.h>
const int ma = 1e9+7;
int main(){
int num,i,a;
unsigned long long int ans=1;
scanf("%d", &num);

for(i=0;i<num;i++){
   scanf("%d", &a);
   ans=(ans*a)%ma;
}
printf("%d", ans);

}

=============================================================
Let Us Understand Computer Hacker Earth Solution in C

#include <stdio.h>
#include <math.h>
int main()
{
unsigned long long int t;
scanf("%llu",&t);
while(t--)
{
unsigned long long int k,n,ans,i;
scanf("%lld",&n);
i=1;
while(i<=sqrt(n))
{
i=i*2;
}
if(n/i>=i/2)
ans=n-n/i;
else
ans=(n-(i/2))+1;
  printf("%lld\n",ans);
}
}

===========Count Numbers Hacker Earth Solution in C==============
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. int main()
  4. {
  5. int t,i;
  6. scanf("%d",&t);
  7. for(i=1;i<=t;i++)
  8. {
  9. int n,j,c=0;
  10. scanf("%d",&n);
  11. n++;
  12. char s[n];
  13. scanf("%s",s);
  14. for(j=0;s[j]!='\0';j++)
  15. {
  16. if(isdigit(s[j]) && !isdigit(s[j-1]))
  17. c++;
  18. }
  19. printf("%d\n",c);
  20. }
  21. return 0;
  22. }

Thursday, 22 August 2019

Finalized without bounds infix to postfix

#include<stdio.h>
#include <process.h>
#define MAX 50
char stack[MAX];
int top=-1;
int getpriority(char);
void push(char);
char pop();
main()
{
char infix[50],temp,ch,postfix[50];
int i=0,j=0;
printf("enter the infix expression\n");
scanf("%s",&infix);
while(infix[i] != '\0')
{
ch=infix[i];
if(ch == '(')
push(ch);
else if(ch == ')')
{
while(stack[top]!='(')
postfix[j++]=pop();
temp=pop();// popping the (
}
else if(isdigit(ch)||isalpha(ch))
postfix[j++]=ch;
else
{
while(getpriority(stack[top])>=getpriority(ch))
postfix[j++]=pop();
push(ch);
}
i++;
}
while(top != -1)
postfix[j++]=pop();
postfix[j]='\0';
puts(postfix);
}
int getpriority(char ch)
{
if( ch == '*'|| ch == '/'|| ch == '%')
return 2;
else if(ch == '+' || ch == '-')
return 1;
else
return 0;
}
void push(char item)
{
if(top == MAX-1)
{
printf("stack is full");
return;
}
top=top+1;
stack[top]=item;
}

char pop()
{
if(top == -1)
{
printf("stack empty");
return;
}
char temp;
temp=stack[top];
top=top-1;
return temp;
}

Resize an image keeping Aspect Ratio using Python

import cv2
import numpy as np

img = cv2.imread('flower.jpg');
# To resize the image keeping aspect ratio
rows=100.0/img.shape[1] #img.shape[1] gives the number of rows
dimensions = (100, int(img.shape[0]*rows))
resized = cv2.resize(img, dimensions, interpolation = cv2.INTER_AREA)
cv2.imshow("Original", img)
cv2.imshow("Resized", resized)
cv2.waitKey(0)
cv2.destroyAllWindows


Output:





















Resized Image:


Tuesday, 20 August 2019

Add the integral and decimal part of a floating number without using . in scanf in C

#include<stdio.h>
int main()
{
    float x=12.25;
    int y=x,c=0;
    float d = x-y;
 
    for(; x !=((int)x);x = x*10)
    {
        c++;
    }
    while(c>0)
    {
        d=d*10;
        c--;
    }
    printf("%d",y+(int)d);

}

Friday, 2 August 2019

Questions on Conditional Statements in C:


Library fine:
https://www.hackerrank.com/challenges/library-fine/problem

#include<stdio.h>
int main()
{
int dr,mr,yr,de,me,ye,fine=0;
scanf("%d%d%d",&dr,&mr,&yr);
scanf("%d%d%d",&de,&me,&ye);
if(yr == ye)
{
if(mr > me)
fine =(mr-me)*500;
else if(mr == me)
{
if(dr>de)
fine = (dr-de)*15;
}
}
else if(yr>ye)
fine =10000;
printf("%d",fine);
return 0;
}


Given an integer, , perform the following conditional actions:
  • If  is odd, print Weird
  • If  is even and in the inclusive range of  to , print Not Weird
  • If  is even and in the inclusive range of  to , print Weird
  • If  is even and greater than , print Not Weird
The above statements can be converted to 

 if the number is from 6 to 20 and the number is odd then  print "Weird" and if the number is even also print "Weird" and so we conclude if the number is even or odd and is from 6 to 20 then print "Weird"
so the condition becomes

if( n %2 != 0 || (n>=6 && n<= 20))
                   printf("Weird");
else
                  printf("Not Weird");




Sunday, 21 July 2019

Count the number of times a character has occurred before a given index in a given string: C, C++ and Python Program

Sample Input:
11                             is length of string
aabcdbcdbcd            given string       
4                               Number of Queries
1                               we have to find the number of times character at index 1 occurred before index 1
3                               we have to find the number of times character at index 3 occurred before index 3
4                               we have to find the number of times character at index 4 occurred before index 4
5                               we have to find the number of times character at index 5 occurred before index 5


Sample Output:
1
0
0
1

In C:
#include<stdio.h>

int main()
{
 unsigned long long int n,i,q,p;
 scanf("%llu",&n);
 char st[n+1];
 scanf(" %s",st);

 scanf("%llu",&q);
 long long int h[26][500000]={{0}};

 char j;
 for(i=1;st[i]!='\0';i++)
 {
     for(j='a';j<='z';j++)
     {
         if(st[i] == j)
            

             h[st[i]-'a'][i]=h[j-'a'][i-1]+1;
         else 
            h[j-'a'][i]=h[j-'a'][i-1];
            
     }
 }

 while(q--)
 {
     scanf("%llu",&p);
     char temp=st[p-1];
     printf("%lld\n",h[temp-'a'][p-1]);
 }
}

In C++


#include <bits/stdc++.h>
using namespace std;

int main()
{
    unsigned long long int n,i,q,p;
    scanf("%llu",&n);
    char st[n+1];
    scanf(" %s",st);
    scanf("%llu",&q);
    while(q--)
    {
        scanf("%llu",&p);
        printf("%lld\n",count(st,st+p,st[p]));
    }
 

    return 0;