Search This Blog

Friday, September 6, 2019

pl/sql conditional statements

PL/SQl introduction

The DBMS_OUTPUT is a built-in package that enables you to display output, debugging information, and send messages from PL/SQL blocks, subprograms, packages, and triggers.


DECLARE
   <declarations section>
BEGIN
   <executable command(s)>
EXCEPTION
   <exception handling>
END;


Note:

1.Every PL/SQL statement ends with a semicolon (;).

2.PL/SQL blocks can be nested within other PL/SQL blocks using BEGIN and END.

3.The end; line signals the end of the PL/SQL block. To run the code from the SQL command line, you may need to type / at the beginning of the first blank line after the last line of the code.

4.PL/SQL allows the nesting of blocks, i.e., each program block may contain another inner
block. If a variable is declared within an inner block, it is not accessible to the outer block.
However, if a variable is declared and accessible to an outer block, it is also accessible to
all nested inner blocks




A PL/SQL unit is any one of the following:
? PL/SQL block
? Function
? Package
? Package body
? Procedure
? Trigger
? Type
? Type body







PL/SQL Character Data Types and Subtypes
Following is the detail of PL/SQL pre-defined character data types and their sub-types:
Data Type Description
CHAR         Fixed-length character string with maximum size of 32,767 bytes
VARCHAR2    Variable-length character string with maximum size of 32,767 bytes
RAW        Variable-length binary or byte string with maximum size of 32,767 bytes, not         interpreted by PL/SQL
NCHAR        Fixed-length national character string with maximum size of 32,767 bytes
NVARCHAR2     Variable-length national character string with maximum size of 32,767 bytes
LONG         Variable-length character string with maximum size of 32,760 bytes       
LONG RAW    Variable-length binary or byte string with maximum size of 32,760 bytes, not         interpreted by PL/SQL
ROWID         Physical row identifier, the address of a row in an ordinary table
UROWID         Universal row identifier (physical, logical, or foreign row identifier)


DECLARE
message varchar2(30):= 'Data Base Management System';
BEGIN
dbms_output.put_line(message);
END;
/


Variable Declaration in PL/SQL

The syntax for declaring a variable is:

variable_name [CONSTANT] datatype [NOT NULL] [:= | DEFAULT initial_value]



sales number(10, 2);
pi CONSTANT double precision := 3.1415;
name varchar2(25);
address varchar2(100);


BEGIN
dbms_output.put_line( 10 + 5);
dbms_output.put_line( 10 - 5);
dbms_output.put_line( 10 * 5);
dbms_output.put_line( 10 / 5);
dbms_output.put_line( 10 ** 5);
END;
/



DECLARE
a integer := 10;
b integer := 20;
c integer;
f real;
BEGIN
c := a + b;
dbms_output.put_line('Value of c: ' || c);
f := 70.0/3.0;
dbms_output.put_line('Value of f: ' || f);
END;
/




DECLARE
-- Global variables
num1 number := 95;
num2 number := 85;
BEGIN
dbms_output.put_line('Outer Variable num1: ' || num1);
dbms_output.put_line('Outer Variable num2: ' || num2);
    DECLARE
    -- Local variables
    num1 number := 195;
    num2 number := 185;
    BEGIN
        dbms_output.put_line('Inner Variable num1: ' || num1);
    dbms_output.put_line('Inner Variable num2: ' || num2);
    END;
END;
/





DECLARE
   a number(2) := 10;
BEGIN
   a:= 10;
  -- check the boolean condition using if statement 
   IF( a < 20 ) THEN
      -- if condition is true then print the following  
      dbms_output.put_line('a is less than 20 ' );
   END IF;
   dbms_output.put_line('value of a is : ' || a);
END;
/



DECLARE
a number (2) := 21;
b number (2) := 10;
BEGIN
IF (a = b) then
dbms_output.put_line('Line 1 - a is equal to b');
ELSE
dbms_output.put_line('Line 2 - a is not equal to b');
END IF;
END;
/





