Breaking News
You are here: Home / Paper Solutions / SEM-2 / C Programming / C Programming Paper solution APRIL/MAY 2011

C Programming Paper solution APRIL/MAY 2011

C Programming Paper solution APRIL/MAY 2011

Q1 answer the following questions

1. what is keyword?

Answer: keywords are the predefind words in programming language. Every keywords has its own specific meaning and purpose. It is known as reserve words.

 

2. What is difference between= and == ?

Answer:

= = =
– it is a Assignment operators – it is a relational operators
– it used in variable Assignment to – it used in compare to two variable
value
– example: a=5,b=7 – example: a==b

 

3. Differentiate between control string of input and output statements.

INPUT OUTPUT
– we shall explore all of the options -the printf statement provides certain features that
that  are  available  for  reading can be effectively exploited to control the alignment
formatted data with scanf function. and spacing of print-outs on the terminals.
-the general form of scanf is – the general form of printf is
scanf(“control string”,arg1, scanf(“control string”,arg1, arg2…argn);
arg2…argn);

 

4.Explain bit wise operator.

Answer: ‘c’ has a distinction of supporting special operators. Known as bit wise operators for manipulation of data at bit leval. This operators are used for testing the bits or shifting them right or left. Bitwise operators may not apply to float or double.

operators meaning
& Bit wise AND
| Bit wise OR
^ Bit wise exclusives OR
<< Shift left
>> Shift right

 

5. How can you define enumerated, data constant ?

Answer: An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name of the (optional) enumeration tag and defines the set of named integer identifiers (called the “enumeration set,” “enumerator constants,” “enumerators,” or “members”). A variable with enumeration type stores one of the values of the enumeration set defined by that type.

 

Syntax: enum-specifier:

enum identifier opt { enumerator-list }

enum identifier

to deifne enumrated data constant just declare the values of data items directly in the enum declaration.

 

Eg.

#include<stdio.h>
#include<conio.h> 

void main()
{

int c;

enum e{sunday=10,monday=13,tuesday=200}; 
clrscr();

enum e a; 
c=tuesday; 
printf("%d",c); 
getch();
}

 

6.What is significance of variable declaration ?

Answer:

Variables are simply names used to refer to some location in memory – a location that holds a value with which we are working.

Variables are useful for assigning values. They are used for storing information.

All variables in C are typed. That is, every variable declared must be assigned as a certain type of variable.

So different data are stored in different variables.

 

 

7.Differentiate & and && operator.

Answer:

& operator && operator
& is a bitwise operation, && is a boolean operation.
& is bitwise operator && is logical operator
Eg. 2 && 3 evaluates to true Eg. 2 & 3 gives result as 2(010 & 011)

 

Q2

(a) Explain NULL statement. Explain its usefulness.

Answer:

“why we need a terminating null character ?”.

a string is not a data type in C, but it is considered a data structure stored in an array.

the string is a variable-length structure and is stored in a fixed-length array.

 

the array size is not always the size of the string and most often it is much larger than string stored in it.

 

therefore, the last element of the array need not represent the end of the string data and the NULL character serves as the “end-of-string” marker.

 

(b) How can you define constant in ‘C’ ? Explain its use.

Answer:

Constant refer to fixed values that do not change during the execution of the program.

there are several types of constants as following illustared.

c programming constants

 

 

Integer constant:

An integer constant refers to a sequence of digit. There are three types of integers, namely, decimal integer, octal integer, hexadecimal integer.

 

decimal integers consists of a set of digit, 0 through 9, preceded by an optional – or + sign. valid examples are decimal integer constant are:

 -321 0 6 54321 +78

 

embedded spaces, commas, and non-digit character are not permitted between digits for example,

 15 750  20,000, $1000

an octal integer constant consists of any combination of digits from the set 0 through 7, with a leading 0, some example of octal integer are;

 

0 0435 0551

 

A sequence of digits preceded by ox or OX is considered as hexadecimal integer. They may also include alphabets A through F or a through f. the letter A through F represent the numbers 10 through 15, following are the example of valid hex integer:

OX2 ox9F OXbcd ox

 

We rarely use octal and hexadecimal numbers in programming.

 

the largest integer value that can be stored in machine-dependent. It is 32767 on 16-bit machine and 2,147,483,647 on 32-bit machine. It is also possible to store larger integer constants on these machine by appending qualifiers such as U,L and UL to the constants.

 

Real constants:

Integers numbers are inadequate to represent quantities that vary continuously, such as distances, heights, temperatures, prices, and so on. These quantities are represented by

numbers containing fractional parts like 17.548. such numbers called real(or floating point)contants. Further examples of real constants are:

-0.75 435.36 +247.0

 

These numbers are shown in decimal notation, having a whole number followed by a decimal point and the fractional part. It is possible to omit digits before the decimal point, or digits after the decimal points. That is,

 

