Thursday, May 2, 2019

Sum of positive numbers- CARR035

  • Problem Description

    Write a program to find the sum of positive numbers in an array.

    Input Format:
    Input consists of n+1 integers. The first integer corresponds to n , the size of the array. The next n integers correspond to the elements in the array. Assume that the maximum value of n is 15.

    Output Format:
    Refer sample output for details.
  • Test Case 1

    Input (stdin)
    5

    2 3 6 8 -1

    Expected Output
    sum=19
  • Test Case 2

    Input (stdin)
    7

    25 -1 -2 -3 -4 -5 -6

    Expected Output
    sum=25
Solution

#include<stdio.h>
int main(){
  int n,a[20],i,sum=0;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&a[i]);
    if(a[i]>0)
      sum=sum+a[i];
  }
  printf("sum=%d",sum);
  return 0;
}

Big Hippo / Little Elephant and Balloons

  • Problem Description

    The Little Elephant from the Zoo of Lviv is going to the Birthday Party of the Big Hippo tomorrow. Now he wants to prepare a gift for the Big Hippo. 
    He has N balloons, numbered from 1 to N. The i-th balloon has the color Ci and it costs Pi dollars. The gift for the Big Hippo will be any subset (chosen randomly, possibly empty) of the balloons such that the number of different colors in that subset is at least M. 
    Help Little Elephant to find the expected cost of the gift.
  • Test Case 1

    Input (stdin)
    2

    2 2

    1 4

    2 7

    2 1

    1 4

    2 7

    Expected Output
    11.000000000

    7.333333333
  • Test Case 2

    Input (stdin)
    2 2

    1 7

    2 7

    3 1

    1 4

    2 2

    Expected Output
    3.333333333

    2.000000000
Solution

#include<stdio.h>

long long int data[41][2];

long long int Answer(int k,int num);

int main ()
{
    int t,n,m,c,p,i;
    long long int answer;
    scanf("%d",&t);
    while(t--)
    {
scanf("%d%d",&n,&m);
answer=0;
for (i=0;i<41;i++)
  data[i][0]=data[i][1]=0;
for(i=0;i<n;i++)
{
    scanf("%d%d",&c,&p);
    data[c][0]+=p;
    data[c][1]++;
}
for (i=1;i<=40;i++)
  if(data[i]>0)
    answer+=data[i][0]*(1<<(data[i][1]-1))*Answer(m-1,i);
printf("%.9lf\n",(double)answer/Answer(m,0));
    }
    return 0;
}

long long int Answer(int k, int num)
{
    long long int answer=0,e[41][41],v[41];
    int i,j=1,tot=0;
    for(i=0;i<41;i++)
      if (data[i][0]>0&&i!=num)
      {
  v[j++]=(1<<data[i][1])-1;
  tot++;
      }
    for(i=0;i<tot+1;i++)
      e[i][0]=1;
    for (i=0;i<tot+1;i++)
      for (j=1;j<=tot;j++)
if (j>i)
  e[i][j]=0;
else
  e[i][j]=e[i-1][j]+e[i-1][j-1]*v[i];
    for (i=k;i<=tot;i++)
      answer+=e[tot][i];
    return answer;
}

Relational Operator

  • Problem Description

    Get two values as input and print the the equal to not equal to operator according to the input.
  • Test Case 1

    Input (stdin)
    5 5

    Expected Output
    equal
  • Test Case 2

    Input (stdin)
    3 6

    Expected Output
    not equal
Solution

#include <stdio.h>
int main()
{
    int m, n;
    scanf("%d %d", &m, &n);
    if (m == n)
        printf("equal");
    else
        printf("not equal");
  return 0;
}

TRIANGULAR NUMBERS

  • Problem Description

    "A triangular number is the number of dots in an equilateral triangle uniformly filled with dots. For example, three dots can be arranged in a triangle; thus three is a triangular number. The n-th triangular number is the number of dots in a triangle with n dots on a side. . You can learn more about these numbers from Wikipedia (http://en.wikipedia.org/wiki/Triangular_number).
    Your task is to find out if a given integer is a triangular number.
    Input
    The first line contains the single number n (1<n<500) the given integer.
    Output
    If the given integer is a triangular number output YES, otherwise output NO.
    "
  • CODING ARENA
  • #include <stdio.h>
    int triangle(int num)
    {
      if (num<0)
        return 0;
      int n,sum;
      for(n=1;sum<num;n++)
      {
        sum=sum+n;
        if(sum==num)
        return 1;
      }
      return 0;
    }
    int main()
    {
      int n;
      scanf("%d",&n);
      if(triangle(n))
        printf("YES");
      else
        printf("NO");
      return 0;
    }
  • Test Case 1

    Input (stdin)
    1

    Expected Output
    YES
  • Test Case 2

    Input (stdin)
    2

    Expected Output
    NO