DECLARE
   a number(3) := 100;
BEGIN
   -- check the boolean condition using if statement 
   IF( a < 20 ) THEN
      -- if condition is true then print the following  
      dbms_output.put_line('a is less than 20 ' );
   ELSE
      dbms_output.put_line('a is not less than 20 ' );
   END IF;
   dbms_output.put_line('value of a is : ' || a);
END;
/


IF-THEN-ELSIF Statement


DECLARE
   a number(3) := 100;
BEGIN
   IF ( a = 10 ) THEN
      dbms_output.put_line('Value of a is 10' );
   ELSIF ( a = 20 ) THEN
      dbms_output.put_line('Value of a is 20' );
   ELSIF ( a = 30 ) THEN
      dbms_output.put_line('Value of a is 30' );
   ELSE
       dbms_output.put_line('None of the values is matching');
   END IF;
   dbms_output.put_line('Exact value of a is: '|| a ); 
END;
/



DECLARE
   grade char(1) := 'A';
BEGIN
   CASE grade
      when 'A' then dbms_output.put_line('Excellent');
      when 'B' then dbms_output.put_line('Very good');
      when 'C' then dbms_output.put_line('Well done');
      when 'D' then dbms_output.put_line('You passed');
      when 'F' then dbms_output.put_line('Better try again');
      else dbms_output.put_line('No such grade');
   END CASE;
END;
/




DECLARE
   grade char(1) := 'B';
BEGIN
   case 
      when grade = 'A' then dbms_output.put_line('Excellent');
      when grade = 'B' then dbms_output.put_line('Very good');
      when grade = 'C' then dbms_output.put_line('Well done');
      when grade = 'D' then dbms_output.put_line('You passed');
      when grade = 'F' then dbms_output.put_line('Better try again');
      else dbms_output.put_line('No such grade');
   end case;
END;
/




DECLARE
   a number(3) := 100;
   b number(3) := 200;
BEGIN
   -- check the boolean condition 
   IF( a = 100 ) THEN
   -- if condition is true then check the following 
      IF( b = 200 ) THEN
      -- if condition is true then print the following 
      dbms_output.put_line('Value of a is 100 and b is 200' );
      END IF;
   END IF;
   dbms_output.put_line('Exact value of a is : ' || a );
   dbms_output.put_line('Exact value of b is : ' || b );
END;
/



Wednesday, August 21, 2019

day 2



PROBLM NO :1
An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
Write a program to find all Armstrong number in the range of 0 and 999.


PROBLM NO :2
Project predictor
In the three-field system the sequence of field use involved an autumn planting of grain (wheat, barley or rye) and a spring planting of peas, beans, oats or barley. The third was left fallow, in order to allow the soil of that field to regain its nutrients.

The 3 durations(4 months each) are,
Duration 1: 1-4 (all inclusive)
Duration 2: 5-8 (all inclusive)
Duration 3: 9-12 (all inclusive)

The 2 crops that were used are,
Crop 1: Winter Wheat
Crop 2: Beans

The initial crops in three fields at month 1are,
Field 1: Winter Wheat
Field 2: Beans
Field 3: Left Fallow

Given the month number and field number, write a program to print the crop name.

Input Format:
The first input is an integer corresponds to month number.
The second input is an integer corresponds to field number.

Output Format:
The output is the string.

Sample Input 1:
5
2
Sample Output 1:
Winter Wheat

Sample Input 2:
2
3
Sample Output 2:
Left Fallow

Sample Input 3:
2
2
Sample Output 3:
Beans
PROBLEM NO : 3
Crop cultivation strategy
 
If a crop is grown for once, the fertility of the soil reduces by 30. After cultivation, if the land is left free for one month, the fertility increases by a factor of 2. If the fertility becomes 0, the crop cannot be grown further. Write a program to get the initial fertility and get the number of months the land is left free after every cultivation and find the number of times the crops are successfully grown, before the fertility becomes 0.

