P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS DEPARTMENT OF SCIENCE AND HUMANITIES EVEN SEMESTER FEB 2017

Similar documents
P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

PESIT Bangalore South Campus Hosur Road (1km before Electronic City), Bengaluru Department of Basic Science and Humanities

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

Operators and Expressions:

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Informatics Ingeniería en Electrónica y Automática Industrial

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Assignment: 1. (Unit-1 Flowchart and Algorithm)

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Flow Chart. The diagrammatic representation shows a solution to a given problem.

Computer System and programming in C

Fundamentals of Programming

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

DEPARTMENT OF MATHS, MJ COLLEGE

Computers Programming Course 5. Iulian Năstac

Fundamental of Programming (C)

C Programming Multiple. Choice

Programming and Data Structures

C: How to Program. Week /Mar/05

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Basics of Programming

The Hyderabad Public School, Begumpet, Hyderabad, A.P

C Programming

Data types, variables, constants

Fundamental of C programming. - Ompal Singh

2. Numbers In, Numbers Out

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

UNIT- 3 Introduction to C++

JAVA OPERATORS GENERAL

Data Types and Variables in C language

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

MODULE 2: Branching and Looping

Question Bank (SPA SEM II)

SHARDA UNIVERSITY SCHOOL OF ENGINEERING & TECHNOLOGY Mid Term Examination, (Odd Term, ) SOLUTION

Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations. Practical 1.

Prepared by: Shraddha Modi

UNIT 3 OPERATORS. [Marks- 12]

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Reserved Words and Identifiers

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

Subject: Fundamental of Computer Programming 2068

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

2. Numbers In, Numbers Out

BRANCHING if-else statements

Programming for Engineers Introduction to C

Additional Questions

Q 1. Attempt any TEN of the following:

Chapter 2 - Introduction to C Programming

A flow chart is a graphical or symbolic representation of a process.

Advantages of writing algorithm

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

C Programming Class I

Preview from Notesale.co.uk Page 6 of 52

Operators And Expressions

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Conditional Expressions

Operators in java Operator operands.

ET156 Introduction to C Programming

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

'C' Programming Language

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Lecture 02 C FUNDAMENTALS

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Engineering 12 - Spring, 1999

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

BCA-105 C Language What is C? History of C

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Computers Programming Course 6. Iulian Năstac

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

1.3b Type Conversion

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Creating a C++ Program

Unit 3. Operators. School of Science and Technology INTRODUCTION

3. Types of Algorithmic and Program Instructions

Unit-2 (Operators) ANAND KR.SRIVASTAVA

PROGRAMMING IN C AND C++:

Questions Bank. 14) State any four advantages of using flow-chart

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Computers Programming Course 7. Iulian Năstac

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Department of Computer Science

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

3. Java - Language Constructs I

Transcription:

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS DEPARTMENT OF SCIENCE AND HUMANITIES ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS) EVEN SEMESTER FEB 07 FACULTY: Dr.J Surya Prasad/Ms. Saritha/Mr. Sreenath M.V/Ms.Swathi G SUBJECT / CODE: Programming in C and Data Structures- 5PCD3 PART a Write an algorithm to compute Simple Interest that takes three inputs as p,t,r. Ans Algorithm:To compute simple interest Step:start Step:Read p,t,r Step3:compute SI<-p*t*r Step:Display SI Step5:stop b What is variable? Explain the rules for constructing variables in C-Language. Give examples for valid and invalid variables. Ans A variable is an identifier whose value can be changed during execution of the program. In other words, a variable is a name given to a memory-location where the data can be stored. Using the variable-name, the data can be stored in a memory-location and accessed or manipulated Rules for defining a variable ) The first character in the variable should be a letter or an underscore ) The first character can be followed by letters or digits or underscore 3) No extra symbols are allowed (other than letters, digits and underscore) ) Length of a variable can be up to a maximum of 3 characters 5) Keywords should not be used as variable-names Valid variables: a, principle_amount, sum_of_digits Invalid variables: 3fact //violates rule sum= sum-of-digits 6$ //violates rule 3 for int if //violates rule 5 A What are data types? Mention the different data types supported by C Language giving an example to each.

