Tuesday 12 July 2016

Software Engineering Part 3


  1. At which level does the Software process rating start in continuous assessment model?

      2
      3
      1
      0

  2. What is the final level of the software process assessment ratings?

      Both of the listed options
      Optimised
      Completion
      None of the listed options

  3. Quantitative assessments are evaluated based on which of the following?

      Action taken
      Process planned
      Objective planned
      Scores
     

  4. Which of the following is an activity performed in SCAMPI method?

      Final reporting
      Primary activity
      Technical analysis
      Information gathering

Monday 27 June 2016

Software Engineering Part 2


  1. The approach to construction affects the project's ability to reduce complexity, anticipate change, and construct for verification. State whether the statement is TRUE or FALSE.

  2.   True
      False

  3. The choice pf construction method affects which of the following?

  4.   All of the given options
      the order in which construction prerequisite are performed
      and the degree to which construction prerequisites are expected to be completed before construction work begins
      extent to which construction prerequisites are performed
  5. What are the correct sequence of steps followed while debugging a piece of code?

  6.   1. Identify the exact place in the code causing it
      2. Find the defect in the code
      3. Identify the source of problem
      4. Test to check if the fix is working correctly
      5. Fix the defect
      1,2,3,4,5
      2,4,3,1,5
      2,3,1,5,4
      1,3,5,2,4

  7. Software Construction procedure high volume of configure items. Thus Software Construction is related Software Configuration Management. State whether the statement is True or False.

  8.   False
      True

  9. A measurement of code construction can be useful for which of the following purpose?
  10.   improving the construction process
      ensuring quality during construction
      managing construction
      all of the given options

Wednesday 8 June 2016

Software Engineering Part - I


  1. Which of the following diagram depicts the logic of use case descriptions. It shows the information that passes between objects in the system and throughout the execution of the use case?

  2.   activity diagram
      class diagram
      use case diagram
      flowchart diagram
      sequence diagram

     
  3. Requirements refinement and documentation takes place very early on in the project cycle. State True or False.

  4.   True
      False

     
  5. The activity diagram analysis technique is used in which of the below analysis areas?

  6.   Process flow analysis
      Structured Analysis
      Object-oriented analysis
      Solution Development Analysis

     
  7. Which of the below is NOT the most common type of Environmental requirement?

  8.   interface requirements
      audit requirements
      legal and regulatory requirements
      globalization and localization requirements

     
  9. Which of the below depicts workflow or a sequence of related activities by showing the order in which activities occur and the decisions that control the flow?

  10.   activity diagram
      dataflow diagram
      workflow diagram
      flowchart diagram
      sequence diagram

     
  11. What is the popluar model used by Business Analysts to analyze the requirements?

  12.   Usage Model
      Process and Flow model
      Data and Behaviour model
      All the listed options

     
  13. Which one of the following cannot be depicted using state machine diagrams about an object?

  14.   the transitions between states
      the events that cause the transitions
      data that flows within the domain
      the possible states it can be in
     

  15. The benefits of knowing how to refine requirements is that you will be able to _______

  16.   classify quality of service requirements according to type
      recognize the importance of considering assumptions and constraints when analyzing requirements
      effectively decompose requirements for documentation
      use appropriate techniques to document requirements
      All the listed options

  17. Functional requirements are required for all projects, and must be thoroughly documented and analyzed. State True or False.

  18.   True
      False

     
  19. UML defines standards for the format and components of activity diagrams.

  20.   True
      False

  21. Identify non functional requirement(s)?
  22.   Security
      All the listed options
      Performance
      Usability

  23. Identify the requirement that states -"A system must track all failed login attempts".

  24.   Operational requirement
      Interface requirement
      Environment requirement
      Performance requirement
      Privacy requirement

  25. The advantage of ____________ is that functional requirements are traceable and map to objectives.
  26.   flowchart
      models
      textual documentation
      matrix documentation
      diagrams

    Note: If any of the answer is wrong. Please post a comment.

Tuesday 17 May 2016

Sub Queries MCQ


  1. The SQL _______ Query is used to delete the existing records from a table.

  2.         DELETE
            REMOVE
            DELETIT

  3. Subqueries that return more than one row can only be used with multiple value operators, such as the IN operator.
    state True or False:

  4.         True
            False

  5. Which one of the following sorts rows in SQL?

  6.         SORT BY
            ALIGN BY
            ORDER BY
            GROUP BY

  7. We can test for the nonexistence of tuples in a subquery by using the _____ construct.

  8.         EXISTS
            NOT EXISTS
  9. Subquery is usually added in the WHERE Clause of the SQL statement.
    state True or False:

            True
            False

Thursday 14 April 2016

SQL MCQ Questions

  1. Which of the following creates temporary relation for the query on which it is defined ?
  2.         With
            From
            Where
            Select
  3. The EXISTS keyword will be true if:
  4.         Any row in the subquery meets the condition only.
            All rows in the subquery fail the condition only.
            Both of these two conditions are met.
            Neither of these two conditions is met.
  5. Which of the following relational algebra operations do not require the participating tables to be union-compatible?
  6.         Union
            Insertion
            Difference
            Join
  7. Which of the following operations need the participating relations to be union compatible?
  8.         Union
            Insertion
            Difference
            All the above
  9. Select name from instructor where salary <=100000 and salary >=90000;
    This query can be replaced by which of the following ?
  10.         Select name from instructor where salary between 90000 and 100000;
            Select name from employee where salary <= 90000 and salary>=100000;
            Select name from employee where salary between 90000 and 100000;
            Select name from instructor where salary between 100000 and 90000;
  11. Select __________
    from instructor
    where dept name= ’Comp. Sci.’;
    Which of the following should be used to find the mean of the salary ?
  12.         Mean(salary)
            Count(salary)
            Avg(salary)
            Sum(salary)
  13. Aggregate functions are functions that take a ___________ as input and return a single value
  14.         Collection of values
            Single value
            Aggregate value
            Both a & b

Saturday 19 March 2016

