Structured Programming Language C: LU Mid-Term Exam - 2010

Question no. 1

  1. What is Programming? Define Compiler.
  2. Write a program to calculate the following:
    F = a*b-b*c-c2*(b-1)/3
  3. Which of the following are valid/invalid names for variables? If invalid, explain why?
    1. _abc
    2. 9abc
    3. A098
    4. abc
  4. Write the difference between WHILE and DO-WHILE loop.

Question no. 2

  1. Write a for loop to print all the odd numbers from 122 to 10000.
  2. Write the output of the following code:
    int main()
    {
        int n=5, i, j;
        for(i=5; i>=1; i--)
        {
            for(j=1; j<=i; j=j+2) { printf("%d",j); }
            printf("\n");
        }
    }
    
  3. Given N, write a program to output the following:
    1234...(N-1)N
    1234...(N-1)
    ......
    12
    1
    

Question no. 3

  1. What is an Array? Why do we need array?
  2. Write a program to take 1000 numbers as input. Print the numbers in reverse order.
  3. Write a program to find MAXIMUM between 100 numbers.

Question no. 4

  1. Write a program to find MINIMUM between 2 numbers.
  2. Write a program, which will take MARK as input and show grade as output.
    Grading system:
    80 or above A+
    70 or above A
    60 or above A-
    Below 60 F
    
  3. Write the program to print all numbers from 22 to 2000 EXCEPT the multiples of 8.




Answer no. 1

Answer the question no. 1(a)

Programming:

A programming language is an artificial language designed to express computations that can be performed by a machine, particularly a computer. Programming languages can be used to create programs that control the behaviour of a machine, to express algorithoms precisely, or as a mode of a human communication.


Many programming languages have some form of written specification of their syntax (form) and semantics (meaning). Some languages are defined by a specification document. For example, the C programming language is specified by an ISO standard.

Compiler:

A compiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code). The most common reason for wanting to transform source cade is to create an executable program.



Answer the question no. 1(b)

#include‹stdio.h›

main()
{
 int a, b, c;
 printf("Enter values of a, b, and c: ");
 scanf("%d%d%d",&a,&b,&c);
 
 float sum = a*b-b*c-(c*c*(b-1))/3.0;
 
 printf("\nF = %f",sum);
}



Answer the question no. 1(c)
(i) _abc (iii) A098 and (iv) abc ; these variables are valid.
But the variable (ii) 9abc is invalid; because a variable name could not start with a number.



Answer the question no. 1(d)

Difference between while and do-while loop:

There is a minor difference between the working of while and do-while loops. This difference is the place where the condition is tested. The while tests the condition before executing any of the statements within the while loop.


As against this, the do-while tests the condition after having executed the statements within the loop.



Answer no. 2

Answer the question no. 2(a)

#include‹stdio.h›

main()
{
 for(int i=122; i<=10000; i++)
 {
  if(i%2==0) continue;
  else printf("\n%d",i);
 }
}



Answer the question no. 2(b)

135
13
13
1
1



Answer the question no. 2(c)

#include‹stdio.h›

main()
{
 int N;
 printf("Enter value of N: ");
 scanf("%d",&N);
 
 while(N>=1)
 {
  for(int i=1; i<=N; i++)
   printf("%d",i);
  printf("\n");
  N--;
 }
}



Answer no. 3

Answer the question no. 3(a)

Array:

An array is a collection of similar elements. These similar elements could be all ints, or all floats, or all chars, etc. Usually, the array of characters is called simply an array.


The all elements of any given array must be of the same type. i.e. we cannot have an array of 10 numbers, of which 5 are ints and 5 are floats.

Needs of Array:

We need array because instead of having to separately store related information in different variable, we can store them as a collection in just one variable. It is more efficient for a program to access and process the information in an array then it is to deal with many separate variables.



Answer the question no. 3(b)

#include‹stdio.h›

main()
{
 int p[1001];
 
 int i;
 for(i=0; i<1000; i++)
  scanf("%d",&p[i]);
 for(i=i-1; i>=0; i--)
  printf("%d",p[i]);
}



Answer the question no. 3(c)

#include‹stdio.h›

main()
{
 int i, p, max;
 
 for(i=0; i<100; i++)
 {
  scanf("%d",&p);
  
  if(i==0) max=p;
  else if(p>max) max=p;
 }
 printf("\nMaximum number = %d",max);
}



Answer no. 4

Answer the question no. 4(a)

#include‹stdio.h›

main()
{
 int a, b;
 printf("\nEnter two numbers: ");
 scanf("%d%d",&a,&b);
 
 if(a‹b) printf("Minimum number = %d",a);
 else printf("Minimum number = %d",b);
}



Answer the question no. 4(b)

#include‹stdio.h›

main()
{
 int m;
 printf("\nPlease enter your mark: ");
 scanf("%d",&m);
 
 if(m>=80) printf("\nYour Grade is A+");
 else if(m>=70 && m<80) printf("\nYour Grade is A");
 else if(m>=60 && m<70) printf("\nYour Grade is A-");
 else printf("\nYour Grade is F");
}



Answer the question no. 4(c)

#include‹stdio.h›

main()
{
 for(int i=22; i<=2000; i++)
 {
  if(i%8==0) continue;
  else printf("\n%d",i);
 }
}

Comments

Popular posts from this blog

C++ :: Topological Sort Algorithm (using DFS)

How to Hack Facebook Account

C++ :: Strongly Connected Components Algorithm (SCC)