Sie sind auf Seite 1von 10

Registration No.

: 200503256
Name : Ravinder Singh Yadav
Batch : 2005
Course : Post Graduate Diploma in Information Technology

--------------------------------------------------------------------------------
-----------

Q. 1 Write algorithm for the following :


a) to check whether an entered number is odd / even.
Ans: 1. Accept the number from the user (suppose x).
2. Use modulus(%) operator to find remainder of the division of number b
y 2 i.e. x%2.
3. If result is 0 then entered number is even.Else number is odd.
4. Print the output.
b) to calculate sum of three numbers.
Ans: 1. Accept three numbers from the user (suppose a,b,c)
2. Use '+' operator to add these numbers i.e. a+b+c.
3. Store the result of addition into any variable.
4. Print the output.

Q. 3 Write short notes on the following :


a) C Variables
Ans: Like most programming languages, C is able to use and process named vari
ables and their contents. Variables are most simply describe
d as names by which we refer to some location in memory - a location that holds
a value with which we are working.
It often helps to think of variables as a "pigeonhole", or a placeholder
for a value. You can think of a variable as being equiva
lent to its value. So, if you have a variable i that is initialized to 4, i+1 wi
ll equal 5.
Since C is a relatively low-level programming language, it is necessary
for a C program to claim the memory needed to store the valu
es for variables before using that memory. This is done by declaring variables,
the way in which a C program shows the number of variables i
t needs, what they are going to be named, and how much mem
ory they will need.
Within the C programming language, when we manage and work with variable
s, it is important for us to know the type of our variables and the
size of these types. This is because C is a sufficiently low-level programming
language that these aspects of its working can be hardware specific - that is,
how the language is made to work on one type of machine can be d
ifferent from how it is made to work on another.
All variables in C are typed. That is, you must give a type for every va
riable you declare.
Variable names in C are made up of letters (upper and lower case) and di
gits. The underscore character ("_") is also permitted. Names
must not begin with a digit. Unlike some languages (such as Perl and some BASIC
dialects), C does not use any special prefix characters on variable names.
Some examples of valid (but not very descriptive) C variable names:
foo, Bar, BAZ, foo_bar, _foo42, _QuUx
Here is an example of declaring an integer, which we've called some_numb
er. (Note the semicolon at the end of the line - that is how your
compiler separates one program statement from another.)
int some_number;
This statement means we're declaring some space for a variable called so
me_number, which will be used to store integer data. Note that
we must specify the type of data that a variable will store.
We can also declare and assign some content to a variable at the same ti
me. This is called initialization because it is the "initi
al" time a value has been assigned to the variable:
int some_number=3;
In C, all variable declarations (except for globals) must be done at the
beginning of a block. You cannot declare your variables,
insert some other statements, and then declare more variables. Variable declarat
ions (if there are any) are always the first part of any bloc
k.
After declaring variables, you can assign a value to a variable later on
using a statement like this:
some_number=3;

b) C datatypes
Ans:
In Standard C there are four basic data types. They are int, char, float
, and double.
The int type
The int type stores integers in the form of "whole numbers". An
integer is typically the size of one machine word, wh
ich on most modern home PCs is 32 bits (4 octets). Examples of literals are whol
e numbers (integers) such as 1,2,3, 10, 100... Whe
n int is 32 bits (4 octets), it can store any whole
number (integer) between -2147483648 and 2147483647. A 32 bit word (number) has
the possibility of representing 4294967296 numbers
(2 to the power of 32).
If you want to declare a new int variable, use the int keyword.
For example:
int numberOfStudents, i, j=5;
In this declaration we declare 3 variables, numberOfStudents, i
& j, j here is assigned the literal 5.
The char type
The char type is similar to the int type, yet it is only big eno
ugh to hold one ASCII character. It stores the same
kind of data as an int (i.e. integers), but always has a size of one byte. It is
most often used to store character data, hence its na
me.
Examples of character literals are 'a', 'b', '1', etc., as well
as some special characters such as '\0' (the null c
haracter) and '\n' (endline, recall "Hello, World"). Note that the char value mu
st be enclosed within single quotations.
The reason why one byte is seen as the ideal size for character
data is that one byte is large enough to provide one s
lot for each member of the ASCII character set, which is a set of characters whi
ch maps one-to-one with a set of integers. At compi
le time, all character literals are converted into
their corresponding integer. For example, 'A' will be converted to 65 (0x41).(Kn
owing about the ASCII character set is often useful.)
When we initialize a character variable, we can do it two ways.
One is preferred, the other way is bad programming
practice.
The first way is to write
char letter1='a';
This is good programming practice in that it allows a person rea
ding your code to understand that letter is being
initialized with the letter 'a' to start off with.
The second way, which should not be used when you are coding let
ter characters, is to write
char letter2=97; /* in ASCII, 97 = 'a' */
This is considered by some to be extremely bad practice, if we a
re using it to store a character, not a small number,
in that if someone reads your code, most readers are forced to look up what char
acter corresponds with the number 97 in the encoding s
cheme. In the end, letter1 and letter2 store both the
same thing -- the letter "a", but the first method is clearer, easier to debug,
and much more straightforward.
One important thing to mention is that characters for numerals a
re represented differently from their corresponding nu
mber, i.e. '1' is not equal to 1.
There is one more kind of literal that needs to be explained in
connection with chars: the string literal. A strin
g is a series of characters, usually intended to be output to the string. They a
re surrounded by double quotes (" ", not ' '). An e
xample of a string literal is the "Hello, world!\n"
in the "Hello, World" example.
The float type
float is short for Floating Point. It stores real numbers also,
but is only one machine word in size. Therefore, it is
used when less precision than a double provides is required. float literals mus
t be suffixed with F or f, otherwise they will be int
erpreted as doubles. Examples are: 3.1415926f, 4.0f,
6.022e+23f. float variables can be declared using the float keyword.
The double type
The double and float types are very similar. The float type allo
ws you to store single-precision floating point n
umbers,while the double keyword allows you to store double-precision floating po
int numbers - real numbers, in other words, both int
eger and non-integer values. Its size is typically
two machine words, or 8 bytes on most machines. Examples of double literals are
3.1415926535897932, 4.0,6.022e+23 (scientific notati
on). If you use 4 instead of 4.0, the 4 will be interpreted
as an int.
The distinction between floats and doubles was made because of t
he differing sizes of the two types. When C was first
used, space was at a minimum and so the judicious use of a float instead of a d
ouble saved some memory. Nowadays, with memory more fr
eely available, you do not really need to conserve
memory like this - it may be better to use doubles consistently. Indeed, some C
implementations use doubles instead of floats when y
ou declare a float variable.
If you want to use a double variable, use the double keyword.