DBMS Questions


  1. A table is the primary unit of physical storage of data in a database.State True or False.
  2.         True
            False

  3. Which one of the following is a large database from which information can be extracted and analyzed?
  4.         Database
           Sysbase
           Datawarebase
           DBMS

  5. Which one of the following data model resembles like a tree structure?
  6.         Network model
            Database model
            Relational model
            Hierarchical model

  7. Which of the given option is true regarding the database?
  8.         Data Sharing
            Data concurrency
            All the listed options
            Data Integrity
            Data security

  9. DBMS can replace a file processing system. State True or False.
            True
            False

  10. What is the advantage of minimizing the data redundancy?
            Avoid inaccuracies and inconsistencies in the data
            Save Time in processing the data
            All the above
  11.        To free up the storage space

  12. Which of the following are the types of data models
            All of the listed option
            Network Model
            Hierarchial Model
            Relational Model

  13. Database system is used to restrict the user from unauthorized access.State True or False
            True
            False

  14. Which one of the following is true about RDBMS?
      1. It has relations data integrity
      2. It can maintain many users at the same time
      3. It doesn’t support client/server based application
      4. It is a relational database system
      5. It cant maintain many user at the same time.
            1,3
            2,4
            3,4,5
            1,2,4
            4,5

  15. What is the main component of DBMS?
            Data Modelling
            Data Dictionary
            OLAP Database
            Data Sharing

  16. In which form does the data gets stored in the database?
            Electronic form
            Rich text form
            Non Electronic form
            None of the listed options

  17. Which of the given term refers to Data about the data?
            Data Abstraction
            Views
            Metadata
            Program

  18. DBMS controls redundancy.State True or False.
            False
            True

  19. DBMS helps us to?
      1. Define Schemas and Subschemas in a database and establish relationships between different data elements
      2. Enter Data Into Database
      3. Manipulate and Process Data
      4. None of the listed options
            1&2
            1&3
            4
            1&2&3

  20. The traditional storage of data which is organized by the customers by storing it in separate folders. This is the example of which type of database system?
            Object oriented
            Hierarchical
            Relational
            Network

  21. Arena is married to Vasu and she has a unique SSN. This is an example of a (an):
            Unary Relationship
            Binary Relationship
            Hierarchical Relationship
            Ternary Relationship

  22. In DBMS, what will be the outcome when we map a multivalued attribute for any entity from the ER model to the relational model?
            None of the above
            Many relations, one for each of the distinct values of the attribute
            A column in the relation that represents the entity E
            One relation that contains a foreign key and a column for the attribute

  23. The efficient performance of the database is based on which of the following factors?
      1. Database Design
      2. Application Design
      3. Network Speed
      4. None of the listed option
           1,2,3
            1,3
            1,2
            4

  24. There are two tables Customers and Orders. The customers table would include information about the names , addresses and phone numbers of customers, the order table would include information about the products ordered , the customers,order dates and sales prices ,the Sales manager wants to view only the number of orders placed by each customer on a given date, A Finance Manager in the same company can group the table entries to view only the names and addresses of customers with pending payments.This Grouping of data is possible in?
           Hierarchical Database
            Relational Databse
            Network Database
            Object Oriented Database

Note: If any of the answer is wrong please post a comment. I will update it.

Decipher my Ciphertext


In the language of cryptography, ciphertext refers to a message encoded with a particular key. Plaintext refers to the original, unencoded text. In this problem, both the ciphertext and the key are simply strings of upper-case characters. The ciphertext is generated from the plaintext by “adding” corresponding characters of the plaintext and the key together. If the plaintext is shorter than the key, only some of the key will be used. Similarly, if the plaintext is shorter than the key, the key will be used multiple times.

For example, to encode the plaintext “HELLO” with the key “CAT”:
Plaintext: HELLO
Key: CATCA
Ciphertext: KFFOP

And to encode the plaintext “DOG” with the key “FIDO”:
Plaintext: DOG
Key: FID
Ciphertext: JXK

To add two letters together, use the following convention: A=1, B=2, …, Z=26. If the sum of two letters is greater than 26, subtract 26 from the sum. For example: A + E = 1 + 5 = 6 = F, and D + X = 4 + 24 = 28 = 2 = B.

Given a ciphertext/key pair, determine the corresponding plaintext.

Input Format :
Input will consist of pairs of lines, with the first line being the ciphertext and the second line being the key. Both will consist of only uppercase letters.
Output Format:
For each ciphertext/key pair, print the corresponding plaintext message.

Example:
Sample Input:
HELLO
CAT

Sample Output:
KFFOP

Code:
  #include<stdio.h>
#include<string.h>
int main()
{
  char p[50]={0},key[50]={0};
    int x=0;
  char c[50]={0};
  int i,l1,l2;
  scanf("%s",p);
  scanf("%s",key);
  l1=strlen(p);
  l2=strlen(key);
  if(l1<=l2)
  {
    goto END;
  }
  else if(l1>l2)
  {
    for(i=l2;i<l1;i++)
    {
      key[i]=key[x];
      x++;
    }
    //printf("\n%s",key);
    goto END;
  } 
  END:
  for(i=0;i<l1;i++)
  {
    p[i]=(int)p[i]-64;
    key[i]=(int)key[i]-64;
    c[i]=p[i]+key[i];
    if(c[i]>26)
      c[i]=c[i]-26;
    c[i]=c[i]+64;
    printf("%c",c[i]);
  }
  return 0;
}
    
  

Fibonacci Roots


The first two terms in the Fibonacci sequence are 0 and 1, respectively, and each subsequent term is the sum of the previous two. Using this definition to calculate the first several terms in the sequence, we get

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Let us define the Fibonacci roots of a positive integer n to be the two smallest consecutive Fibonacci numbers whose sum is greater than or equal to n.

Input Format: 
Input consists of a single integer which corresponds to n.
Assumption: 
Assume that n is less than or equal to 2000.
Output Format: 
Output consists of integers, separated by a space.
Sample Input 1 : 
31
Sample Output 1: 
13 21
Sample Input 2 : 
89
Sample Output 2: 
34 55

Code:
  #include<stdio.h>
int main(){
  int a=0,b=1,c=0,n;
  scanf("%d",&n);
  while(1){
    c=a+b;
    if(c>=n)
      goto END;
    a=b;
    b=c;
  }
  END: printf("%d %d",a,b);
  return 0;
}

Vowel or Consonant


Write a program to determine whether the input character is a vowel or consonant.

