Friday 20 July 2018

Alphabets Plus Digits Sum

The program must accept a string S which has alphabets and digits as the input. The program must find the sum of all digits as D in the input string S. Then the program must print the alphabets which are D positions away from the alphabets in the string S.

Example

Input
435acl

The digits are 4,3,5 and their sum D=12

Output
mox
The alphabets in the input are a, c, l
a+12 = m    c+12 = o    l+12 = x

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

int main()
{
    int D=0,i,c=0;
    char S[100];
    scanf("%s",S);
    for(i=0;S[i]!='\0';i++)
        if (isdigit(S[i]))
        {
            D+=S[i]-48;
        }
    D=D%26;

    for(i=0;S[i]!='\0';i++)
   {
      
        if (isalpha(S[i])) 
       {
           
            S[i]=tolower(S[i]);
            c=D+S[i];
            
            if(c>122)
            {
                c=96+(c-122);
            }
              
            printf("%c",c);
        }
    }   
}

OUTPUT


1121ZU                                                                                                                         
                                                                                                                              
ez  

Friday 13 July 2018

Vertical ZigZag - C columns

This C program accepts two numbers R and C as input and prints the vertical zigzag C columns.

Example 1

Input
5 5

Output
1  10  11  20  21
2   9   12  19  22
3   8   13  18  23
4   7   14  17  24
5   6   15  16  25

Example 2
Input
2 3

Output
1 4 5
2 3 6

Program

#include <stdio.h>

int main()
{
   int a[10][10],R,C,i,j,k=1;
   scanf("%d%d",&R,&C);
    for(i=1;i<=C;i++)
   {
        for(j=1;j<=R;j++)
        {
           if  (i%2==1)
             a[i][j]=k++;
           else
             a[i][j]=--k;
    
        }
        k=k+R;
    }
    for(i=1;i<=R;i++)
    {
         for(j=1;j<=C;j++)
           printf("%d ",a[j][i]);
         printf("\n");
    }
    
}

OUTPUT
5 5

1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25