.95 -.71 +.5

 

Are all valid numbers.

a real numbers may also be expressed in exponential(or scientific)notation. For example, the value 215.65 may be written as 2.1565e2 in exponential notation. E2 means multiply by 102. the general from is:

 

Mantissa e exponent

 

the mantissa is either a real number expressed in decimal notation or an integer. The exponent is integer number with an optional plus or minus sign. The letter e separating the mantissa and the exponent can be written in either lowercase or uppercase. Since the exponent causes the decimal points to “float”, this notation is said to represent a real number in floating point form. Example of legal floating-point constants are:

 

0.65e4 12e-2 1.5e+5 3.18e3 -1.2e-1

 

Embedded white spaces is not allowed.

 

exponential notation is useful for representing numbers that are either very large or very small magnitude. For example, 7500000000 may be written as 7.5E9 or 75E8. similarly, – 0.000000368 is equivalent to -3.68E-7.

 

floating-point constants are normally represented as double –precision quantities. however, the suffixes f or F may be used to force single-precision and l or L extend double precision further.

 

Single character constants:

 

a single character constants (or simply character constants) contains a single character enclosed within a pair of single quote marks. Example of character constants are:

 

‘5’ ‘x’ ‘;’ ‘ ‘

 

note that the character constants ‘5’ is not the same as the number 5. the last constant is a blank space>

 

character constants have integer values known as ASCII values. For example, the statement

 

printf(“%d”, ‘a’);

 

would print the number 97, the ASCII values of the letter a similarly, the statement ,

 

printf(“%c”, ‘97’);

 

would output the letter ‘a’. ASCII values for all character are given in appendix II.

 

since each character constant represent an integer value, it is also possible to perform arithmetic operations on character constants.

 

 

String constants:

 

a string constants is a sequences of character enclosed in double quotes. The characters may be letters, numbers, special characters and back spaces. Example are:

 

“Hello!” “1987” “WELL DONE” “?…!” “5+3” “X”

 

remember that a character constant (e.g., ‘X’) is not equivalent to the single character string constant (e.g., ’X’). further, a single character string constant does not have an equivalent integer value while a character constant has an integer value. Character strings are often used in programs to build meaningful programs.

 

Q-2(c) write a program to check that whether given matrix is unit matrix or not…

#include<stdio.h>

#include<conio.h> 

void main()
{

int a[3][3],i,j,flag=0; clrscr();

printf("enter a square matrix to check unit:\n"); for(i=0;i<3;i++)
{
for(j=0;j<3;j++)

{
scanf("%d",&a[i][j]);

}
printf("\n");
}

for(i=0;i<3;i++)
{

for(j=0;j<3;j++)
{
printf("%d",a[i][j]);

}
printf("\n");
}

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)

{
if(i==j)
{

if(a[i][j]!=1)
{

flag=1;
break;
}

}
else
{

if(a[i][j]!=0)
{

flag=1;
break;
}

}
}
}

printf("The given matrix is "); if (flag==0)

{
printf("An unit matrix");
}

else
{

printf("Not An unit matrix");
}
getch();

}

 

OR

(c) write a program to find minimum and maximum element in 3*3 matrix

#include<stdio.h>
#include<conio.h> 
void main()
{
int a[3][3],i,j, max=1,min; 
clrscr();
printf("enter element for A==>>\n"); 

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}

}

printf("***************************\n");

for(i=0;i<3;i++)

{
for(j=0;j<3;j++)

{
if(a[i][j]>max)
max=a[i][j];

if(a[i][j]<min)
min=a[i][j];
}

}

printf("\n THE MAXIMUM VALUE IS-->>%d",max); printf("\n THE MINIMUM VALUE IS-->>%d",min); getch();
}

 

Q-3 (a) Define basic structure of ‘C’ Program. Explain each of its parts.

C – Program Structure

Documentation Section
Preprocessor Commands
Global Declaration Section
Main() Function Section{Statements;}
Subprogram Section(User Defined Function)Function 1Function 2……Function n

 

The following program is written in the C programming language. Open a text file one.c using vi editor and put the following lines inside that file.

/* simple c programme                         
   Author: xyz    //  Documentation section
   Date    : 19-08-2013
*/
#include <stdio.h>              //  Link section
int total;                      //  Global declaration section
int sum (int, int);             //  Function declaration section
int main ()                     // Main function
{       
     printf ("This is my first c programe \n");
     total = sum (10, 20);                               
     printf ("Total = %d \n", total);
     return 0;
}
int sum (int a, int b)
{                               //  User defined function
     return a + b;              // definition section
}

 