Q. 4 Accept principal amount, rate of interest, and duration from the user. D
isplay Interest Amount and Total Amount (Principal + Interest).
Ans: /*Calculate interest amount and total amount*/
#include<stdio.h> /*includes statdard head
er files*/
#include<conio.h>
void main()
{
long int p,r,t; /*declare variables to b
e used*/
float interest,totamt;
clrscr();
printf("\nEnter principle amount: "); /*accept the desired val
ues from user*/
scanf("%ld",&p);
printf("\nEnter rate of interest: ");
scanf("%ld",&r);
printf("\nEnter time: ");
scanf("%ld",&t);
interest = (p*r*t)/100; /*calculate the interest
*/
totamt = p + interest; /*calculate total amount
*/
printf("\nInterest is: %f",interest); /*show the result to the
user*/
printf("\nTotal amount is: %f",totamt);
getch();
}

Q. 5 Accept the salary of an employee from the user. Calculate the gross sal
ary on the following basis:
Basic HRA DA
1 - 4000 10% 50%
4001 - 8000 20% 60%
8001 - 12000 25% 70%
12000 and above 30% 80%

Ans: /*Calculate total salary of the employee*/


#include<stdio.h> /*include necessary header files*/
#include<conio.h>
void main()
{
long int bsalary; /*declare variables to be used*/
float totsalary;
clrscr();
printf("\nEnter basic salary: "); /*accept initial values fro
m the user*/
scanf("%ld",&bsalary);
if(bsalary>=1 && bsalary<=4000) /* check for salary slot*/
{
printf("\nOn this basic salary, HRA will be 10% and DA w
ill be 50%");
totsalary = bsalary + (bsalary*10)/100 + (bsalary*50)/10
0; /*calculate total salary depending upon slot of salary*/
}
else
{
if(bsalary>=4001 && bsalary<=8000)
{
printf("\nOn this basic salary, HRA will be 20%
and DA will be 60%");
totsalary = bsalary + (bsalary*20)/100 + (bsalar
y*60)/100;
}
else
{
if(bsalary>=8001 && bsalary<=12000)
{
printf("\nOn this basic salary, HRA will
be 25% and DA will be 70%");
totsalary = bsalary + (bsalary*25)/100 +
(bsalary*70)/100;
}
else
{
printf("\nOn this basic salary, HRA will
be 30% and DA will be 80%");
totsalary = bsalary + (bsalary*30)/100 +
(bsalary*80)/100;
}
}
}
printf("\nTotal Salary of the employee is: %f",totsalary);
/*print the result*/
getch();
}

Q. 6 Accept any number from the user. Display whether the number is divisibl
e by 100 or not.

Ans: /*Find whether a number is divisible by 100 or not*/


#include<stdio.h> /*include standard header files*/
#include<conio.h>
#include<math.h>
void main()
{
long int x; /*declare variables to be used*/
clrscr();
printf("\nEnter the number: "); /*get the values from the use
r*/
scanf("%ld",&x);
if((x%100)==0) /*condition for checking whet
her number is divisible by 100 or not*/
{
printf("\nNumber is divisible by 100"); /*show the
result*/
}
else
{
printf("\nNumber is not divisible by 100");
}
getch();
}

Q. 7 Accept a month in digit from the user. Display the month in words. If n
umber is not between 1 and 12 display message Invalid Month . (Use switch )

Ans: /*Display months in words if it is entered in numbers*/


#include<stdio.h> /*include standard header files*/
#include<conio.h>
void main()
{
int month; /*declare necessary variables*/
clrscr();
printf("\nEnter month in number: "); /*accept the data fro
m user*/
scanf("%d",&month);
switch(month) /*use swith statement
for various month options*/
{
case 1: printf("\nMonth is January"); /* define vario
us cases according to the value entered*/
break;
case 2: printf("\nMonth is February");
break;
case 3: printf("\nMonth is March");
break;
case 4: printf("\nMonth is April");
break;
case 5: printf("\nMonth is May");
break;
case 6: printf("\nMonth is June");
break;
case 7: printf("\nMonth is July");
break;
case 8: printf("\nMonth is August");
break;
case 9: printf("\nMonth is September");
break;
case 10: printf("\nMonth is October");
break;
case 11: printf("\nMonth is November");
break;
case 12: printf("\nMonth is December");
break;
default: printf("\nInvalid Month");
break;
}
getch();
}

Q. 8 Display all prime numbers between 50 and 150.

Ans: /*Display all prime numbers between 50 and 150*/


#include<stdio.h> /*include standard header files*/
#include<conio.h>
void main()
{
int i,j,pflag; /*declare varibles to be used*/
clrscr();
for(i=50;i<=150;i++) /*start loop from 50 to 150*/
{
for(j=2;j<=i-1;j++) /*start another loop from 2 to o
ne less then the outer loop counter*/
{
pflag = i%j; /* find remainder of the divisio
n*/
if(pflag==0) /* if number is divisible, exit
loop and leave this number as it is not prime*/
{
break;
}
}
if(pflag!=0) /* else show that number is pri
me*/
{
printf("\n%d",i);
}
}
getch();
}

Q. 12 Accept any two strings from the user. Display whether both the strings a
re equal or not. (do not use standard functions.)
Ans: /*Display whether two strings are equal or not*/
#include<stdio.h> /*include standard header files*/
#include<conio.h>
void main()
{
int i; /*declare required variables*/
int sflag=0;
char s1[50],s2[50];
clrscr();
printf("\nEnter first string: "); /*get the data from the use
r*/
gets(s1);
printf("Enter second string: ");
gets(s2);
sflag = stringcmp(s1,s2); /*call the defind function
for checking whether strings are equal or not*/
if(sflag!=0)
{
printf("\nTwo strings are not equal"); /* print the res
ult accordingly*/
}
else
{
printf("\nTwo strings are equal");
}
getch();
}
int stringcmp(char str1[50], char str2[50]) /*function stringcmp defi
nded here*/
{
int i,scntr=0,cntr1=0,cntr2=0; /*local variables for fun
ction*/
for(i=0;str1[i]!='\0';i++) /*find first string leng
th*/
{
cntr1++;
}
printf("\nFirst string length: %d",cntr1);
for(i=0;str2[i]!='\0';i++) /*find second string len
gth*/
{
cntr2++;
}
printf("\nSecond string length: %d\n",cntr2);
if(cntr1==cntr2) /* check whether both stri
ngs are of same length or not*/
{
for(i=0;i<cntr1;i++)
{
if(str1[i]!=str2[i]) /* check whether each cha
racter of both strings matches or not*/
scntr++;
}
}
else
{
scntr++;
}
return scntr; /* return the result*/
}

Q. 13 Accept any string from the user. Convert case of the string to lower / u
pper using pointers. (if entered string is in lower case convert it to upperca
se and vice versa.)
Ans: /*Convert case of the string using pointers*/
#include<stdio.h> /* include standard header files*/
#include<conio.h>
#include<ctype.h>
void main()
{
char str[50],*p; /*declare variables to be used*/
clrscr();
printf("\nEnter a string: "); /*get the data from the user
*/
gets(str);
p = str;
if(islower(str[0])) /*check the case of entered
string*/
{
printf("\nOriginal string is: %s",str);
while(*p)
{
*p = toupper(*p); /*change the cas
e of string*/
p++;
}
printf("\nChanged case is: %s",str); /*print changed
string*/
}
else
{
printf("\nOriginal string is: %s",str);
while(*p)
{
*p = tolower(*p); /*change the cas
e of string*/
p++;
}
printf("\nChanged case is: %s",str); /*print changed
string*/
}
getch();
}

Q. 14 Accept any two numbers from the user. Using pointers swap the values two
numbers without using third variable.

Ans: /*Using pointers, swap two numbers*/


#include<stdio.h> /*include standard header files*/
#include<conio.h>
void main()
{
int a,b; /*declare variables to be used*/
clrscr();
printf("\nEnter first number: "); /*accept the data from t
he user*/
scanf("%d",&a);
printf("\nEnter second number: ");
scanf("%d",&b);
printf("\nValue of a and b before swapping is: %d %d",a,b);
/*print original value of variables*/
swap(&a,&b);
/*call swap function*/
printf("\n Value of a and b after swapping is: %d %d",a,b);
/*print swapped values of the variables*/
getch();
}
swap(int *x,int *y) /*swap function defined here*/
{
*x = *x + *y; /*do swapping using pointers*/
*y = *x - *y;
*x = *x - *y;
return *x,*y; /*return result*/
}

==== End ====

Das könnte Ihnen auch gefallen