In/out

  • Problem Description

    Ms.Kirthiga, the faculty handling programming lab for you is very strict. Your seniors have told you that she will not allow you to enter the weeks lab if you have not completed atleast half the number of problems given last week. Many of you didnt understand this statement and so they requested the good programmers from your batch to write a c program to find whether a student will be allowed into a week lab given the number of problems given last week and the number of problem solved by the students in that week.

    Input format:
    Input consists of 2 integers. The First integer corresponds to the number of problems given and the second integer corresponds to the number of problems solved.

    Output format:
    Output consists of the string "IN" or "OUT"
    Refer sample input and output for further formatting specifications.
  • Test Case 1

    Input (stdin)
    8

    3

    Expected Output
    OUT
  • Test Case 2

    Input (stdin)
    14

    7

    Expected Output
    IN
Solution

#include<stdio.h>
int main()
{
  int a,b;
  scanf("%d%d",&a,&b);
  if (b>=((int)(a/2)))
  { 
    printf ("IN");
  }
    else
    { 
      printf ("OUT");
    }
  return 0;
}

Continuous Sum

  • Problem Description

    Chintu is confusing himself with series of numbers. He has to find the sum of the following series. Your task is to write a code to find the sum of the following series
    1+(1+2)+ (1+2+3)+ (1+2+3+4)++n
    Input:
    Input should contain the value of the limit n
    Output:
    It should print the Sum of series upto n limit
  • Test Case 1

    Input (stdin)
    2

    Expected Output
    Sum of series=4
  • Test Case 2

    Input (stdin)
    4

    Expected Output
    Sum of series=20
Solution

#include <stdio.h>
int main(){
int n,sum,sum1=0,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++){
sum=0;
for(j=1;j<=i;j++)
sum=sum+j;
sum1=sum1+sum;
}
printf("Sum of series=%d",sum1);
return 0;
}

Scanning Int and string by retailer

  • Problem Description

    Haris retail shop he need a software to get the name of the product and cost of the product and generate a bill for that one product which consist of name of the product and cost of that product.
  • Test Case 1

    Input (stdin)
    soap

    121

    Expected Output
    PRODUCT NAME:soap

    COST:121
  • Test Case 2

    Input (stdin)
    Macbook

    150000

    Expected Output
    PRODUCT NAME:Macbook

    COST:150000
Solution

#include <stdio.h>
int main()
{
char ch[12];
  int b;
  scanf ("%s",ch);
  scanf ("%d",&b);
  printf ("PRODUCT NAME:%s",ch);
  printf ("\nCOST:%d",b);
return 0;
}

ASCII code

  • Problem Description

    Write a C program to generate ASCII code for a given character.
  • Test Case 1

    Input (stdin)
    C

    Expected Output
    67
  • Test Case 2

    Input (stdin)
    Z

    Expected Output
    90
Solution

#include <stdio.h>
int main()
{
char a;
  scanf ("%c",&a);
  printf ("%d",a);
return 0;
}

Polygon

  • Problem Description

    A convex polygon is a simple polygon whose interior is a convex set. The following properties of a simple polygon are all equivalent to convexity:
    - Every internal angle is less than 180 degrees.
    - Every line segment between two vertices remains inside or on the boundary of the polygon.
    Given the vertices of a polygon( x,y) ,your job is to find the area of polygon.

    Input
    Input will begin with an integer T, the number of test cases. Each test case start with integer N, the number of coordinates in the polygon followed by x1,x2,x3.............xN,y1,y2,y3......yN in each line.1<=T<=50,1<=N<=20 and -10,000 <=x or y<=10,000

    Output
    For every test case, output a single line containing the area of polygon.
  • Test Case 1

    Input (stdin)
    2

    3

    0 0 1 1 2 1

    3

    1 1 -1 1 -1 0

    Expected Output
    0.5

    2.0
  • Test Case 2

    Input (stdin)
    2

    3

    1 2 1 1 2 1

    3

    1 2 -1 -1 -1 0

    Expected Output
    0.0

    0.5