Input and Output Format: 
Input consists of a single character. Output consists of a string --- “Vowel” / “Consonant” / “Not an alphabet”
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output 1: 
Enter a character
a
Vowel
Sample Input and Output 2: 
Enter a character
Z
Consonant

Sample Input and Output 3: 
Enter a character
#
Not an alphabet

Code:
  #include<stdio.h>
int main(){
  char in;
  char arr[11]={'a','e','i','o','u','A','E','O','U','I'};
    int i,flag=0,con=0;
  printf("Enter a character\n");
  scanf("%c",&in);
  if(((in>='a') && (in<='z'))||((in>='A')&&(in<='Z')))
  {
    for(i=0;i<10;i++)
    {
      if(in==arr[i]){
        flag++;
        break;
      }
      else
        con++;
    }
  }
  else 
    printf("Not an alphabet\n");
  
  if(con)
    printf("Consonant");
  if(flag)
    printf("Vowel");

  
  
  return 0;
}

Anagrams


Write a program to find whether the 2 given strings are anagrams or not. Anagrams are words or phrases made by mixing up the letters of other words or phrases,

Input and Output Format: 
Input consists of 2 string. Assume that all characters in the string are lowercase letters or spaces and the maximum length of the string is 100.
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output 1: 
Enter the first string
anitha
Enter the second string
amphisoft
anitha and amphisoft are not anagrams

Sample Input and Output 2: 
Enter the first string
the eyes
Enter the second string
they see the eyes and they see are anagrams

Code:
  #include<stdio.h>
#include<string.h>
int main(){
  char s1[200],s2[200];
  int first[26]={0},second[26]={0},c=0,i,flag=0;
    //int l,l1;
  printf("Enter the first string\n");
  gets(s1);
  printf("Enter the second string\n");
  gets(s2);
  
    while(s1[c]!='\0')
    {
      first[s1[c]-'a']++;
      c++;
    }
    c=0;
    while(s2[c]!='\0')
    {
      second[s2[c]-'a']++;
      c++;
    }
    for(i=0;i<26;i++){
      if(first[i]!=second[i])
        flag=1;
    }
      
  if(flag==0)
    printf("%s and %s are anagrams",s1,s2);
  else
    printf("%s and %s are not anagrams",s1,s2);
  
  return 0;
}

Zero Duplicates


Write a program that will read in a list of numbers and will then print out the same list except numbers that have already been printed will be printed as a zero instead.

Input Format: 
Input consists of n+1 integers where n is the number of elements in the list. The first integer corresponds to n. The remaining n integers correspond to the elements in the list. 
Assume that the maximum value of n is 100. 

Output Format: 
Output consists of n integers on separate lines.

Sample Input : 
5
2
3
4
3
6
Sample Output: 
2
3
4
6

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

The Next Palindrome


A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K, write the value of the smallest palindrome larger than K to output. 

Input 
The first line contains an integer, which corresponds to K. Assume that K is less than 200000. 

Output
Output consists of a single integer, which corresponds to the smallest palindrome larger than K.

Sample Input 1:
808

Sample Output 1:
818

Sample Input 2:
2133

Sample Output 2:
2222

Code:
  #include<stdio.h>
int main()
{
  long int num,pa=0,rem,temp;
  scanf("%ld",&num);
  while(num!=pa){
    num=num+1;
    temp=num;
    pa=0;
    while(temp!=0){
      rem=temp%10;
      pa=rem+pa*10;
      temp=temp/10;
    }
  }
  printf("%ld",pa);
  return 0;
}

TENNIS COURT


Many of the current final year students of a college are excellent tennis players. They always emerge as champions in the State Level Inter-Collegiate Tennis Meet. They want their juniors to carry on their legacy in the field of tennis and they started training their juniors in tennis. They were happy to see them play very well during their coaching sessions. They had a very comfortable win over one of the top teams in the game played at their college. But to their dismay, their juniors performed very poorly in the games conduced at other venues.

The think-tank of the College Tennis club was pondering over the reasons for their poor performance in the meets. David came up with a possible reason for the poor performance of their junior team. The tennis court in their college is a grass court but the tennis courts in most other competitive venues are synthetic courts. Their junior team has learnt playing tennis in the grass court and they found it very difficult to adapt their game to synthetic courts. The entire team agreed with this reason and they planned to request the chairman to convert the grass tennis court to synthetic tennis court.

They sent a request to the Chairman and he readily agreed but he asked to do it with minimal expenditure. He gave them square stones. 

The tennis court in the college is rectangular in shape of size n*m metres. We need to pave the court with square stones. The square stone is of size a*a. 

What is the least number of stones needed to pave the tennis court? It's allowed to cover the surface larger than the Tennis Court, but the court has to be covered. It's not allowed to break the stones. The sides of stones should be parallel to the sides of the court. 

Input Format: 
Input consists of 3 integers. The first integer corresponds to 'n', the length of the court. The second integer corresponds to 'm', the width of the court. The third integer corresponds to 'a', the side of the square stone. 

Output Format: 
Output consists of ta single integer k, where k is the least number of stones needed. 

Sample Input: 
 6 
Sample Output: 
4

Code:
  #include<stdio.h>
#include<math.h>
int main(){
  int l,b,la,ba,plate,a;
  scanf("%d",&l);
  scanf("%d",&b);
  scanf("%d",&a);
  la=round((float)l/a);
  ba=round((float)b/a);
  plate=la*ba;
  printf("%d",plate);
  return 0;
}
  

SMS Language


SMS language or textese (also known as txt-speak, txtese, chatspeak, txt, txtspk, txtk, txto, texting language, txt lingo, SMSish, txtslang,or txt talk) is a term for the abbreviations and slang commonly used with mobile phone text messaging. 

 Some of the abbreviations used are

  • s for yes
  • u for you
  • 2day for today
  • y for why

Many grandpa's have started sending SMSes to their grand children. But they are not familiar with the SMS lingo. 

Can you help them by writing a program that would convert a given text in proper English to SMS lingo? Consider only the 4 words listed above. 

Input Format: 
Input consists of a single string. Assume that the maximum length of the string is 200 and all letters are in lower-case. 

Output Format: 
Output consists of a single string. 

Sample Input 1: 
where were you yesterday? 