Ans The data type defines the type of data stored in a memory-location. C supports 3 classes of data types: ) Primary data type ex: int, float,char,double,void,double ) Derived data types For ex: array C supports 5 primary data types: ) int An int is a keyword which is used to define integers. Using int keyword, the programmer can inform the compiler that the data associated with this keyword should be treated as integer. C supports 3 different sizes of integer: short int int long int ) float A float is a keyword which is used to define floating point numbers. 3) double A double is a keyword used to define long floating point numbers. ) char A char is a keyword which is used to define single character. 5) void void is an empty data type. Since no value is associated with this data type, it does not occupy any space in the memory. This is normally used in functions to indicate that the function does not return any value. Range of data types Data type Bytes Range of data type Char bytes -8 to 7 Int bytes -3, 768 to 3,767 Float bytes 3.E-38 to 3.E38 Double 8 bytes.7e-308 to.7e308 B Draw a flowchart to find the largest of three numbers. Ans PART

3 A Ans What is the output of the following Code fragment? int a,b; a=b=50; printf( %0d%d,a,b); a=a*b; b=b/; printf( %0d%d,a,b); Ans 0050 50500 5 b What do you mean by type conversion? Explain the two types of conversion with example. Ans Type conversion is used to convert data of one type to data of another type. Type conversion is of types as shown in below above: Implicit Conversion If a compiler converts one type of data into another type of data automatically, it is known as implicit conversions. There is no data loss in implicit conversion. The conversion always takes place from lower rank to higher rank. For ex, int to float as shown in the above datatype hierarchy. For ex: int a =, b=; float c = a; //c becomes.000000 float d=b/c=/.000000=.000000/.000000=0.500000 If one operand type is same as other operand type, no conversion takes place and type of result remains same as the operands i.e. int+int=int float+float=float Conversion rules are as follows: If either operand is long double, convert the other to long double. Otherwise, if either operand is double, convert the other to double. Otherwise, if either operand is float, convert the other to float. Otherwise, convert char and short to int. Then, if either operand is long, convert the other to long.

EXPLICIT CONVERSION When the data of one type is converted explicitly to another type with the help of some predefined functions, it is called as explicit conversion. There may be data loss in this process because the conversion is forceful. The syntax is shown below: data_type v; data_type v= (data_type) v; where v can be expression or variable For ex: float b=.000000; int c = ; float d=b/(float)c=.000000/.000000=0.500000 a Find the values of a, b, c, d, e in the following code segment. int a=0,b=3,c;(take the original values of a and b for every expression) ) c=(a==3)&& b++; ) a+=b--; 3) d=a++ + b++; ) e=++a - --b; Ans C=0 * A=3 D=3 E=- b Write a program to check whether a given number is even or odd without using modulus operator. Ans #include<stdio.h> Main() Int a; Printf( enter anumber\n ); Scanf( %d,&a); If((a&)==0) Printf( Even ); Else Printf( Odd ); PART 3 5 a What is the purpose of a printf() statement? Explain the formatted printf () along with the respective examples. Ans The printf function does following tasks: Accept a series of arguments Apply to each argument a format-specifier contained in the format-string Output the formatted data to the screen The syntax is shown below: n=printf("format-string", variable-list); where format-string contains one or more formatspecifiers variable-list contains names of variables

For ex: n=printf("%d %f %c", x, y, z ); b Write a C program that computes the size of int, float, double and char variables. Ans #include<stdio.h> * Main() Printf( size of int is %d bytes,sizeof(int)); Printf( size of float is %d bytes,sizeof(float)); Printf( size of char is %d bytes,sizeof(char)); Printf( size of double is %d bytes,sizeof(double)); 6 a What is an Expression? Explain bitwise operators with example Ans An expression is combination of operands and operator that reduces to a single value. For ex: Consider the following expression a+b here a and b are operands while + is an operator These operators are used to perform logical operation (and, or, not) on individual bits of a binary number. There are 6 bitwise operators: Operators Meaning of operators & Bitwise AND Bitwise OR ^ Bitwise exclusive OR ~ Bitwise complement << Shift left >> Shift right Truth Table A B A&B A B A^B ~A 0 0 0 0 0 0 0 0 0 0 0 Ex for ~ (bitwise complement) Ex for & (bitwise AND) a = 3 0000 0 a = 3 0000 0 ~a= 000 b = 6 0000 00 a & b 0000 000 Ex for (bitwise OR) Ex for ^ (bitwise xor) a = 3 0000 0 a = 3 0000 0 b = 6 0000 00 b = 6 0000 00 a b 0000 a ^ b 0000 0 The operator that is used to shift the data by a specified number of bit positions towards left or right is called shift operator. The syntax is shown below for << The syntax is shown below for >> b=a << num; b=a >> num; where a is value to be shifted num is number of bits to be shifted Ex for <<(left shift): Ex for >>(right shift): a = 3 b=a<< 0000 0 000 00 a = 3 b=a<< 0000 0 0000 00

