Saturday 22 September 2018

1. To Print Butterfly Pattern using loops


Solution:

#include <stdio.h>

int main()

{
    int i,j,n;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=2*n;j++)
        {
            if(i<j)
            printf(" ");
            else
            printf("&");
            if(i<=2*n-j)
            printf(" ");
            else
            printf("&");
        }
        printf("\n");
    }
        for(i=1;i<=n;i++)
    {
        for(j=1;j<=2*n;j++)
        {
            if(i>(n-j+1))
            printf(" ");
            else
            printf("&");
            if((i+n)>j)
            printf(" ");
            else
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

Tuesday 18 September 2018

TCS NINJA CODING Season-2

C program to print Nth number in a series

To find Nth number in series of 
0 0 1 2 2 4 3 6 4 8 5  ........................
The above series even index contains whole numbers and odd index contains even numbers.

When given Nth index number the number at that position is to be printed.
Input:
First line of input contains Nth index number
Output:
Number at Nth index should be printed

Example:

Input:
10
Output:
5
Explanation:
In the above example
When we enter 10 index number then at that 10 index number 5 is present.
0 0 1 2 2 4 3 6 4 8 5

SOURCE CODE:
#include<stdio.h>
int main()
{
    int n,i,j,e=0,od=0,k;
    long int a[10000]={0};
    scanf("%d",&n);
 
    for(i=2;i<=n;)
    {
        if(i%2==0)
        {
            e=e+1;
            a[i]=e;
        }
        else
        {
            od=od+2;
            a[i]=od;
        }
        i++;
    }
    for(i=0;i<=n;i++)
    {
       if(i==n)
        printf("%d ",a[i]);
    }
    return 0;

}

TCS NINJA CODING PROGRAM Season-2 NQT

C Program to replace lower case letters with upper case (Vice-versa) and replacing vowels with * 

Input:
First line of input contains string 1
Second line of input contains string 2
Third line of input contains string 3

Output:
First line of output were replaced by upper case letters 
Second line of output were replaced by lower case letters
Third Line of output contains * replacing vowels.

Example:

Input:
how 
ARE 
you

Output:
HOW
are
y**

SOURCE CODE:

#include<stdio.h>
#include<string.h>
int main()
{
    char str1[10],str2[10],str3[10];
    int i,j,c=0;
    gets(str1);
    gets(str2);
    gets(str3);
    
    
    for(i=0;str1[i]!='\0';i++)
    {  
        if(str1[i]>='a'&&str1[i]<='z')
        {
            str1[i]=str1[i]-32;
        }
    }
    for(i=0;str2[i]!='\0';i++)
    {  
        if(str2[i]>='A'&&str2[i]<='Z')
        {
            str2[i]=str2[i]+32;
        }
    }
  for(i=0;str3[i]!='\0';i++)
    {  
        if(str3[i]=='a'||str3[i]=='A'||str3[i]=='e'||str3[i]=='E'||str3[i]=='i'||str3[i]=='I'||str3[i]=='o'||str3[i]=='O'||str3[i]=='u'||str3[i]=='U')
        {
            str3[i]='*';
        }
    }
   
    puts(str1);
    puts(str2);
    puts(str3);
    return 0;
}

C program to print number of times a digit has been Repeated

Input:
First line contains 'N'
Second line of input contains N number of integers.

Output:
Number of times a digit has been repeated.

Example:

Input:
6
1 1 7 7 7 9 9 9

Output:
2 3 3

Explanation:
1 is repeated for 2 times 7 is repeated for 3 times and 9 is repeated for 3 times.

SOURCE CODE:

/*To find number of times a digit is repeated*/
#include<stdio.h>
int main()
{
    int a[50],n,i,j,c=0,r=0,b[10]={0},k;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    
    for(i=0,k=0;i<n;i=j,k++)
    {
        c=0;
        for(j=i;;j++)
        {
            if(a[i]==a[j])
            {
                c++;
            }
            else
            {
                b[k]=c;
                break;
            }
        }
    }
    k=0;
    while(b[k]!=0)
    {
        printf("%d ",b[k]);
        k++;
    }
    return 0;
}

Random password generator in Java

       Random password generator in Java Source code: mport java.io.*; import java.util.*; public class Main { public static void main(Str...