Sample Output 1: 
where were u yesterday? 

Sample Input 2: 
why is today a working day for you? 

Sample Output 2: 
y is 2day a working day for u?

Code:
  #include<stdio.h>
#include<string.h>
void replace(char *);
int main(){
  char s1[100];
  gets (s1);
  replace(s1);
  printf("\n%s",s1);
  return 0;
}
void replace(char *s1)
{
  char r[4][10]={"yes","you","today","why"};
    char rep[4][5]={"s","u","2day","y"};
      char buff[500];
  int i;
  char *ch;
  for(i=0;i<4;i++)
    if((ch=strstr(s1,r[i]))){
      strncpy(buff,s1,ch-s1);
      buff[ch-s1]=0;
      sprintf(buff+(ch - s1),"%s%s",rep[i],ch+strlen(r[i]));
      s1[0]=0;
      strcpy(s1,buff);
      return replace (s1);
    }
}

ENCODING


In this program you will be given a letter to encode. The difference here is that different rules are used for different letters and the counting process can cause you to wrap around the alphabet. Using the numerical value of each letter (A=1, B=2, ... Z= 26) the rules are as follows:


As an example if the letter to encode is a B, the B has a numerical value of 2 and encodes to a 4 and becomes a D, the 4th letter of the alphabet.
The G has a numerical value of 7. It encodes to a 5 and becomes an E.
The numerical value of Z is 26. Its largest factor is 13. You must count 156 (13*12) letters. This has the effect of wrapping around the alphabet 6 complete times and ending at Z. If a numerical value of zero is evaluated print a # symbol.
[Hint: ASCII value of A is 65].

INPUT: 
Input consists of an upper case letter.
OUTPUT: 
Print the encoded letter it produces.
SAMPLE INPUT 1
B
SAMPLE OUTPUT 1
D
SAMPLE INPUT 2
Z
SAMPLE OUTPUT 2
Z

Code:
  #include<stdio.h>
void prin(int);
int main(){
  unsigned char c,r;
  int k,r1=0,i=2;
  scanf("%c",&c);
  c=c-64;
  if(c>=1 && c<=5)
  k=1;
  else if(c>=6 && c<=10)
  k=2;
  else if(c>=11 && c<=15)
  k=3;
  else if(c>=16 && c<=20)
    k=4;
  else if(c>=21 && c<=26)
    k=5;
    
   switch(k)
  {
    case 1:
    r=c*2;
    break;
    case 2:
    r=(c%3)*5;
    break;
    case 3:
    r=(c%4)*8;
    break;
    case 4:
    r=c+10;
    break;
    case 5:
    {
      for(i=2;i<c;i++)
      {
        r1=c%i;
        if(r1==0)
          r=i*12;
      }
      break;
    }
  }
  
  if(r==0)
    r=-29;
  else if(r>26)
  {
    r=r%26;
    if(r==0)
      r=26;
  } 
  r=r+64;
  printf("%c",r);
  return 0;
}
    
        

Railway – Seating arrangement for Sleeper Class


Write a program to determine the type of berth when the seat/berth number in the train is given.
Input Format: 
Input consists of a single integer. Assume that the range of input is between 1 and 72.
Output Format: 
Output consists of a single string. [Upper or Middle or Lower or Side Lower or Side Upper]

Sample Input 1: 
9
Sample Output 1: 
Lower

Sample Input 2: 
72
Sample Output 2: 
Side Upper





Code:
  #include<stdio.h>
#include<string.h>
int main(){
  int n,k;
  scanf("%d",&n);
  k=n%8;
  if(k==1 || k==4)
    printf("Lower");
  if(k==2 || k==5)
    printf("Middle");
  if(k==3 || k==6)
    printf("Upper");
  if (k==0)
    printf("Side Upper");
  if(k==7)
    printf("Side Lower");
           
           
           
    return 0;
}
    

Sum of positive numbers


Write a program to find the sum of positive numbers in an array.

Input Format: 
Input consists of n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the array. Assume that the maximum value of n is 15.

Output Format: 
Refer sample output for details.
Sample Input :
5
2
3
6
8
-1
Sample Output : 
The sum of the positive numbers in the array is 19

Code:
  #include<stdio.h>
int main(){
  int n,a[20],i,sum=0;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&a[i]);
    if(a[i]>0)
      sum=sum+a[i];
  }
  printf("The sum of the positive numbers in the array is %d",sum);
  return 0;
}
  
  

Compatible Arrays


2 arrays are said to be compatible if they are of the same size and if the ith element in the first array is greater than or equal to the ith element in the second array for all i.Write a program to find whether 2 arrays are compatible or not.

Input Format: 
Input consists of 2n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the first array. The last 'n' integers correspond to the elements in the second array. Assume that the maximum value of n is 15.

Output Format: 
Refer sample output for details. Sample Input 1:
5
2
3
6
8
1
1
1
1
1
1

Sample Output 1:
Compatible

Sample Input 2: 
5
2
3
6
8
1
1
1
1
1
2

Sample Output 2:
Incompatible

Code:
  #include<stdio.h>
int main(){
  int n,a[20],b[20],i,flag=0;
  scanf("%d",&n);
  for(i=0;i<n;i++)
    scanf("%d",&a[i]);
  for(i=0;i<n;i++){
    scanf("%d",&b[i]);
    if(a[i]<b[i])
      flag=1;
  }
  if(flag)
    printf("Incompatible");
  else
    printf("Compatible");
  
  
  return 0;
}

Sum of 2 arrays


Write a program to find the sum of the corresponding elements in 2 arrays.

Input Format: 

Input consists of 2n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the first array. The last 'n' integers correspond to the elements in the second array. Assume that the maximum value of n is 15.

Output Format: 
Refer sample output for details.

Sample Input 1: 
 5
 2
 3
 6
 8
 1
 1
 1
 1
 1
 1

Sample Output 1: 
 3
 4
 7
 9
 2

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

Sum of array elements


Write a C program to find the sum of the elements in an array.

Input Format:

Input consists of n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the array. Assume that the maximum value of n is 15.

Output Format:
Refer sample output for details.

Sample Input 1: 
 5
 2
 3
 6
 8
 1

Sample Output 1: 
The sum of the elements in the array is 20

