Monday, 14 March 2016

Pattern 2


Write a program to print the given pattern.

Input Format: 
Input consists of a single integer.

Output Format: 
Refer sample outputs. There is a trailing space at the end of each line.

Sample Input:
5


1 2 3 4 5 
2 3 4 5
3 4 5
4 5
5


Code:
  #include<stdio.h>
int main(){
  int n,i=1,j=1;
  scanf("%d",&n);
  for(i=1;i<=n;i++)
  {
    for(j=i;j<=n;j++)
    {
      printf("%d ",j);
    }
    
    printf("\n");
  }
  return 0;
}
    
      
    
  
  
           

Pattern 1


Write a program to print the given pattern.

Input Format: 
Input consists of a single integer.

Output Format: 
Refer sample outputs. There is a trailing space at the end of each line.

Sample Input: 5


1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Code:
  #include<stdio.h>
int main(){
  int n,i,j,a;
  scanf("%d",&n);
  a=n;
  for (i=1;i<=a;i++)
  {
    for (j=1;j<=n;j++){
    printf("%d ",j);
    }
    printf("\n");
    n--;
  }
  return 0;
}

Series 5


Write a program to generate the first n terms in the series --- 3,9,27,81,...,...

Input Format: 
Input consists of a single integer which corresponds to n.

Output Format: 
Output consists of the terms in the series separated by a blank space.

Sample Input: 
6
Sample Output: 
 3 9 27 81 243 729

Code:
  #include<stdio.h>
int main(){
  int n,i,j=3;
  scanf("%d",&n);
  for (i=1;i<=n;i++)
  {
    printf("%d ",j);
    j=j*3;
  }
  return 0;
}

Series 4


Write a program to generate the first n terms in the series --- 6,11,21,36,56,...

Input Format: 
Input consists of a single integer which corresponds to n.

Output Format: 
Output consists of the terms in the series separated by a blank space.

Sample Input: 
 6
 Sample Output: 
 6 11 21 36 56 81

Code:
  #include<stdio.h>
int main()
{
  int n,i,j=6,sum=5;
  scanf("%d",&n);
  for (i=1;i<=n;i++)
  {
  printf("%d ",j);
  j=j+sum;
    sum=sum+5;
    
  }
  return 0;
}
  

Series 3


Write a program to generate the first n terms in the series --- 2,3,5,7,11,...,17

Input Format: 
Input consists of a single integer which corresponds to n.

Output Format: 
Output consists of the terms in the series separated by a blank space.

Sample Input: 
8

Sample Output: 
 2 3 5 7 11 13 17 19

Code:
  #include<stdio.h>
#include<math.h>
int main(){
  int n,div,count,num,t;
  scanf("%d",&n);
  printf("%d ",2);
  count=1;
  num=3;
  while(count<n)
  {
    t=sqrt(num);
    div=2;
      while(div<=t)
      {
        if(num%div==0)
          break;
        div++;
      }
    if(div>t)
    {
      printf("%d ",num);
      count++;
    }
    num=num+2;
  }   
  return 0;
}

Series 2


Write a program to generate the first n terms in the series --- 20,19,17,..,10,5

Input Format: 
Input consists of a single integer which corresponds to n.

Output Format: 
Output consists of the terms in the series separated by a blank space.

Sample Input:
6

Sample Output:
 20 19 17 14 10 5

Code:
  #include<stdio.h>
int main(){
  int n,i=20,j,k=1;
  scanf("%d",&n);
  for (j=1;j<=n;j++)
  {
  printf("%d ",i);
    i=i-k;
    k++;
  }
  return 0;
}
  
  

Series I


1)Write a program to generate the first n terms in the series --- 1,4,9,16,25, ....

Input Format: 

Input consists of a single integer which corresponds to n.

Output Format: 

Output consists of the terms in the series separated by a blank space.

Sample Input: 
 7
Sample Output: 
1 4 9 16 25 36 49

Code:
  #include<stdio.h>
int main()
{
  int n,i;
  scanf("%d",&n);
  for (i=1;i<=n;i++)
  {
  printf("%d ",i*i);
  }
  return 0;
}