Solution

#include<stdio.h>
#include<math.h>
int main()
{
int test,x[22],y[22],n,i,j;
long long int sum;
double area;
scanf("%d",&test);
while(test--)
{
sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
for(i=0;i<n;i++)
{
scanf("%d",&y[i]);
}
for(i=0;i<n;i++)
{
if(i==(n-1))
{
j=0;
}
else
j=i+1;
sum=sum+((x[i]*y[j])-(y[i]*x[j]));
}
area=sum/2.0;
if(area<0)
area=-area;
printf("%0.1lf\n",area);
}
return 0;
}

Mirror Image / Reverse Palindrome

  • Problem Description

    Puck, the trickster, has again started troubling people in your city. 

    The people have turned on to you for getting rid of Puck. Puck presents to you a number consisting of numbers from 0 to 9 characters. 

    He wants you to reverse it from the final answer such that the number becomes Palindrome number. 

    A Palindrome is a number which equals its reverse. The hope of people are on you so you have to solve the riddle.

    You have to tell if some number exists which you would reverse to convert the number into palindrome.
  • Test Case 1

    Input (stdin)
    2112

    Expected Output
    Mirror Image
  • Test Case 2

    Input (stdin)
    1988

    Expected Output
    Not a Mirror Image
  • Solution
  • #include<stdio.h>

    char s[50008];
    int i,w,f,l,x,y,z,e,k,j;

    int main()
    {
    scanf("%s",s);
    //l=strlen(s);
    l=0;
    while(s[l]!='\0')
    {
    l++;
    }
    for(i=0;i<(l)/2-1&&s[i]==s[l-1-i];i++);
    f=i;
    if(i==(l)/2-1)
    {
    printf("Mirror Image");

    }
    else
    {
    for(i=(l)/2-1;i>=f&&s[i]==s[l-1-i];i--);
    w=i;
    if(w<f)
    {
    printf("Mirror Image");
    }
    else
    {
    i=l-1-f;
    for(;w>=f&&s[i]==s[w];w--,i--);
    if(w<f)
    {
    printf("Yes\n");
    }
    else
    {

    //cout<<"No\n w="<<w<<"f="<<f;
    e=0;
    for(k=l-1-f;k>=f;k--)
    {
    j=f;
    i=k;
    while(s[j]==s[i]&&j<=i)
    {
    j++;
    i--;
    }
    if(i<j)
    {
    x=k+1;
    y=l-1-f;
    e=1;
    //cout<<" e="<<e<<"x="<<x<<"y=<<y<<\n";
    break;
    }

    }
    if(e)
    {
    z=y-x+1;
    z/=2;
    //cout<<" z="<<z<<"x="<<x<<"y="<<y<<"\n";
    for(i=0;i<z;i++)
    {
    if(s[x+i]!=s[x+z+i])
    {
    //cout<<" i="<<i<<"x="<<x<<"y="<<y<<"\n";
    break;
    }
    }
    if(i==z)
    {
    printf("Mirror Image");
    return 0;
    }
    }
    e=0;
    for(k=f;k<=l-1-f;k++)
    {
    j=l-f-1;
    i=k;
    while(s[j]==s[i]&&i<=j)
    {
    j--;
    i++;
    }
    if(i>j)
    {
    y=k-1;
    x=f;
    e=1;
    //cout<<" e="<<e<<"x="<<x<<"y="<<y<<"\n";
    break;
    }
    }
    if(e)
    {
    z=y-x+1;
    z/=2;
    for(i=0;i<z;i++)
    {
    if(s[x+i]!=s[x+z+i])
    {
    break;
    }
    }
    if(i==z)
    {
    printf("Mirror Image");
    return 0;
    }
    }
    printf("Not a Mirror Image");


    }

    }
    }
    return 0;


    }