Code:

  #include<stdio.h>
int main(){
  int n,i,a[20],sum=0;
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
    sum=sum+a[i];
  }
  printf("The sum of the elements in the array is %d",sum);
  return 0;
}
  

Minimum Element in an Array


Write a program to find the minimum element in an array.


Input Format: 
Input consists of n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the array. Assume that the maximum value of n is 15.

Output Format: 
Refer sample output for details.

Sample Input 1: 
5
2
3
6
8
1

Sample Output 1: 
1 is the minimum element in the array

Code:
  #include<stdio.h>
int main(){
  int n, a[20], i,min=1;
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);  
  }
  min=a[0];
  for(i=1;i<n;i++)
  {
    if(a[i]<min)
      min=a[i];
  }
  printf("%d is the minimum element in the array",min);
  return 0;
}

Maximum Element in an Array


Sita has been promoted as a Team Leader and she has been shifted to Multimedia Team. As she needs to extensively work on audio, image and video signals, she is planning to spend a day in mastering the basics of 1-D and 2-D arrays. 
Can you please help her out?

Maximum Element in an Array

Write a program to find the maximum element in an array.

Input Format: 
Input consists of n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the array. Assume that the maximum value of n is 15.

Output Format: 
Refer sample output for details.

Sample Input 1: 
5
2
3
6
8
1

Sample Output 1: 
8 is the maximum element in the array


Code:

  #include<stdio.h>
int main(){
  int n,max=0,i,a[20];
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
    if(a[i]>max)
      max=a[i];
  }
  printf("%d is the maximum element in the array",max);
  return 0;
}

Sorted Order Check


Write a program to find whether the given array is sorted in ascending or descending order.

Input Format:
Input consists of n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the first array. Assume that the maximum value of n is 15.

Output Format:

Print yes if the array is sorted in ascending or descending order. Print no if the array is not sorted in ascending or descending order .

Sample Input 1: 
 5
 2
 3
 6
 8
 10

Sample Output 1: 
 yes

Sample Input 2: 
 5
 20
 13
 6
 4
 1

Sample Output 2: 
yes

Sample Input 2: 
5
20
13
6
4
10

Sample Output 3: 
 no

Code:
  #include<stdio.h>
int main(){
  int a[15],n,i,aflag=0,bflag=0;
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
    if(i>0){
      if((a[i-1]>a[i]))
        aflag++;
      else
        bflag++;
    }
  }
  if(aflag==(n-1)||(bflag==(n-1)))
    printf("yes");
  else
    printf("no");
  return 0;
}

Descending Order Check


Write a program to find whether the given array is sorted in descending order.

Input Format:
Input consists of n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the first array. Assume that the maximum value of n is 15.

Output Format: 
Print yes if the array is sorted in descending order. Print no if the array is not sorted in descending order.

Sample Input 1: 
 5
 2
 3
 6
 8
 1
 Sample Output 1: 
 no

Sample Input 2: 
 5
 20
 13
 6
 4
 1

 Sample Output 2: 
 yes

Code
  #include<stdio.h>
int main(){
  int a[15],i,flag=1;
  int n;
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
    if(i>0)
    {
      if(!(a[i-1]>a[i]))
        flag=0;
    }
  }
  if(flag)
    printf("yes");
  else 
    printf("no");
  return 0;
}
      

Ascending Order


Write a program to find whether the given array is sorted in ascending order.

Input Format: 
Input consists of n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the first array. Assume that the maximum value of n is 15.

Output Format: 
Print yes if the array is sorted in asecending order. Print no if the array is not sorted in ascending order.

Sample Input 1: 
 5
2
3
6
8
1

Sample Output 1: 
no

Sample Input 2: 
 5
 2
 3
 6
 8
10

Sample Output 2: 
 yes

Code
  #include<stdio.h>
int main(){
  int n,i,a[15],flag=1;
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  if(i>0)
  {
    if(!(a[i-1]<a[i]))
      flag=0;
  }
  }
  if (flag)
    printf("yes");
  else 
    printf("no");
  return 0;   
}

BUBBLE SORT


Write a C program to perform bubble sort on an array of n elements.

Input Format: 
Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next n integers correspond to the elements in the array.

Output Format: 
Refer sample output for formatting specs.

Sample Input and Output: 
[All text in bold corresponds to input and the rest corresponds to output]
Enter the number of elements :
5
Enter element 1
34
Enter element 2
23
Enter element 3
67
Enter element 4
34
Enter element 5
2
Unsorted list is :
34 23 67 34 2
After Pass 1 elements are :23 34 34 2 67
After Pass 2 elements are :23 34 2 34 67
After Pass 3 elements are :23 2 34 34 67
After Pass 4 elements are :2 23 34 34 67
Sorted list is :
2 23 34 34 67
 Note: stop the process if u find that the list is sorted in any intermediate point

Code:
  #include<stdio.h>
int main(){
  int n,a[20],i,temp,j,k,flag;
  printf("Enter the number of elements :\n");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    printf("Enter element %d\n",i+1);
    scanf("%d",&a[i]);
  }
  printf("Unsorted list is :\n");
  for(i=0;i<n;i++)
    printf("%d ",a[i]);
  for(i=0;i<n-1;i++)
  {
    flag=0;
    for(j=0;j<=n-2;j++)
    {
      if(a[j]>a[j+1])
      {
        temp=a[j];
        a[j]=a[j+1];
        a[j+1]=temp;
        flag++;
      }
    }
      printf("\nAfter Pass %d elements are :",i+1);
      for(k=0;k<n;k++)
        printf("%d ",a[k]);
    if(flag==0)
      break;
  }
  printf("\nSorted list is :\n");
  for(i=0;i<n;i++)
    printf("%d ",a[i]);
  return 0;
}
  

Insertion Sort


Write a C program to perform insertion sort on an array of n elements.

Input Format: 
Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next n integers correspond to the elements in the array.

Output Format: 
Refer sample output for formatting specs.

Sample Input and Output: 
[All text in bold corresponds to input and the rest corresponds to output]
Enter the number of elements in the array
5
Enter element 1
4
Enter element 2
2
Enter element 3
7
Enter element 4
3
Enter element 5
1
Insertion sort.
array before sorting:
4 2 7 3 1
After Iteration 1
2 4 7 3 1
After Iteration 2
2 4 7 3 1
After Iteration 3
2 3 4 7 1
After Iteration 4
1 2 3 4 7
array after sorting: 1 2 3 4 7

