Sunday 27 January 2019

Binary Representation - Squares

The program must accept an integer N as the input. The program must print the binary representation of the square of each integer from 1 to N as the output.

Boundary Condition(s):
1 <= N <= 1000

Input Format:
The first line contains the integer N.

Output Format:
The first line contains the binary representation of squares from 1 to N are separated by space(s).

Example Input/Output 1:
Input:
4
Output:
1 100 1001 10000

Explanation:
The square of 1 is 1. So the binary representation of 1 is 1.
The square of 2 is 4. So the binary representation of 4 is 100.
The square of 3 is 9. So the binary representation of 9 is 1001.
The square of 4 is 16. So the binary representation of 16 is 10000.
Hence the output is 1 100 1001 10000

Program

#include<stdio.h>
#include <stdlib.h>

int main()
{
   int N,a[1000],i,j,k=0;
   scanf("%d",&N);
   for(i=1;i<=N;i++)
   {
        j=i*i;
        k=0;
       while(j>0)
       {
           a[k++]=j%2;
           j=j/2;
       }
       for(k=k-1;k>=0;k--)
       printf("%d",a[k]);
       printf(" ");
   }

}

Output

9

1 100 1001 10000 11001 100100 110001 1000000 1010001

Monday 21 January 2019

Positive Negative Series

The program must accept an integer N as the input. The program must print the sum of the first N terms in the series given below.
The order of the series must be 1, -2, 3, -4, 5, -6 and so on (All the odd terms in the series formed from the positive odd numbers and all the even terms in the series formed from the negative even numbers).

Boundary Condition(s):
1 <= N <= 10^4

Input Format:
The first line contains the integer N.
Output Format:
The first line contains the sum of the first N terms in the above mentioned series.

Example Input/Output 1:
Input:
5
Output:
3
Explanation:
The first 5 terms are 1, -2, 3, -4 and 5 and their sum is 3 (1 - 2 + 3 - 4 + 5).
Hence the output is 3

Program

#include<stdio.h>
#include <stdlib.h>

int main()
{
       
   int sum=0,i,j,N;
   scanf("%d",&N);
   for(i=1;i<=N;i+=1)
   {
       if (i%2==0)
            sum=sum + (-1)*i;
       else
            sum=sum+i;
   }
   
   printf("%d",sum);


}

Output:
Input:
8

Output:
-4

Sunday 20 January 2019

Sum of every X integers


The program must accept N integers and an integer X (where X is the factor of N) as the input. The program must print the sum of every X integers among N integers as the output.
Boundary Condition(s):
1 <= N <= 1000
1 <= Each integer value <= 1000
1 <= X <= N
Input Format:
The first line contains the integer N.
The second line contains N integers separated by space(s).
The third line contains the integer X.
Output Format:
The first line contains the sum of every X integers among N integers separated by a space.
Example Input/Output 1:
Input:
8
2 3 4 9 8 7 1 5
4
Output:
18 21
Explanation:
The first 4 integers are 2, 3, 4 and 9. So their sum is 18.
The next 4 integers are 8, 7, 1 and 5. So their sum is 21.
Hence the output is 18 21

Program
#include<stdio.h>
#include <stdlib.h>

int main()
{
   int N,A[1001],i,X,b=0,c=0,cnt=0;
   scanf("%d",&N);
   for(i=0;i<N;i++)
      scanf("%d",&A[i]);
   scanf("%d",&X);
   cnt=X;
   for(i=0;i<N;i++)
   {
       b+=A[i];
       --cnt;
       if (cnt==0)
       {
        printf("%d ",b);
        b=0;
        cnt=X;
       }
       
   }

}

Input/Output:
Input:
9
21 7 46 37 5 73 6 8 9
1
Output
21 7 46 37 5 73 6 8 9

Thursday 17 January 2019

Toggle Case - Vowels

The program must accept a string S as the input. The program must toggle the case of vowels in the string S. Then the program must print the modified string as the output.

Boundary Condition(s):
1 <= Length of S <= 100

Input Format:
The first line contains the string S.

Output Format:
The first line contains the modified string.

Example Input/Output 1:
Input:
EquilIbriUm

Output:
eqUIlibrIum

Explanation:
The vowels in the string "EquilIbriUm" are 'E', 'u', 'i', 'I', 'i' and 'U'.
So toggle the case of all the vowels in the string "EquilIbriUm".
Hence the output is eqUIlibrIum

Example Input/Output 2:
Input:
JUNKVIRUS

Output:
JuNKViRuS

Program
#include<stdio.h>
#include <stdlib.h>

int main()
{
  char S[100];
  int i=0;
  scanf("%s",S);
  
  while(S[i]!='\0')
  {
    switch (S[i])
    {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u': S[i]=toupper(S[i]);
                  break;
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U': S[i]=tolower(S[i]);
                  break; 
    } 
    ++i;
  }
  printf("%s",S);
     
      
}

OUTPUT
EvaluAte
evAlUatE

C program to display a string until a specified character

The program gets the string S and a character C as input. The characters in the input string are displayed until the specified input character in the string.

For example,
Input
Evaluate
l
Output
Eva

Program
#include<stdio.h>
#include <stdlib.h>

int main()
{
  char S[100],C;
  int i=0;
  scanf("%s %c",S,&C);  //Leave a space between %s and %c to skip enter key input
    
  while(S[i]!=C)
  {
      printf("%c",S[i]);
      ++i;
    }    
      
}

C Program to reverse middle K characters

The program must accept a string S and an integer K as the input. The program must reverse the middle K characters in S. Then the program must print the modified string as the output.
Note:  The length of S and the integer K are always either odd or even.

Boundary Condition(s):

1 <= Length of S <= 100
1 <= K <= Length of S

Input Format:

The first line contains the string S.
The second line contains the integer K.

Output Format:
The first line contains the modified string.

Example
Input:
acknowledgement
7

Output:
acknegdelwoment

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

int main()
{
   int K,cnt=0,i=0,j,a,l;
   char S[100],tmp[100];
   scanf("%s%d",S,&K);
   a=strlen(S);
   if ((a%2==0 && K%2==0) || (a%2!=0 && K%2!=0))
   {
        cnt=K;
        j=(a-K)/2;
        l=j;   
        while (cnt>0)
       {
                     tmp[i++]=S[l++];
                      cnt--;
       }
       tmp[i]='\0';
      for(l=j,i=i-1;i>=0;--i)
                 S[l++]=tmp[i];
       printf("%s",S);
   }
}

OUTPUT
example
3

expmale