Saturday, 10 March 2018

Sum of Digits

Sum of Digits



All submissions for this problem are available.
You're given an integer N. Write a program to calculate the sum of all the digits of N.

Input

The first line contains an integer T, total number of testcases. Then follow T lines, each line contains an integer N.

Output

Calculate the sum of digits of N.

Constraints

  • 1  T  1000
  • 1  N  1000000

Example

Input
3 
12345
31203
2123
Output
15
9
8
 

solutions

  1. #include<stdio.h>
  2. int main()
  3. { int t,i,d;
  4. long n,s;
  5. scanf("%d",&t);
  6. for(i=0;i<t;i++)
  7. { s=0;
  8. scanf("%ld",&n);
  9. while(n!=0){
  10. d=n%10;
  11. s=s+d;
  12. n=n/10;
  13. }
  14. printf("%ld\n",s);
  15. }
  16. }

Packaging Cupcakes

Packaging Cupcakes

All submissions for this problem are available.

Now that Chef has finished baking and frosting his cupcakes, it's time to package them. Chef has N cupcakes, and needs to decide how many cupcakes to place in each package. Each package must contain the same number of cupcakes. Chef will choose an integer A between 1 and N, inclusive, and place exactly A cupcakes into each package. Chef makes as many packages as possible. Chef then gets to eat the remaining cupcakes. Chef enjoys eating cupcakes very much. Help Chef choose the package size A that will let him eat as many cupcakes as possible.

Input

Input begins with an integer T, the number of test cases. Each test case consists of a single integer N, the number of cupcakes.

Output

For each test case, output the package size that will maximize the number of leftover cupcakes. If multiple package sizes will result in the same number of leftover cupcakes, print the largest such size.

Constraints

  • 1 ≤ T ≤ 1000
  • 2 ≤ N ≤ 100000000 (108)

Sample Input

2
2
5

Sample Output

2
3

Explanation

In the first test case, there will be no leftover cupcakes regardless of the size Chef chooses, so he chooses the largest possible size. In the second test case, there will be 2 leftover cupcakes


solutions

#include<stdio.h>
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        long n;
        scanf("%ld",&n);       
        printf("%ld\n",n/2+1);
    }
}

 

Wednesday, 7 March 2018

Add Two Numbers

  1. Add Two Numbers

     
    All submissions for this problem are available.
    Shivam is the youngest programmer in the world, he is just 12 years old. Shivam is learning programming and today he is writing his first program. 

    Program is very simple, Given two integers A and B, write a program to add these two numbers.

    Input

    The first line contains an integer T, total number of test cases. Then follow T lines, each line contains two Integers A and B.

    Output

    Add A and B and display it.

    Constraints

    • 1  T  1000
    • 1  A,B  10000

    Example

    Input
    3 
    1 2
    100 200
    10 40
    
    Output
    3
    300
    50


    1. Solutions
       
      #include<stdio.h>
    2. int main()
       {
      int i,T,a[1000],b[1000];
      scanf("%d",&T);

      for(i=0;i<T;i++)
      {scanf("%d %d",&a[i],&b[i]);
      }

      for(i=0;i<T;i++)
      printf("\n %d",a[i]+b[i]);     
       }


     
    
    

Enormous Input Test

Enormous Input Test

All submissions for this problem are available.

The purpose of this problem is to verify whether the method you are using to read input data is sufficiently fast to handle problems branded with the enormous Input/Outputwarning. You are expected to be able to process at least 2.5MB of input data per second at runtime.

Input

The input begins with two positive integers n k (n, k<=107). The next n lines of input contain one positive integer ti, not greater than 109, each.

Output

Write a single integer to output, denoting how many integers ti are divisible by k.

Example

Input:
7 3
1
51
966369
7
9
999996
11

Output:
4 
 

Solutions


#include<stdio.h>
int main()
{
 int n,count=0,i;
  long x,y;
  scanf("%d %ld",&n,&x);
  for(i=0;i<n;i++)
  {
 scanf("%ld",&y);
  if(y%x==0)
  count++;
  }
printf("%d",count);
}

ATM

ATM


All submissions for this problem are available.
Pooja would like to withdraw X $US from an ATM. The cash machine will only accept the transaction if X is a multiple of 5, and Pooja's account balance has enough cash to perform the withdrawal transaction (including bank charges). For each successful withdrawal the bank charges 0.50 $US. Calculate Pooja's account balance after an attempted transaction.

Input

Positive integer 0 < X <= 2000 - the amount of cash which Pooja wishes to withdraw.
Nonnegative number 0<= Y <= 2000 with two digits of precision - Pooja's initial account balance.

Output

Output the account balance after the attempted transaction, given as a number with two digits of precision. If there is not enough money in the account to complete the transaction, output the current bank balance.

Example - Successful Transaction

Input:
30 120.00

Output:
89.50

Example - Incorrect Withdrawal Amount (not multiple of 5)

Input:
42 120.00

Output:
120.00

Example - Insufficient Funds

Input:
300 120.00

Output:
120.00

Solution 

  1. #include<stdio.h>
  2. int main()
    { 
      
      float s;
  3. int n;
  4. scanf("%d%f",&n,&s);
  5. if(n%5==0 && s>=n+0.50)
  6. {printf("%.2f",s-n-0.50);
  7. }
  8. else
  9. printf("%.2f",s);

      return 0;
    }

Life, the Universe, and Everything

  1. Life, the Universe, and Everything  

    All submissions for this problem are available.All submissions for this problem are available.
    Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.

    Example

    Input:
    1
    2
    88
    42
    99
    
    Output:
    1
    2
    88

    Solution

    #include<stdio.h>
  2. int main()
  3. { int n;
  4. while(1){
  5. scanf("%d",&n);
  6. if(n==42)
  7. break;
  8. else
  9. printf("%d\n",n);
  10. }
  11. return 0;
  12. }

Reverse The Number

Reverse The Number All submissions for this problem are available. If an Integer   N , write a program to reverse the given number. ...