Code:

  #include<stdio.h>
void insertionsort(int *, int);
int main(void)
{
  int n, a[20],i;
  printf("Enter the number of elements in the array\n");
  scanf("%d",&n);
  for (i=0;i<n;++i)
  {
    printf("Enter element %d\n",i+1);
    scanf("%d",&a[i]);
  }
  printf("Insertion sort.\narray before sorting:\n");
  for (i=0;i<n;i++)
    printf("%d ",a[i]);
  insertionsort(a,n);
  return 0; 
}

void insertionsort(int *a, int size)
{
  int value, hole,i,j;
  for(i=1;i<size;++i)
  {
    value=a[i];
    hole=i-1;
    while(hole>=0 && a[hole]>value)
    {
      a[hole+1]=a[hole];
      hole=hole-1;
    }
    a[hole+1]=value;
    printf("\nAfter Iteration %d\n",i);
    for(j=0;j<size;j++)
      printf("%d ",a[j]);
}
  printf("\narray after sorting:\n");
  for(j=0;j<size;j++)
    printf("%d ",a[j]);
}

Implementation of Binary Search


Write a C program to implement Binary Search Algorithm.

Include a function
int BinarySearch (int, int, int *, int x) --- The 1st parameter is the lower limit of the list or array, the 2nd parameter is the upper limit of the list or array, the third parameter is a pointer to the array and the fourth parameter is the search element. 

Please note that the index of the search element is returned by the function. If the search element is not present in the array, -1 is returned. 
Assume that the maximum size of the array is 10 . Please note that if a is the array, then a[0] is in position 0, a[1] is in position 1 ... 

Input and Output Format: 
Refer sample input and output for formatting specifications.

Sample Input and Output 1:
[All text in bold corresponds to input and the rest corresponds to output.]
Enter the number of elements :
5
Enter the elements :
12
16
23
45
67
Enter the element to be searched :
23
The element 23 is in position 2

Sample Input and Output 2: 
[All text in bold corresponds to input and the rest corresponds to output.]
Enter the number of elements :
5
Enter the elements :
12
16
23
45
67
Enter the element to be searched :
28
The element 28 is not present in the array

Code:

  #include<stdio.h>
int BinarySearch(int, int ,int *, int);
int main(){
  int first=0, last; 
  int a[20],search,s=0,n,i;
  //int j,temp=0;
  printf("Enter the number of elements :\n");
  scanf("%d",&n);
  printf("Enter the elements :\n");
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  printf("Enter the element to be searched :\n");
  scanf("%d",&search);
  last=n-1;
  
  s=BinarySearch(first, last, a, search);
  if(s>0){
  printf("The element %d is in position %d",search,s);
  }
  else{
  printf("The element %d is not present in the array",search);
  }
  return 0;
}

int BinarySearch(int l, int h, int *a, int x)
{
  int mid;
  while(l<=h){
    mid=(l+h)/2;
    if (x==a[mid])
      return mid;
  else if(x<a[mid])
    h=mid-1;
  else if(x>a[mid])
    l=mid+1;
    }
return -1;
}

Monday 14 March 2016

SELECTION SORT


Write a C program to perform selection sort on an array of n elements. 
Selection sort algorithm starts by comparing first two elements of an array and swapping if necessary, i.e., if you want to sort the elements of array in ascending order and if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, leave the elements as it is. Then, again first element and third element are compared and swapped if necessary. This process goes on until first and last element of an array is compared. This completes the first step of selection sort.(Refer below diagram ....)

Input Format: 
Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next n integers correspond to the elements in the array.

Output Format: 
Refer sample output for formatting specs.

Sample Input and Output1: 
[All text in bold corresponds to input and the rest corresponds to output]
Enter the number of elements in the array
6
Enter element 1
2
Enter element 2
7
Enter element 3
3
Enter element 4
8
Enter element 5
5
Enter element 6
1
Selection sort.
array before sorting:
2 7 3 8 5 1
After Iteration 1
1 7 3 8 5 2
After Iteration 2
1 2 7 8 5 3
After Iteration 3
1 2 3 8 7 5
After Iteration 4
1 2 3 5 8 7
After Iteration 5
1 2 3 5 7 8
After Iteration 6
1 2 3 5 7 8
array after sorting:
1 2 3 5 7 8

Sample Input and Output2: 
[All text in bold corresponds to input and the rest corresponds to output]
Enter the number of elements in the array
5
Enter element 1
13
Enter element 2
9
Enter element 3
7
Enter element 4
4
Enter element 5
2
Selection sort.
array before sorting:
 13 9 7 4 2
 After Iteration 1
 2 13 9 7 4
 After Iteration 2
 2 4 13 9 7
 After Iteration 3
 2 4 7 13 9
 After Iteration 4
 2 4 7 9 13
 After Iteration 5
 2 4 7 9 13
 array after sorting:
 2 4 7 9 13

Code:
  #include<stdio.h>
int main(){
  int a[20],n,i;
  int j,temp=0,k;
  printf("Enter the number of elements in the array\n");
  scanf("%d",&n);
  for(i=1;i<=n;i++)
  {
    printf("Enter element %d\n",i);
    scanf("%d",&a[i]);
  }
  printf("Selection sort.\n");
  printf("array before sorting:\n");
  for(i=1;i<=n;i++)
  {
    printf("%d ",a[i]);
  }
  for(i=1;i<=n;i++)
  {
    for(j=i+1;j<=n;j++)
    {
      if(a[i]>a[j])
      {
        temp=a[i];
        a[i]=a[j];
        a[j]=temp;
      }
    }
    printf("\nAfter Iteration %d\n",i);
      for(k=1;k<=n;k++)
      {
        printf("%d ",a[k]);
      }
  }
  printf("\narray after sorting:\n");
  for(i=1;i<=n;i++)
  {
    printf("%d ",a[i]);
  }
  return 0;
}

Searching an Array


Write a C program to search for an element ‘a’ in the array. (Linear Search) 

Input Format: 
Input consists of n+2 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the array. The last integer corresponds to ‘a’, the element to be searched. 
Assume that the maximum size of the array is 20. 