Note 1: If the fertility becomes 0 in the middle of the growth of crop, the crop stops growing.
Note 2: Stop getting the input if the fertility becomes 0.

Input Format:
First input is an integer that corresponds to the initial fertility of the soil.
Next inputs are number of months the land is left free after every cultivation.
Output Format:
Number of times the crops are grown successfully.

Sample Input:
35
3
1
Sample Output:
2


Explanation:
35->after first cultivation fertility become 5
3-> after 3 months the fertility becomes 40( 5*2 = 10, 10*2 = 20, 20*2 = 40); After second cultivation the fertility becomes 10
1-> after 1 month the fertility becomes 20; In the middle of the crop growth the fertility becomes 0, so stop.
So the total number of successful cultivations = 2.

day 1

Wednesday, August 14, 2019

Consider the following information about a university database:
Professors have an SSN, a name, an age, a rank, and a research specialty.

Projects have a project number, a sponsor name (e.g., NSF), a starting date, an
ending date, and a budget.

Graduate students have an SSN, a name, an age, and a degree program (e.g., M.S.
or Ph.D.).

Each project is managed by one professor (known as the project’s principal investigator).

Each project is worked on by one or more professors (known as the project’s
co-investigators).

Professors can manage and/or work on multiple projects.

Each project is worked on by one or more graduate students (known as the
project’s research assistants).

When graduate students work on a project, a professor must supervise their work
on the project. Graduate students can work on multiple projects, in which case
they will have a (potentially different) supervisor for each one.

Departments have a department number, a department name, and a main office.

Departments have a professor (known as the chairman) who runs the department.

Professors work in one or more departments, and for each department that they
work in, a time percentage is associated with their job.

Graduate students have one major department in which they are working on their
degree.

Each graduate student has another, more senior graduate student (known as a
student advisor) who advises him or her on what courses to take.




Monday, July 29, 2019

Use case view


  • Image result for use case diagram for atm Indian government had decided that all information related to the airport should be organized using automation, and you have been hired to design the system. For this the relevant information is as follows:

1.      Every airplane has a registration number, and each airplane is of a specific model.
2.      The airport accommodates a number of airplanes models, and each model is identified by a model number and has a capacity and a weight.
3.      The number of technicians works at the airport. You need to store the name, SSN, address, phone number and a salary of each technician.
4.      Each technician is an expert on one or more plane model(s), and his or her expertise may overlap with that of other technicians. This information about technicians must also be recorded.
5.      Traffic controllers must have an annual medical examination. For each traffic controller, you must store the date of the most recent exam.
6.      All airport employees belong to a union. You must store the union membership number of each employee. You can assume that each employee is uniquely identified by a social security number.
7.      The airport has a number of tests that are used periodically to ensure that airplanes are still airworthy. Each test has a Indian Aviation Administration (IAA) test number, a name, and a maximum possible score.
8.      The IAA requires the airport to keep track of each time a given airplane is tested by a given technician using a given test. Foe each testing event the information needed is the date, the number of hours the technicians spent doing the test, and the score the airplane received on the test.

  • You are appointed to design International movie data base system. The system stores and manipulates information about movies, casts (actors or actresses), crews, studios, awards, cinemas, news, etc. The following gives the requirements for the IMDB.