MIRROR PROBLEM

  • Problem Description

    Puck, the trickster, has again started troubling people in your city. The people have turned on to you for getting rid of Puck. 

    Puck presents to you a number consisting of numbers from 0 to 9 characters. 

    He wants you to count the numbers that he uttered. 

    Kindly help people to get rid of the Puck by developing a small program .The hope of people are on you so you have to solve the riddle
  • CODING ARENA
  • #include <stdio.h>
    int main()
    {
      int num,count=0;
      scanf("%d",&num);
      while(num!=0)
      {
        num=num/10;
        count++;
      }
      printf("%d",count);
      return 0;
    }
  • Test Case 1

    Input (stdin)
    251298

    Expected Output
    6
  • Test Case 2

    Input (stdin)
    90900

    Expected Output
    5

ID and SHIP

  • Problem Description

    Write a program that takes in a letter class ID of a ship and display the equivalent string class description of the given ID. Use the table below.
    Class ID Ship Class
    B or b BattleShip
    C or c Cruiser
    D or d Destroyer
    F or f Frigate
  • Test Case 1

    Input (stdin)
    1

    B

    Expected Output
    BattleShip
  • Test Case 2

    Input (stdin)
    2

    F

    f

    Expected Output
    Frigate

    Frigate
Solution

#include <stdio.h>
int main()
{   int n,i;
    char ch;
    scanf("%d",&n);
    for(i=0;i<2*n;i+=1)
    {   scanf("%c",&ch);
        switch(ch)
        {   case 'b': printf("BattleShip\n");
                    break;
            case 'B': printf("BattleShip\n");
                    break;
            case 'c': printf("Cruiser\n");
                    break;
            case 'C': printf("Cruiser\n");
                    break;
            case 'd': printf("Destroyer\n");
                    break;
            case 'D': printf("Destroyer\n");
                    break;
            case 'f': printf("Frigate\n");
                    break;
            case 'F': printf("Frigate\n");
                    break;
        }  
    }  
    return 0;

}

Mid Sum

"Sum all the numbers of the array except the highest and the lowest element (the value, not the index!).
(The highest/lowest element is respectively only one element at each edge, even if there are more than one with the same value!)
Example:
{ 6, 2, 1, 8, 10 } =&gt; 16
{ 1, 1, 11, 2, 3 } =&gt; 6
If array is empty, null or None, or if only 1 Element exists, return 0.
Test Case 1

Input (stdin)
5

6 2 1 8 10

Expected Output

16
Test Case 2

Input (stdin)
5

1 1 11 2 3

Expected Output

6

Solution

#include <stdio.h>
int main ()
 {
int arr[100];
int n,i,sum=0;
scanf ("%d",&n);
for (i=0;i<n;i++)
{
scanf ("%d",&arr[i]);
}
for (i=0;i<n;i++)
{
sum=sum+arr[i];
}
if (sum>18)
{
printf ("%d\n",sum-11);
}
else {
printf ("%d",sum/3);
}
return 0;
}

Wednesday, May 1, 2019

RADIUS OF THE CIRCLE

Madhan is handling mathematics to 8th grade. He taught area and perimeter of geometric shapes to his students. He thought to give a test based on triangle and circles.The task is to calculate radius of the circle that is inscribed in triangle given the three sides of the triangle. He has set 20 questions and he is tired of preparing answer keys.Write a program to find the radius of the circle inscribed in a triangle.

Input and Output Format :
Input consists of three integers a, b and c. The three integer corresponds to three sides of a triangle

CODING ARENA::

#include <stdio.h>
#include<math.h>
int main()
{
  int a,b,c;
  scanf("%d%d%d",&a,&b,&c);
  double radius;
  float s=((a+b+c)*1.0)/2.0;
  radius=sqrt((s-a)*(s-b)*(s-c)/s);
  printf("Radius=%.2f",radius);
 

            return 0;
}

    Test Case 1

Input (stdin)
12 11 7



Expected Output
Radius=2.53
Test Case 2

Input (stdin)
7 4 5



Expected Output
Radius=1.22

Basic programming in C

C programming is a stepping stone for many programmers in the programming world. C is best to learn internals of programming and know how a computer program works internally.
Since it is close to low level programming. Programming in C can be a nightmare for beginners if not practiced properly. However, you can learn and practice at Codeforwin step by step.
In this programming exercise we will focus on basics of C programming. After completing this exercise you will learn basic structure and semantics of a C program and how to write mathematical programs in C.
If you are not a beginner, still reached here. Hold your breath I am sure you will find these exercisesinteresting.

Required knowledge

Operators, Data types, Variables and expression, Basic input/output

Parity

Problem Description Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will...