Output Format: 
Refer sample output for details. 
Sample Input 1: 
3

Sample Output 1: 
 6 is present in the array 

Sample Input 2: 
 5 
 2 
 3 
 6 
 8 
 1 
 9 
 Sample Output 2:
 9 is not present in the array

Code:
  #include<stdio.h>
int main(void){
  int a[20], find, size,i;
  scanf("%d",&size);
  for(i=1;i<=size;i++)
  {
    scanf("%d",&a[i]);
  }
  scanf("%d",&find);
  for(i=0;i<size;i++)
  {
    if(a[i]==find){
    printf("%d is present in the array",find);
      break;
    }
  }
    if(i==size)
    {
      printf("%d is not present in the array",find);
    }
  return 0;
}
    
  
  

File : Count Character


Write a program to count the number of times a character appears in the File. (Case insensitive... 'a' and 'A' are considered to be the same)

Input and Output Format:
Refer sample input and output for formatting specifications. All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output: 
Enter the file name test.txt
Enter the character to be counted
r
File 'test.txt' has 99 instances of letter 'r'.

Code:
  #include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main(void)
{
  FILE *fptr=NULL;
  char c[2],get;
  int ch;
  int count=0;
  char filename[100];
  printf("Enter the file name\n");
  scanf("%19s",filename);
  fptr=fopen(filename,"r");
  printf("Enter the character to be counted\n");
  scanf(" %s",c);
  
  if(fptr==NULL)
  {
    exit(-1);
  }
  
  if(((int)c[0]>=65)&&((int)c[0]<=122))
  {
    if(((int)c[0]>=97)&&((int)c[0]<=122))
      ch=(int)c[0]-32;
    else
      ch=(int)c[0]+32;
  }
  
  do{
    get=fgetc(fptr);
    if(((int)get==ch)||get==c[0])
      count=count+1;
  }while(get!=EOF);
  
      
  printf("File '%s' has %d instances of letter '%s'.",filename,count,c);
  fclose(fptr);
  return 0;
}

File Copy


Write a program to copy from one file to another.

Input and Output Format: 
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output: 
Enter the input file name
testInput.txt 
Enter the output file name
testOutput.txt

Code:
  #include<stdio.h>
int main(){
  FILE *fp1, *fp2;
  char fname1[50],fname2[50],ch;
  printf("Enter the input file name\n");
  scanf("%s",fname1);
  printf("Enter the output file name\n");
  scanf("%s",fname2);
  fp1=fopen(fname1,"r");
  fp2=fopen(fname2,"w");
  
  while((ch=getc(fp1))!=EOF)
  {
    putc(ch,fp2);
  }
  fclose(fp1);
  fclose(fp2);
    
   
    return 0;
}

Distance between 2 points


Write a program to find the distance between 2 points using functions.

Function specification: 
float findDistance(int x1, int y1, int x2, int y2) The function accepts 4 integer and returns a float. 

Input Format: 
Input consists of 4 integers. The first and second integer corresponds to the x-coordinate and y-coordinate of the first point. The third and fourth integer corresponds to the x-coordinate and y-coordinate of the second point.

Output Format: 
Output consists of a single floating point number (correct to 2 decimal places.) Refer sample output for formatting details.

Sample Input: 
3
6
4
3

Sample Output: 
Distance between the 2 points is 3.16

Code:
  #include<stdio.h>
#include<math.h>
float findDistance(int,int,int,int);
int main()
{
  int x1,x2,y1,y2;
  float r=0;
  scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
  
  r=findDistance(x1,y1,x2,y2);
  printf("Distance between the 2 points is %.2f",(float)r );
  return 0;
}
float findDistance(int x1, int y1, int x2, int y2)
{
  float d;
  d=sqrt((pow(x2-x1,2))+(pow(y2-y1,2)));
  return d;
}

Factorial of a number


Write a program to find the factorial of a number using functions.

Function Specification:
int factorial(int n)
The function accepts a int and returns an int.

Input Format: 
Input consists of 1 integer.

Output Format:
Output consists of a single integer. Refer sample output for formatting details.

Sample Input: 
3
Sample Output: 
6

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

Functions – Lucky String


Write a program to find whether the given string is Lucky or not.

A string is said to be lucky if the sum of the ascii values of the characters in the string is even.

Function specifications: 

int checkLucky(char * a) The function accepts a pointer to a string and returns an int.
The return value is 1 if the string is lucky and 0 otherwise.

Input and Output Format: 
Input consists of a string. Assume that all characters in the string are lowercase letters and the maximum length of the string is 100.
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output 1: 
Enter the input string
anitha
anitha is not lucky

Sample Input and Output 2: 
Enter the input string
technology
technology is lucky

Code:
  #include<stdio.h>
#include<string.h>
int checkLucky(char *a);
int main(){
  char s[100],c;
  printf("Enter the input string\n");
  scanf("%s",s);
  c=checkLucky(s);
  if(c==1)
    printf("%s is lucky",s);
  else if(c==0)
    printf("%s is not lucky",s);
  return 0;
}
int checkLucky(char *a)
{
  char *name;
  int i,sum=0,j;
  name=a;
  i=strlen(a);
  for(j=0;j<i;j++)
  {
    sum=sum+name[j];
  }
  if((sum%2)==0)
    return 1;
  else 
    return 0; 
}
  
  
  

Pattern 10


Write a program to print the given pattern.

Input Format: 
Input consists of a single integer.

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

Sample Input: 
5
Sample Output:



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

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

Pattern 9


Write a program to print the given pattern.

Input Format: 
Input consists of a single integer.

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

Sample Input: 
5
Sample Output:
5 5 5 5 5
4 4 4 4 
3 3 3
2 2 
1

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

Pattern 8


Write a program to print the given pattern.

Input Format: 
Input consists of a single integer.

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

Sample Input: 
5

Sample Output:



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


Code:

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

Pattern 7


Write a program to print the given pattern.

Input Format: 
Input consists of a single integer.

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

Sample Input:
5


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


Code:


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

Pattern 6


Write a program to print the given pattern.

Input Format:
Input consists of a single integer.

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

Sample Input:
5


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


code:

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

Pattern 5


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