a.       IMDB records the information about each studio, such as studio name, year established, year closed (if applicable), country, etc The system records overall movie information, such as movie title, tagline, genre, year made, country, website, running time, language, colour, rating, showing start date, ranking, etc.
b.      Tagline is a one-sentence description of a movie. Values of colour could be “True” or “False” for colour movie or black/white movie respectively. And Rating could be “G”, “PG”, “M”, “MA”, etc. Studios produce movies.
c.       The genre describes the type of a movie, such as “comedy”, “drama”, “biography”, “action”, “thriller”, “horror”, “romance”, “war”, “animation”, “adventure”, etc. One movie may have more than one genre.You may record the people involved with the movies with their titles, family names, given names, genders, websites, emails, dates of birth, cities of birth, countries of birth, and other necessary information.
d.      A casts is actor or actress in a movie. Crews are staff other than casts involved with a movie, such as “Director”.  You should record each role’s name for casts. For example, “Actor of leading role”, “Actor of supporting role”, etc.
e.       You should record each job title for crews. For example, “Director”, “Producer”, “Writer”, etc. You may assume a person only performs one role in a movie. IMDB records the name of each part in a movie, also records the job title for each crew in a movie. There are many movie awards around the world from different organization. The organization is recorded with name and country.  Each organization holds one award ceremony in each year.
f.       You should record the award and nominations with title, category, etc. Award titles could be classified as to two categories: for person (such as “Best Actor of leading role”, “Best Director”) or for movie “Best movie”, etc. There will be many nominations for an award in a given year, but only one winner for the award in that year. An award may been won many times, or may have never been awarded (or nominated)

Thursday, March 28, 2019

Data Sets

 Weather



@relation weather

@attribute matchno numeric

@attribute humidity numeric

@attribute temperature numeric

@attribute weathercondition{sunny,cloudy}

@attribute occuranceofrain{y,n,maybe}

@attribute possibilityofmatch{y,n,maybe}

@data   

1,29,39,sunny,n,y

2,23,35,sunny,n,y

3,12,20,cloudy,y,n

4,22,25,cloudy,maybe,maybe

5,17,23,cloudy,y,n

6,30,45,sunny,n,y

7,15,24,cloudy,maybe,maybe

8,20,29,cloudy,maybe,maybe

9,30,35,sunny,n,y

10,29,35,sunny,n,y

11,29,24,sunny,n,y

12,26,30,sunny,n,y

13,22,24,cloudy,maybe,maybe

14,23,24,cloudy,maybe,maybe

15,30,38,sunny,n,y

16,29,30,sunny,n,y

17,19,21,cloudy,y,n

18,22,24,cloudy,n,y

19,28,30,sunny,n,y

20,31,37,sunny,n,y


 employee







@relation employee
@attribute eid numeric
@attribute ename string
@attribute age {20-29,>30,<22}
@attribute income{20000-30000,>30000,<40000}
@attribute buys{YES,NO}
@data
101,"A",20-29,20000-30000,YES
102,"B",<22,>30000,NO
103,"C",>30,<40000,NO
104,"D",20-29,20000-30000,YES
105,"E",20-29,>30000,NO
106,"F",<22,<40000,NO
107,"G",<22,>30000,NO
108,"H",>30,20000-30000,NO
109,"I",20-29,<40000,NO
110,"J",<22,<40000,NO
111,"K",20-29,20000-30000,NO
112,"L",<22,20000-30000,NO
113,"M",20-29,20000-30000,NO
114,"N",20-29,20000-30000,YES
115,"O",<22,<40000,NO
116,"P",20-29,20000-30000,YES
117,"Q",<22,<40000,NO
118,"R",>30,20000-30000,NO
119,"S",20-29,20000-30000,YES
120,"T",<22,<40000,NO








labour

@relation labour
@attribute lid numeric
@attribute lname string
@attribute age {30-39,45-50}
@attribute workinghours {6-8,8-10}
@attribute income {20000-30000,30000-40000}
@data
101001,"A",30-39,8-10,30000-40000
101002,"B",45-50,6-8,20000-30000
10103,"C",30-39,8-10,30000-40000
10104,"D",45-50,6-8,20000-30000
10105,"E",30-39,8-10,30000-40000
10106,"F",45-50,6-8,20000-30000
10107,"G",45-50,6-8,20000-30000
10108,"H",30-39,8-10,30000-40000
10109,"I",30-39,8-10,30000-40000
101010,"J",45-50,6-8,20000-30000
101011,"K",30-39,8-10,30000-40000
101012,"L",45-50,6-8,20000-30000
101013,"M",45-50,6-8,20000-30000
101014,"N",30-39,8-10,30000-40000
101015,"O",45-50,6-8,20000-30000
101016,"P",45-50,6-8,20000-30000
101017,"Q",30-39,8-10,30000-40000
101018,"R",45-50,6-8,20000-30000
101019,"S",45-50,6-8,20000-30000
101020,"T",30-39,8-10,30000-40000

 student