S.No Sections Description
1 Documentation section We can give comments about the program, creation or modified date, author name etc in this section. Comments can be given in two ways inside a C program.      1. Single line comment – Syntax :// Your Comments Here2. Multi line comment – Syntax :/*
comment line1
comment line2
comment line3
*/The characters or words or anything which are given between “/*” and “*/”, won’t be considered by C compiler for compilation process.These will be ignored by C compiler during compilation.
2 Link Section Header files that are required to execute a C program are included in this section
3 Definition Section In this section, variables are defined and values are set to these variables.
4 Global declaration section Global variables are defined in this section. When a variable is to be used throughout the program, can be defined in this section.
5 Function declaration section Function prototype gives many information about a function like return type, parameter names used inside the function.
6 Main function Every C program is started from main function and this function contains two major sections called declaration section and executable section.
7 User defined function section User can define their own functions in this section which perform particular task as per the user requirement.

 

(c) Write a program to sort each alphabet in a given string in descending order…

#include<stdio.h>
#include<conio.h> 
void main()
{
char c[100],temp; int i,j;
clrscr();

printf("enter any string : "); 
gets(c); 
for(i=0;c[i]!='\0';i++)
{

for(j=0;c[j]!='\0';j++)
{
if (c[j]<c[i])

{
temp=c[j];

c[j]=c[i];
c[i]=temp;
}

}
}

printf(" string in descending order %s",c); getch();
}

 

 

Q-4 (a) Explain any two string functions with example.

Ans:

 

strcmp():

 

The “strcmp()” compares two string identified by arguments and has a value 0 if they are equal. If they are not equal it has the numeric difference between the first non matching characters in the string.

 

Syntax: strcmp(string1,string2);

Where, string1 and string2 are string variables or string constant.

For example,

#include<stdio.h>
#include<conio.h> 
void main()
{

char c[50],d[50]; int x;

gets(c);
gets(d);
x=strcmp(c,d);

if(x==0)
{
printf(“Both string are equal”);

}
else

{
printf(“String are not equal”);
}

getch();
}

 

strcpy:

strcpy () works almost like a string assignment operator.

Syntax:

strcpy(string1,string2);

Here, contents of string2 are assigned to string1.

For example, 

#include<stdio.h> 
#include<conio.h> 
void main()
{
char c[50],d[50];

int x;
clrscr();

gets(c);
gets(d);
strcpy(c,d);

printf(“\n Coppied string is %s”,c); 
getch();
}

 

(c) write a program to do following on a given string..

Input:Patel Shiv Kumar

Output:

Patel

Shiv

Kumar

 

Answer:

#include<stdio.h>
#include<conio.h>
#include<string.h> 
void main()
{

char c[50]; int i; 
clrscr();

printf("Enter any string: "); 
gets(c); 
for(i=0;c[i]!='\0';i++)
{

if(c[i]==' ') 
printf("\n");
else
putchar(c[i]);

}
getch();

}

 

Q-5(b) A book shop maintain inventory of books that are being sold at the shop. Information contains author, title, price, qty. when title and author is specified by user, program should search for book and should print its cost and qty available.

#include<stdio.h>

#include<conio.h> struct book

{

char author[50],title[50]; int price,qty;
}b[3];
void main()

{
int i;

char s[50]; clrscr(); for(i=0;i<3;i++)

{
scanf("%s",b[i].author);
scanf("%s",b[i].title);

scanf("%d",&b[i].price);
scanf("%d",&b[i].qty);

}

printf("\nAuthor Title Price Quantity"); for(i=0;i<3;i++)
{
printf("\n%s\t%s\t%d\t%d",b[i].author,b[i].title,b[i].price,b[i].qty);

}

printf("\nenter book name you want to seach--"); scanf("%s",s);
for(i=0;i<3;i++)

{
if(!strcmp(s,b[i].title))
{

printf("match");

printf("\nAuthor Title Price Quantity"); printf("\n%s\t%s\t%d\t%d",b[i].author,b[i].title,b[i].price,b[i].qty);
}
else

{
printf("\nRecord Not Found");

}
}

getch();
}

 

OR

 

(b) Define a structure called time which contains 3 members, Hour, Minute and Second. Develop a program that would assign values to individual members and display time in 12 hour format. E.g. 03:08:06 p.m.

#include<stdio.h>
#include<conio.h> 
struct time
{
int hour,min,sec;

}t;

void main()
{

clrscr();
scanf("%d",&t.hour);

scanf("%d",&t.min);
scanf("%d",&t.sec);

printf("\nHours Minutes Seconds"); printf("\n%d\t%d\t%d",t.hour,t.min,t.sec);

if(t.hour>=12)
{

printf("\n%d:%d:%d PM",t.hour-12,t.min,t.sec);
}
else

{
printf("\n%d:%d:%d AM",t.hour,t.min,t.sec);

}

getch();
}

 

 

 

 

 

 

 

 

 

Download Paper Solutions from Below Link

About admin

Scroll To Top