Sample Output:



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


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

Pattern 4


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

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

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

Pattern 3


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

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


Code:

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

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;
}

Valid


A number is said to be valid iff it is divisible by 8.
Write a C program that allows the user to keep entering numbers as long as the input is valid and also displays a count of the valid numbers entered using a while loop.

Input Format: 
Input consists of integers.

Output Format: 
Refer Sample Input and Output for formatting specifications.

[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output: 
Enter the number
8
Enter the number
16
Enter the number
96
Enter the number
6
The number of valid numbers entered is 3

Code:
  #include<stdio.h>
int main(){
  int n,count=0;;
  while(1){
  printf("Enter the number\n");
  scanf("%d",&n);
    if(n%8==0)
    {      count++;
    }else
    {
      printf("The number of valid numbers entered is %d",count);
      break;
    }
  }
  return 0;
}
    
  

Multiplication Table


Write a program to print the multiplication table of an integer n upto m rows using a while loop. 

Input Format: 
Input consists of 2 integers. The first integer corresponds to n. The second integer corresponds to m.

Output Format: 
Refer Sample Input and Output for formatting specifications. 

[All text in bold corresponds to input and the rest corresponds to output] 
Sample Input and Output: 
Enter n 
Enter m 
The multiplication table of 5 is 
1*5=5 
2*5=10 
3*5=15 
4*5=20

Code:
  #include<stdio.h>
int main(){
  int m,n,i=1;
  printf("Enter n\n");
  scanf("%d",&n);
  printf("Enter m\n");
  scanf("%d",&m);
  printf("The multiplication table of %d is\n",n);
  while(i<=m)
  {
    
    printf("%d*%d=%d\n",i,n,(i*n));
    
    i++;
  }
  return 0;
}

    
  
  

Count positive and negative


Write a C program to that allows the user to enter 'n' numbers and finds the number of positive numbers entered and the number of negative numbers entered using a while loop.

Input Format: 
Input consists of n+1 integers. The first integer corresponds to n. The next n integers correspond to the numbers to be added. Consider 0 to be a positive number.

Output Format: 
Refer Sample Input and Output for formatting specifications.

[All text in bold corresponds to input and the rest corresponds to output]
Sample Input and Output: 
Enter the value of n
4
Enter the number
5
Enter the number
-2
Enter the number
-1
Enter the number 6
Number of positive numbers entered is 2 and the number of negative numbers entered is 2

Code:
  #include<stdio.h>
int main(){
  int n,i=1,b,count=0,count1=0;
  printf("Enter the value of n\n");
  scanf("%d",&n);
  while(i<=n)
  {
    printf("Enter the number\n");
    scanf("%d",&b);
    if(b>=0)
      count++;
    else
      count1++;
    i++;
  }
  printf("Number of positive numbers entered is %d and the number of negative numbers entered is %d",count,count1);
  return 0;
}

    
  
  

Sum of 'n' positive numbers


Write a C program to that allows the user to enter 'n' numbers and finds the number of positive numbers entered and the sum of all positive numbers entered using a while loop.

Input Format:
Input consists of n+1 integers. The first integer corresponds to n. The next n integers correspond to the numbers to be added. Consider 0 to be a positive number.

Output Format: 
Refer Sample Input and Output for formatting specifications.

[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output: 
Enter the value of n
4
Enter the number
5
Enter the number
-2
Enter the number
-1
Enter the number
6
Number of positive numbers entered is 2 and the sum is 11

Code:
  #include<stdio.h>
int main(){
  int n,sum=0,count=0,i=1,b;
  printf("Enter the value of n\n");
  scanf("%d",&n);
  while(i<=n)
  {
    printf("Enter the number\n");
    scanf("%d",&b);
    i++;
    if(b>=0)
    {
      count++;
      sum=sum+b;
    }
  }
  printf("Number of positive numbers entered is %d and the sum is %d",count,sum);
  return 0;
}

Sum of 'n' numbers


Write a C program to find the sum of 'n' numbers using a while loop.

Input Format: 
Input consists of n+1 integers. The first integer corresponds to n. The next n integers correspond to the numbers to be added.

Output Format: 
Refer Sample Input and Output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output: 
Enter the value of n
4
Enter the number
5
Enter the number
2
Enter the number
1
Enter the number
6
The sum is 14

Code:
  #include<stdio.h>
int main(){
  int n,i=1,sum=0,nsum=0;
  printf("Enter the value of n\n");
  scanf("%d",&n);
  while(i<=n)
  {
    printf("Enter the number\n");
    scanf("%d",&nsum);
    sum=sum+nsum;
    i++;
  }
  printf("The sum is %d",sum);
  return 0;
}
  

Print 3


Write a C program to print all numbers between a and b (a and b inclusive) using while loops.

Input Format: 
Input consists of 2 integers. The first integer corresponds to a and the second integer corresponds to b.

Output Format: 
Refer Sample Input and Output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output 1: 
Enter the value of a
10
Enter the value of b
4
10
9
8
7
6
5
4

Sample Input and Output 2: 
Enter the value of a
4
Enter the value of b
10
4
5
6
7
8
9
10

Code:
  #include<stdio.h>
int main()
{
  int a,b;
  printf("Enter the value of a\n");
  scanf("%d",&a);
  printf("Enter the value of b\n");
  scanf("%d",&b);
  while(1)
  {
    if(a>b){
      printf("%d\n",a);
      --a;}
    else if(a<b){
      printf("%d\n",a);
      ++a;}
    else if(a==b){
      printf("%d\n",a);
    break;
    }
      
    
  }
  return 0;
}

Print 2


Write a C program to print all numbers between a and b (a and b inclusive) using a while loop.

Input Format: 
Input consists of 2 integers. The first integer corresponds to a and the second integer corresponds to b. Assume a>=b.

Output Format: 
Refer Sample Input and Output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output: 
Enter the value of a
10
Enter the value of b
4
10
9
8
7
6
5
4

Code:
  #include<stdio.h>
int main(){
    int a,b;
    printf("Enter the value of a\n");
    scanf("%d",&a);
    printf("Enter the value of b\n");
    scanf("%d",&b);
    while(b<=a)
    {
      printf("%d\n",a);
      a--;
    }
    return 0;
  }