@relation student
@attribute sid numeric
@attribute name string
@attribute age numeric
@attribute branch {IT}
@attribute percentage {70-80,80-90,90-100}
@attribute grade {C,B,A}
@data
101,"A",19,IT,70-80,C
102,"B",19,IT,90-100,A
103,"C",20,IT,90-100,A
104,"D",18,IT,70-80,C
105,"E",19,IT,80-90,B
106,"F",20,IT,80-90,B
107,"G",20,IT,70-80,C
108,"H",20,IT,80-90,B
109,"I",19,IT,90-100,A
110,"J",18,IT,70-80,C
111,"K",18,IT,80-90,B
112,"L",20,IT,70-80,C
113,"M",19,IT,80-90,B
114,"N",19,IT,80-90,B
115,"O",20,IT,90-100,A
116,"P",20,IT,70-80,C
117,"Q",19,IT,90-100,A
118,"R",20,IT,80-90,B
119,"S",20,IT,90-100,A
120,"T",20,IT,70-80,C














Friday, March 22, 2019

Design LALR Bottom up Parser using YACC.

Design LALR Bottom up Parser.

<parser.l>
%{
#include<stdio.h> #include "y.tab.h"
%}
%%
[0-9]+ {yylval.dval=atof(yytext); return DIGIT;
}
\n|. return yytext[0];
%%
<parser.y>
%{
/*This YACC specification file generates the LALR parser for the program considered in experiment 4.*/
#include<stdio.h>
%}
%union
{
double dval;
}
%token <dval> DIGIT
%type <dval> expr
%type <dval> term
%type <dval> factor
%%
line: expr '\n' { printf("%g\n",$1);
}
;
expr: expr '+' term {$$=$1 + $3 ;}
| term
;
term: term '*' factor {$$=$1 * $3 ;}
| factor
;
factor: '(' expr ')' {$$=$2 ;}
| DIGIT
 
;
%%
int main()
{
yyparse();
}
yyerror(char *s)
{
printf("%s",s);
}
Output:
$lex parser.l
$yacc –d parser.y
$cc lex.yy.c y.tab.c –ll –lm
$./a.out 2+3
5.0000





click here to download

Implement the Lexical Analyzer Using Lex Tool.