b Write a program to swap two integers without using third variable Ans #include<stdio.h> #include<conio.h> Main() Int a,b; Printf( enter numbers\n ); Scanf( %d%d,&a,&b); Printf( before swapping a=%d\nb=%d\n,a,b); A=a+b; B=a-b; A=a-b; Printf( After swapping a=%d\nb=%d\n,a,b); PART 7 a What is the output of this C code? i) #include<stdio.h> int main() int x = 0; if (x++) printf("true\n"); if (x == ) printf("false\n"); ii) #include<stdio.h> int main() int x=0; if(x==) if(x==0) printf( inside if\n ); printf( inside if\n ); printf( inside \n ); Ans False Inside * b Write a program to check whether the given input is an alphabet or not. int main() char c; printf("enter a character: "); scanf("%c",&c); if( (c>='a' && c<='z') (c>='a' && c<='z')) printf("%c is an alphabet.",c); printf("%c is not an alphabet.",c); return 0; 8 a Write a C program to read a year as input and find whether it is a leap year or not. also consider end of the centuries using Nested if.

int main() int year; printf("enter a year: "); scanf("%d",&year); if(year% == 0) if( year%00 == 0) // year is divisible by 00, hence the year is a leap year if ( year%00 == 0) printf("%d is a leap year.", year); printf("%d is not a leap year.", year); printf("%d is a leap year.", year ); printf("%d is not a leap year.", year); 3 return 0; b Explain Precedence and associativity with example. Ans Precedence Table THE PRECEDENCE OF OPERATORS

9 A Ans The order in which different operators are used to evaluate an expression is called precedence of operators. Associativity:the order in which operator is evaluated Left-right Right-Left PART 5 Write a C program to simulate a simple calculator that performs arithmetic operations like addition, subtraction, multiplication, and division only on integers. Error message should be reported, if any attempt is made to divide by zero. (Using switch statement) int main() char operator; double firstnumber,secondnumber; printf("enter an Expression: "); scanf("%lf %c %lf",&firstnumber, &operator,&secondnumber); switch(operator) case '+': printf("%.lf + %.lf = %.lf",firstnumber, secondnumber, firstnumber + secondnumber); break; case '-': printf("%.lf - %.lf = %.lf",firstnumber, secondnumber, firstnumber - secondnumber); break; case '*': printf("%.lf * %.lf = %.lf",firstnumber, secondnumber, firstnumber * secondnumber); break; case '/': printf("%.lf / %.lf = %.lf",firstnumber, secondnumber, firstnumber / firstnumber); break; 8 6 // operator doesn't match any case constant (+, -, *, /) default: printf("error! operator is not correct"); return 0; 0 A Design and develop a flowchart that takes three coefficients (a, b, and c) of a Quadratic equation 8

Ans (ax +bx+c=0) as input and compute all possible roots. Implement a C program for the developed flowchart and execute the same to output the possible roots for a given set of coefficients with appropriate messages. #include <stdio.h> #include <math.h> int main() double a, b, c, determinant, root,root, realpart, imaginarypart; printf("enter coefficients a, b and c: "); scanf("%lf %lf %lf",&a, &b, &c); determinant = b*b-*a*c; // condition for real and different roots if (determinant > 0) // sqrt() function returns square root root = (-b+sqrt(determinant))/(*a); root = (-b-sqrt(determinant))/(*a); printf("root = %.lf and root = %.lf",root, root); //condition for real and equal roots if (determinant == 0) root = root = -b/(*a); printf("root = root = %.lf;", root); // if roots are not real realpart = -b/(*a); imaginarypart = sqrt(-determinant)/(*a); printf("root = %.lf+%.lfi and root = %.f-%.fi", realpart, imaginarypart, realpart, imaginarypart); ` return 0;