/* program name is lexp.l */
%{
/* program to recognize a c program */ int COMMENT=0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
#.* { printf("\n%s is a PREPROCESSOR DIRECTIVE",yytext);} int |
float | char | double | while | for |
do | if |
break | continue | void | switch | case | long | struct | const | typedef | return | else |
goto {printf("\n\t%s is a KEYWORD",yytext);} "/*" {COMMENT = 1;}
/*{printf("\n\n\t%s is a COMMENT\n",yytext);}*/

"*/" {COMMENT = 0;}
/* printf("\n\n\t%s is a COMMENT\n",yytext);}*/
{identifier}\( {if(!COMMENT)printf("\n\nFUNCTION\n\t%s",yytext);}
\{ {if(!COMMENT) printf("\n BLOCK BEGINS");}
\} {if(!COMMENT) printf("\n BLOCK ENDS");}
{identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}
\".*\" {if(!COMMENT) printf("\n\t%s is a STRING",yytext);}
[0-9]+ {if(!COMMENT) printf("\n\t%s is a NUMBER",yytext);}
\)(\;)? {if(!COMMENT) printf("\n\t");ECHO;printf("\n");}
\( ECHO;
= {if(!COMMENT)printf("\n\t%s is an ASSIGNMENT OPERATOR",yytext);}
\<= |
\>= |
\< |
== |
\> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}
%%
int main(int argc,char **argv)
{
if (argc > 1)
{
FILE *file;
file = fopen(argv[1],"r"); if(!file)
{
printf("could not open %s \n",argv[1]); exit(0);
}
yyin = file;
}
yylex(); printf("\n\n"); return 0;
} int yywrap()
{
return 0;
}

Input:
$vi var.c #include<stdio.h> main()
{
int a,b;
}

Output:
$lex lex.l
$cc lex.yy.c
$./a.out var.c
#include<stdio.h> is a PREPROCESSOR DIRECTIVE FUNCTION
main (
)
BLOCK BEGINS
int is a KEYWORD a IDENTIFIER
b IDENTIFIER BLOCK ENDS

Saturday, March 16, 2019

sample lex

/*lex program to count number of words*/
%{
#include<stdio.h>
#include<string.h>
int i = 0;
%}

/* Rules Section*/
%%
([a-zA-Z0-9])* {i++;} /* Rule for counting
number of words*/

"\n" {printf("%d\n", i); i = 0;}
%%

int yywrap(void){}

int main()
{
// The function that starts the analysis
yylex();

return 0;
}

Monday, February 18, 2019

Problem Design




1.      Indian government had decided that all information related to the airport should be organized using automation, and you have been hired to design the system. For this the relevant information is as follows:
ü  Every airplane has a registration number, and each airplane is of a specific model.
ü  The airport accommodates a number of airplanes models, and each model is identified by a model number and has a capacity and a weight.
ü  The number of technicians works at the airport. You need to store the name, SSN, address, phone number and a salary of each technician.
ü  Each technician is an expert on one or more plane model(s), and his or her expertise may overlap with that of other technicians. This information about technicians must also be recorded.
ü  Traffic controllers must have an annual medical examination. For each traffic controller, you must store the date of the most recent exam.
ü  All airport employees belong to a union. You must store the union membership number of each employee. You can assume that each employee is uniquely identified by a social security number.
ü  The airport has a number of tests that are used periodically to ensure that airplanes are still airworthy. Each test has a Indian Aviation Administration (IAA) test number, a name, and a maximum possible score.
ü  The IAA requires the airport to keep track of each time a given airplane is tested by a given technician using a given test. Foe each testing event the information needed is the date, the number of hours the technicians spent doing the test, and the score the airplane received on the test.
2. You are appointed to design International movie data base system. The system stores and manipulates information about movies, casts (actors or actresses), crews, studios, awards, cinemas, news, etc. The following gives the rDesign the problemequirements for the IMDB.
ü  IMDB records the information about each studio, such as studio name, year established, year closed (if applicable), country, etc The system records overall movie information, such as movie title, tagline, genre, year made, country, website, running time, language, colour, rating, showing start date, ranking, etc.
ü  Tagline is a one-sentence description of a movie. Values of colour could be “True” or “False” for colour movie or black/white movie respectively. And Rating could be “G”, “PG”, “M”, “MA”, etc. Studios produce movies.
ü  The genre describes the type of a movie, such as “comedy”, “drama”, “biography”, “action”, “thriller”, “horror”, “romance”, “war”, “animation”, “adventure”, etc. One movie may have more than one genre.You may record the people involved with the movies with their titles, family names, given names, genders, websites, emails, dates of birth, cities of birth, countries of birth, and other necessary information.
ü  A casts is actor or actress in a movie. Crews are staff other than casts involved with a movie, such as “Director”.  You should record each role’s name for casts. For example, “Actor of leading role”, “Actor of supporting role”, etc.
ü  You should record each job title for crews. For example, “Director”, “Producer”, “Writer”, etc. You may assume a person only performs one role in a movie. IMDB records the name of each part in a movie, also records the job title for each crew in a movie. There are many movie awards around the world from different organization. The organization is recorded with name and country.  Each organization holds one award ceremony in each year.
ü  You should record the award and nominations with title, category, etc. Award titles could be classified as to two categories: for person (such as “Best Actor of leading role”, “Best Director”) or for movie “Best movie”, etc. There will be many nominations for an award in a given year, but only one winner for the award in that year. An award may been won many times, or may have never been awarded (or nominated)

Monday, January 28, 2019


1. What is the output of this C code?



#include <stdio.h>

    void main()

    {

        int x = 4, y, z;

        y = --x;

        z = x--;

        printf("%d%d%d", x,  y, z);

    }



Ans : 2  3  3







2.What is the output of this C code?





 #include <stdio.h>

    int main()

    {

        switch (printf("Do"))

        {

        case 1:

            printf("First\n");

            break;

        case 2:

            printf("Second\n");

            break;                                                     

        default:

            printf("Default\n");

            break;

        }

    }



/Ans: DoSecond





3.What is the output of this C code?



#include <stdio.h>

    int main()

    {

        int a = 10, b = 10;

        if (a = 5)

        b--;

        printf("%d, %d", a, b--);

    }



Ans: a=5  b=9





4.What is the output of this C code?

#include <stdio.h>

    int main()

    {

        int a = 1, b = 1, c;

        c = a++ + b;

        printf("%d, %d", a, b);

    }




Ans: a=2  b=1







5.#include "stdio.h"

int main()

{

 int _ = 18;

 int __ = 38;

 int ___;

 ___ = _ + __;

 printf ("%i", ___);

 return 0;

}



Answer : 56





6.What will be printed as the result of the operation below:main()

{

 int x = 41, y = 43;

 x = y++ + x++;

 y = ++y + ++x;

 printf ("%d %d", x , y);

}

Answer : 86 130

Description : Its actually compiler dependent. After x = y++ + x++, the value of x becomes 85 and y becomes 44, And y = ++y + ++x will be computed as y = (44) + (86). After computation y becomes 130.





7.What will be printed as the result of the operation main()

{

 int x = 7;

 printf ("%d, %d, %d", x,

    x<<5, x>>5);

}





Answer : 7, 224, 0

Description : As x = 7 so first %d gives 7, second %d will take value of x after left shifting it five times, and shifting is done after converting the values to binary, binary value of 7 (000111) will be left shifted twice to make it binary 224(11100000), so x<<5 is 224 and as left shifting does not effect the original value of x its still 5 so third %d will also show 0.









8.What will be the output?



main()



{

if (1, 0)

 printf ("True");

else

 printf ("False");

}



Answer : False

Description :comma(,) operator returns the value which at the right hand side of , and thus if statement become if(0).







9.what is the output ?



#include<stdio.h>

int main()

{

 char arr[5] = "World is beautiful";

 printf ("%s", arr);

 return 0;

}



Answer : World

A warning is also printed “4:19: warning: initializer-string for array of chars is too long [enabled by default]”

Description : Size of any character array cannot be less than the number of characters in any string which it has assigned. Size of an array can be equal (excluding null character) or greater than but never less than.







10.What is the output of following program?



#include<stdio.h>

void main()

{

 int a = 2;

 switch (a)

 {

  case 4: printf ("A");

  break;

  case 3: printf ("B");

  default : printf("C");

  case 1 : printf ("D");

  break;

  case 5 : printf ("E");

 }

}





Answer : CD

Description : In switch statement default should be at mentioned after all the switch cases. In this case, it gets executed in between and all cases after default are executed before a break statement.





11.What is the output?#include<stdio.h>

#define SQR( x ) ( x * x )

int main()

{

 int b = 5;

 int a = SQR(b+2);

 printf("%d\n", a);

 return 0;

}

Answer : 17









12.#include <stdio.h>



int main()

{

int arr[] = {};

printf("%d", sizeof(arr));

return 0;

}







13.Which one of the following is incorrect?

A. enum fruits = { apple, banana }f;

B. enum fruits{ apple, banana }f ;

C. enum fruits{ apple, banana };

D. enum f{ apple, banana };





14.What will be the output of the C program?



#include<stdio.h>

int main(){

 float me = 5.25;

 double you = 5.25;

 if(me == you)

  printf("I love U");

 else

  printf("I hate U");

 return 0;

}

A. Compilation error



B. I love U

C. Runtime error

D. I hate U



Option: D

Explanation

For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.



15.

DATA MINING

In the University Examinations conducted during the past 5 years, the toppers registration numbers were  7126, 82417914, 7687 and 6657. Your father is an expert in data mining and he could easily infer a pattern in the toppers registration numbers. In all the registration numbers listed here, the sum of the odd digits is equal to the sum of the even digits in the number. He termed the numbers that satisfy this property as Probable Topper Numbers.

Write a program to find whether a given number is a probable topper number or not.




Tuesday, December 11, 2018

string belongs to the given grammar or not

C Program for implementation of  language given below
E-> TE’
 E’-> +TE’ | epsilon
T-> FT’
T’-> *FT’  | epsilon
 F-> (E) | i







#include<stdio.h> #include<conio.h> #include<string.h> #include<process.h> void e(); void e1(); void t(); void t1(); void f(); int ip=0; static char s[10]; void main() { char k; int i; ip=0; clrscr(); printf("enter the string:\n "); scanf("%s",s); printf("the string is : %s\n",s); e(); if(s[ip]=='$') printf("String is accepted "); getch(); } void e() { t(); e1(); return; } void t() { f(); t1(); // return; } void e1() { if(s[ip]=='+') { ip++; t(); e1(); } return; } void t1() { if(s[ip]=='*') { ip++; f(); t1(); } return; } void f() { if(s[ip]=='(') { ip++; e(); if(s[ip]==')') ip++; else printf("error closed paranthesis expected"); } else if(s[ip]=='i') ip++; else printf("id expected "); return; }

Thursday, December 6, 2018

Identifying the given number is valid number or not


#include <stdio.h>
#include<string.h>
int main()
{
    char a[500];
    int i,l,c=1,k=0;
    scanf("%s",a);
    l=strlen(a);
    if(a[0]=='+'||a[0]=='-'||(a[0]>='0'&&a[0]<='9'))
    {
        for(i=1;i<l;i++)
        {
            if(a[i]=='.')
            {
             k++;
             if(k>1)
             {
             printf("invalid");
             return 0;
             }
            }
            else if((a[i]>='0'&&a[i]<='9'))
            c++;
            else
            break;
        }
    if(c==l-1)
    printf("valid");
    else
    printf("invalid");
    }
    else
    printf("invalid");
    return 0;
}

Wednesday, November 14, 2018

problem 10

Crop cultivation strategy 
 
If a crop is grown for once, the fertility of the soil reduces by 30. After cultivation, if the land is left free for one month, the fertility increases by a factor of 2. If the fertility becomes 0, the crop cannot be grown futher. Write a program to get the initial fertility and get the number of months the land is left free after every cultivation and find the number of times the crops are successfully grown, before the fertility becomes 0. 
  
Note 1: If the fertility becomes 0 in the middle of the growth of crop, the crop stops growing. 
Note 2: Stop getting the input if the fertility becomes 0. 

Input Format: 
First input is an integer that corresponds to the initial fertility of the soil. 
Next inputs are number of months the land is left free after every cultivation. 
Output Format: 
Number of times the crops are grown successfully. 
  
Sample Input: 
35 
3 
1 
Sample Output: 
2 
  
  
Explanation: 
35->after first cultivation fertility become 5 
3-> after 3 months the fertility becomes 40( 5*2 = 10, 10*2 = 20, 20*2 = 40); After second cultivation the fertility becomes 10 
1-> after 1 month the fertility becomes 20; In the middle of the crop growth the fertility becomes 0, so stop. 
So the total number of successful cultivations = 2.

Hackathon on Problem Solving Skills using C & Data Structures

  File to access link to upload your work