Spa Sem 2 PDF
Spa Sem 2 PDF
Spa Sem 2 PDF
Approach
(MU)
Harish G. Narula
Now, 5 & 3
2. 13 AND 10
Now; 13 and 10
3 2 1 0
(1000)2 = 1 × 2 + 0 × 2 + 0 × 2 + 0 × 2 =
(8)10
(13)10 & (10)10 = (8)10
3. 7 OR 6
In ‘C’ programming, this is written as 7|6.
Now; 7 OR 6
2 1 0
(111)2 = 1 × 2 + 1 × 2 + 1 × 2
= (7)10
∴ (7)10 | (6)10 = (7)10
4. 5 OR 2
Now, 5 OR 2
2 1 0
(111)2 = 1 × 2 + 1 × 2 + 1 × 2
= (7)10
∴ (5)10 | (2)10 = (7)10
5. 6 EXOR 3
In C, this is written as 6 ^ 3
Now; 6 EXOR 3
2 1 0
(101)2 = 1 × 2 + 0 × 2 + 1 × 2
= (5)10
∴ (6)10 ^ (3)10 = (5)10
6. NOT 3
In C, this is written as ~ 3
Now; ~ 3 is
00011
11100
Note : 1. The data may be of more than 2 bits and hence you
will get the output depending on the size of data.
2. In computer binary language you will get this value as
(– 4)10.
Step-form Algorithm
Step I : Indicate the user to enter two numbers
by displaying suitable sentence.
Step II : Wait for the user to enter the input.
Step III : Using a temporary variable swap the
two numbers. The logic to swap the
numbers is shown below :
i. Put the first number in the temporary
variable.
ii. Put the second number in the first
variable.
iii. Put the temporary number in the second
variable
Step IV : Display the two numbers indicating the
two numbers are swapped.
Step V : Stop
Pseudo code Algorithm
Step I : START
Step II : PRINT "Enter two numbers".
Step III : INPUT a, b.
Step IV : temp = a.
Step V : a=b
Step VI : b = temp
Step VII : PRINT a , b.
Step VIII : STOP
Ø Algorithm 1.6.7 : Write an algorithm to display
the word “Computer” five times.
Step-form Algorithm
Step I : Initialize a variable as a counter with the
initial value as 1.
Step II : Check if the counter variable is greater
than 5, if yes goto step VI
Step III : Display the word "Computer".
Step IV : Increment the counter.
Step V : Goto step II
Step VI : Stop.
Pseudo code Algorithm
Step I : START
Step II : i = 1.
Step III : IF i > 5 THEN GOTO step VII
Step IV : PRINT "Computer".
Step V : i = i + 1.
Step VI : GOTO step III
Step VII : STOP.
Ø Algorithm 1.6.8 : Write an algorithm to display
the first ten natural numbers.
Step-form Algorithm
Step I : Initialize a counter with the value as 1
Step II : Check if the counter is greater than 10, if
yes, then goto step VI.
Step III : Display the value of the counter
variable.
Step IV : Increment the counter variable
Step V : Goto step II
Step VI : Stop.
Pseudo code Algorithm
Step I : START
Step II : i=1
Step III : IF i > 10 THEN GOTO step VII.
Step IV : PRINT i.
Step V : i=i+1
Step VI : GOTO step III
Step VII : STOP.
Ø Algorithm 1.6.9 : Write an algorithm to display
the first n natural numbers, where
the value of n is taken from user
Step-form Algorithm
Step I : Indicate the user to enter a number by
displaying suitable sentence.
Step II : Wait for the user to enter the input.
Step III : Initialize a counter with the value as 1.
Step IV : Check if the counter is greater than the
user entered variable, if yes,
then goto step VIII.
Step V : Display the value of the counter variable.
Step VI : Increment the counter variable
Step VII : Goto step IV
Step VIII : Stop.
Pseudo code Algorithm
Step I : START
Step II : PRINT "Enter a number".
Step III : INPUT n.
Step IV : i = 1.
Step V : IF i > n THEN GOTO step IX.
Step VI : PRINT i.
Step VII : i = i + 1.
Step VIII : GOTO step V
Step IX : STOP.
Ø Algorithm 1.6.10 : Write an algorithm find the
factorial of a number.
Step-form Algorithm
Step I : Indicate the user to enter a number by
displaying suitable sentence.
Step II : Wait for the user to enter the input.
Step III : Initialize a counter with the value as 1
and a "fact" variable as 1
Step IV : Check if the counter is greater than the
user entered variable, if yes,
then goto step VIII.
Step V : Multiply the "fact" variable with the
counter value.
Step VI : Increment the counter variable
Step VII : Goto step IV
Step VIII : Display the value of the factorial i.e. the
variable "fact"
Step IX : Stop.
Pseudo code Algorithm
STEP I : START
Step II : PRINT "Enter a number".
Step III : INPUT n.
Step IV : i = 1, fact = 1
Step V : IF i > n THEN GOTO step IX.
Step VI : fact = fact * i.
Step VII : i = i + 1
Step VIII : GOTO step V
Step IX : PRINT fact
Step X : STOP
Ø Algorithm 1.6.11 : Write an algorithm to check if
the entered number is prime
number or not.
Step-form Algorithm
Step I : Indicate the user to enter the input
number by displaying suitable sentence.
Step II : Wait for the user to enter the number to
be checked.
Step III : Initialize a divisor variable with the
value as 2.
Step IV : Check if the user entered variable is
divisible by the divisor variable, if yes,
then goto step VII.
Step V : Increment the divisor variable.
Step VI : Goto step IV
Step VII : If the user entered number is equal to
the current divisor variable value then
display that the number entered by the
user is a prime number; else display that
the user entered number is not a prime
number
Step VIII : Stop.
Pseudo code Algorithm
Step I : START
Step II : PRINT "Enter a number".
Step III : INPUT n.
Step IV : x = 2.
Step V : IF (n mod x) = 0 THEN GOTO step VIII
Step VI : x = x + 1.
Step VII : GOTO step V
Step VIII : IF x = n THEN PRINT "Prime number"
ELSE PRINT "Not a prime number"
Step IX : STOP.
Ø Algorithm 1.6.12 : Write an algorithm to check if
the year entered is leap year or
not.
Note : A normal year is said to consist of 365 days. But the
actual time required for Earth to revolve around the sun is
365.242199 days. Hence to average it out a day is added
in every fourth year which gives average of 365.25 days
per year. To reach more closer to the above mentioned
time, every 100th year isnota leap year and every 400
yearsis leap year. This brings theaverage time for a
yearto be 365.2425 days almost closest.
Step-form Algorithm
Step I : Indicate the user to enter a number by
displaying suitable sentence.
Step II : Wait for the user to enter the input.
Step III : Check if the user entered number is a
leap year by the necessary condition. (The
condition to be checked is that the year
should not be divisible by 100 and divisible
by 4 or divisible by 400). If yes, then
display that the year is a leap year; else
display that the year is not a leap year.
Step IV : Stop.
Pseudo code Algorithm
STEP I : START
Step II : PRINT "Enter a year number".
Step III : INPUT y.
Step IV : IF (y mod 4 = 0 AND y mod 100 ≠ 0) OR
(y mod 400 = 0) THEN
PRINT "It is a leap year"
ELSE PRINT "Not a leap year"
Step V : STOP.
Flowchart 1
Flowchart 2 : Draw a flowchart to find the product of two
numbers.
Solution :
As shown in the Flowchart 2, initially two numbers
are accepted from user, this is shown using a
parallelogram. The two numbers are then multiplied,
shown using a rectangle. The result is displayed,
again shown using parallelogram. The start and stop
are shown using oval.
Flowchart 2
Flowchart 3 : Draw a flowchart to find the square of a numbers.
Solution :
As shown in the Flowchart 3, initially one number is
accepted from user, this is shown using a
parallelogram. The number is then multiplied with
itself, shown using a rectangle. The result is
displayed, again shown using parallelogram. The
start and stop are shown using oval.
Flowchart 3
Flowchart 4 : Draw a flowchart to display the first 'n' natural
numbers.
Solution :
In Flowchart 4 again the value of a variable say 'n' is
taken from user. Another counter variable is
initialized to the value 1.
The value of the counter variable is checked. If the
counter variable value is less than or equal to the
value of variable 'n'; then the counter variable value
is displayed and is incremented. Then the counter
variable value is again checked. This process keeps on
repeating until the counter value crosses the value of
'n', after which the algorithm stops.
Flowchart 4
Flowchart 5 : Draw a flowchart to check if the year entered by
user is leap year or not.
Solution :
Flowchart 5
❏❏❏
2 Programming
Flowchart 1
Ø Program
// “Hello Friend”
#include<conio.h>
#include<stdio.h>
void main ()
printf("Hello Friend");
getch();
Output
Hello Friend
Note : When shifting to left, each of the bit is shifted left. The
first bit is lost and the last bit is inserted as 0.
6. 13 >> 3 = 1
Assuming the data to be char i.e. 8 bit data
(13)10 = (0 0 0 0 1 0 1 1)2
After shifting left once (0 0 0 0 0 1 0 1)2
After shifting left for the second time (0 0 0 0
0 0 1 0)2
After shifting for the third time (0 0 0 0 0 0 0
1)2 = (1)10
Note : When shifting to right, each of the bit is shifted right. The
first bit is inserted as 0 and the last bit is lost.
6. z=a+
Ans. :
1. (A > B ) && (A > C)
2. (A < B ) || ( A > C)
3. Side = pow ( ( a * a + b * b + 2 * a * b ), 0.5)
4. a = x( * y + z * (x / y) + z * y) / (x + y + z);
5. x 1 = (– b + pow ( ( b * b – 4 * a * c ) , 0.5 ) )/(2 * a);
x 2 = (– b – pow ( ( b * b – 4 * a * c ) , 0.5 ) )/(2 * a);
6. z = a + 1 (1 + ( k + a ) / 2 );
Examples :
1. printf(“The number of buildings is %d”,b);
This statement will print the output with the
format specifier replaced with the value of b.
2. printf(“The simple interest is %f”,si);
This statement will print the output where the
format specifier %f will be replaced with the
float type value of the variable si.
3. printf(“The simple interest is %5.2f”,si);
The format specifier in this statement is
accompanied with a field width i.e. 5.2 ( %5.2f),
which indicates five digits before the fraction
point and 2 digits after the fraction point. This
statement will display the output wherein the
value of si will be displayed with five digits
before the fraction point and only two digits
after the fraction point.
2. scanf()
Syntax : scanf(“format String”, address of variables);
Here, format string can contain format specifier, field
width and assignment suppression character. The
assignment suppression character (*) is used to
discard one of the user entered value.
The address of the variable is obtained with the help
of the address of operator (&).
Examples :
1. scanf(“%d”,&x);
This statement is used to accept a int type value
from user in the variable x
2. scanf(“%d %d %f”,&x,&y,&z);
This statement is used to accept two int type
data into variables x and y. It also accepts a
float type data into the variable z.
II. Unformatted IO Functions
The unformatted IO functions do not have any format
specifier. They are mostly used to accept and display
only one character or a string (string is a set of
characters to make a word or sentence).
The different unformatted IO functions are listed
below;
1. getch() : This function is used accept one
character from the user.
2. getche() : This function is used accept one
character from the user and echo it
(display it on the screen).
3. getchar() : This function is used to accept one
character from the user and echo it
(display it on the screen) and also wait
after that for the user to press the
enter key.
4. gets() : This function is used to accept a
string from the user. We will see in
details of this when studying the
chapter on strings.
5. putch() or : These functions are used to display
a character on the monitor. putchar()
6. puts() : This function is used to display a
string on the monitor.
Ø Program 2.13.1 : Write a C program to accept a
number and display its square.
Step-form Algorithm
Step I : Declare the required variables.
Step II : Indicate the user to enter the input
number by displaying suitable sentence
using printf() function.
Step III : Wait using the scanf() function for the
user to enter the number.
Step IV : Calculate the square of the user entered
number.
Step V : Display the calculated result using printf()
statement.
Step VI : Wait for user to press a key using getch()
function.
Step VII : Stop
Pseudo code Algorithm
Step I : START
Step II : PRINT "Enter a number".
Step III : INPUT x.
Step IV : y = x * x.
Step V : PRINT y.
Step VI : STOP
Flowchart : (Refer Flowchart 2)
Flowchart 2
Ø Program
#include<stdio.h>
#include<conio.h>
void main ()
clrscr();
int x, y;
printf("Enter a number:");
scanf("%d",&x);
y=x*x;
getch();
Output
Enter a number:6
The square of 6 is 36
Flowchart 3
Ø Program
#include<stdio.h>
#include<conio.h>
void main ()
int x, y, z;
clrscr();
scanf("%d%d",&x,&y);
z=x*y;
getch();
Output
The product is 12
#include<stdio.h>
#include<conio.h>
void main ()
clrscr();
getch();
Output
9.5
10
#include<stdio.h>
#include<conio.h>
void main ()
float basic,hra,da,gross;
clrscr();
scanf("%f", &basic);
hra=40*basic/100;
da = 50*basic/100;
gross=basic+da+hra;
getch();
Output
Enter the basic salary:10000
#include<stdio.h>
#include<conio.h>
void main ()
clrscr();
mt =km*1000;
ft = mt * 3.33;
cm = mt * 100;
inch = ft *12;
getch();
Output
#include<stdio.h>
#include<conio.h>
void main ()
clrscr();
getch();
Output
Enter the length and breadth of the rectangle:10
15
#include<stdio.h>
#include<conio.h>
void main ()
clrscr();
scanf("%d",&number);
d3=number%10;
number=number/10;
d2= number%10;
d1=number%10;
getch();
Output
#include<stdio.h>
#include<conio.h>
void main ()
int n1;
clrscr();
result=n1*n2;
getch();
Output
3.9
Flowchart 10
Ø Program
#include<stdio.h>
#include<conio.h>
void main ()
clrscr();
scanf("%d %d",&n1,&n2);
greater=(n1>n2)?n1:n2;
getch();
Output
Enter two numbers:35
12
#include<stdio.h>
#include<conio.h>
void main ()
clrscr();
greater=(n1>n2)?((n1>n3)?n1:n3):((n2>n3)?n2:n3);
Output
23
33
Flowchart 12
Ø Program
#include<stdio.h>
#include<conio.h>
void main ()
float r, area;
clrscr();
scanf("%f",&r);
area=3.14 *r*r;
getch();
Output
Flowchart 13
Ø Program
#include<stdio.h>
#include<conio.h>
void main ()
float f, c;
clrscr();
scanf("%f",&f);
c=(f – 32)/1.8;
getch();
Output
#include<conio.h>
void main ()
int a, b, c, sum;
float avg;
clrscr();
scanf("%d %d %d",&a,&b,&c);
sum= a+b+c;
avg=sum/3.0;
getch();
Output
Enter three numbers:3
#include<stdio.h>
#include<conio.h>
void main ()
int a, b, temp;
clrscr();
scanf("%d %d",&a,&b);
temp=a;
a=b;
b=temp;
getch();
Output
Flowchart 16
Ø Program
#include<stdio.h>
#include<conio.h>
void main ()
{
int a, b;
clrscr();
scanf("%d",&a);
b=a<<3;
getch();
Output
#include<stdio.h>
#include<conio.h>
void main ()
clrscr();
printf("Enter a number:");
scanf("%d",&a);
rem1=a%2;
rem2=a%3;
getch();
Output
Enter a number:23
#include<stdio.h>
#include<conio.h>
void main ()
clrscr();
printf("Enter a number:");
scanf("%d",&a);
digit1=a%10;
a=a/10;
digit2=a%10;
printf("Reversed no is %d%d",digit1,digit2);
getch();
Output
Enter a number:67
Reversed no is 76
Flowchart 19
Ø Program
#include<stdio.h>
#include<conio.h>
void main ()
{
int a, rem;
clrscr();
printf("Enter a number:");
scanf("%d",&a);
rem=a%2;
getch();
Output
Enter a number:23
Odd number
#include<stdio.h>
#include<conio.h>
void main ()
int a;
float b;
clrscr();
scanf("%f",&b);
a=(int)b;
getch();
Output
#include<stdio.h>
#include<conio.h>
void main ()
int a,b;
clrscr();
getch();
Output
The OR of 5 and 9 is 13
The NOT of 5 is – 6
The NOT of 9 is – 10
#include<stdio.h>
#include<conio.h>
void main ()
int a;
clrscr();
printf("Enter a number:");
scanf("%d",&a);
getch();
Output
Enter a number:65
#include<stdio.h>
#include<conio.h>
void main ()
int a=2,b=3,ab=4;
int i;
int in='2'*2;
char ch='c';
clrscr();
printf("%c %c\n",ch,(++ch));
printf("%d %d %d\n",a,a,++a);
printf("%d %d %d\n",b,b,++b);
printf("%d %d %d\n",ab,ab,++ab);
printf("%d %d\n",a,!!a);
getch();
Output
dd
333
444
555
31
#include<stdio.h>
#include<conio.h>
void main ()
int a=3;
clrscr();
printf(" %d\n",a);
printf(" %d\n",a++);
printf(" %d\n",++a);
getch();
Output
#include<stdio.h>
#include<conio.h>
void main ()
int x=4,y=9;
int z;
clrscr();
z=(x++)+(– –y)+y;
printf("Value=%d\n",z);
printf("Value=%d\n",z);
getch();
Output
Value=20
Value=16
#include<conio.h>
void main ()
int a,b,c;
clrscr();
a=2;b=5;c=10;
printf("Value=%d\n",(a+b*– c));
getch();
Output
Value=–48
Value=–22
Value=–2
#include<stdio.h>
#include<conio.h>
void main ()
int a=5,b=3;
float c;
clrscr();
c=a/b;
printf("%f",c);
getch();
Output
1.000000
#include<stdio.h>
#include<conio.h>
void main ()
int a=10,b,c;
clrscr();
c=b=a;
b– =a – –;
c– =– – a;
a – =–– a-a– –;
printf("a=%d\nb=%d\nc=%d\n",a,b,c);
getch();
Output
a=6
b=0
c=2
#include<stdio.h>
#include<conio.h>
void main ()
int k=3,l=4,m;
clrscr();
m=++k +l– –;
printf("Value of m %d\n",m);
m=k++ + – –l;
printf("Value of m %d\n",m);
getch();
Output
Value of m 8
Value of m 6
Flowchart 23
Ø Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
int a,b,c;
float x1,x2;
clrscr();
scanf("%d %d %d",&a,&b,&c);
x1=(–b+pow((b*b–4*a*c),0.5))/(2*a);
x2=(–b–pow((b*b–4*a*c),0.5))/(2*a);
getch();
Output
#include<stdio.h>
#include<conio.h>
void main ()
int a=1,b=2,c=3,d=4.75,x;
clrscr();
printf("%d %d %d %d %d",a,b,c,d,x);
getch();
Output
23452
#include<stdio.h>
#include<conio.h>
void main ()
int x=1;
clrscr();
printf("%d%d%d\n",x,(x=x+2),(x<<2));
x<<2;
printf("%d%d%d\n",++x,x++,++x);
getch();
Output
334
644
#include<stdio.h>
#include<conio.h>
void main ()
int x=10,y,z;
clrscr();
z=y=x;
y–=– –x;
z–=x– –;
x–=– –x–x– –;
printf("x = %d y = %d z = %d",x,y,z);
getch();
Output
x=6 y= 1 z= 1
❏❏❏
#include<stdio.h>
#include<conio.h>
void main ()
int i;
clrscr();
for(i=1;i<=5;i++)
printf("Computer\n");
}
getch();
Output
Computer
Computer
Computer
Computer
Computer
#include<stdio.h>
#include<conio.h>
void main()
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d\n",i);
getch();
Output
10
Ø Program 3.2.3 : Write a program to display the
first n natural numbers, where the
value of n is taken from user.
Ø Algorithm
Step I : START
Step II : PRINT "Enter a number".
Step III : INPUT n.
Step IV : i = 1.
Step V : IF i > n THEN GOTO step IX.
Step VI : PRINT i.
Step VII : i = i + 1.
Step VIII : GOTO step V
Step IX : STOP.
Ø Flowchart : (Refer Flowchart 3)
Flowchart 3
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%d\n",i);
getch();
Output
Enter a number: 7
4
5
#include<stdio.h>
#include<conio.h>
void main()
int i,n,fact;
clrscr();
printf("Enter a number: ");
scanf("%d",&n);
for(i=1,fact=1;i<=n;i++)
fact=fact * i;
getch();
Output
Enter a number: 4
#include<stdio.h>
#include<conio.h>
void main()
int i,n;
clrscr();
scanf("%d",&n);
for(i=1;i<=10;i++)
printf("%d X %d = %d\n",n,i,(n*i));
getch();
Output
Enter a number: 5
5X1=5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
Flowchart 6
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int i,n;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("%d\n",2*i+1);
getch();
Output
Enter a number: 6
1
3
11
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i+=2)
printf("%d\n",i);
getch();
Output
Enter a number: 10
9
Ø Program 3.2.8 : Write a program to calculate and
display the sum of first n natural
numbers.
Ø Algorithm
Step I : START.
Step II : PRINT "Enter a number".
Step III : INPUT n.
Step IV : i = 1, sum = 0.
Step V : IF i > n, THEN GOTO step IX.
Step VI : sum = sum + i.
Step VII : i=i+1
Step VIII : GOTO step V.
Step IX : PRINT sum.
Step X : STOP.
Ø Flowchart : (Refer Flowchart 8)
Flowchart 8
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,sum=0;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
sum=sum+i;
printf("Sum= %d\n",sum);
getch();
Output
Enter a number: 10
Sum= 55
#include<stdio.h>
#include<conio.h>
void main()
int i,n,sum=0;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
sum=sum+i*i;
printf("Sum= %d\n",sum);
getch();
Output
Enter a number: 5
Sum= 55
#include<stdio.h>
#include<conio.h>
void main()
int a=0,b=1,c,i,n;
clrscr();
scanf("%d",&n);
printf("Fibonacci Series\n0\n1\n");
for(i=1;i<=n–2;i++)
c=a+b;
printf("%d\n",c);
a=b;
b=c;
getch();
Output
Enter a number: 10
Fibonacci Series
13
21
34
Ø Algorithm
Step I : START.
Step II : PRINT "Enter the value of n".
Step III : INPUT n.
Step IV : i = 1, sum = 0.
Step V : IF i > n, THEN GOTO step IX.
Step VI : sum = sum + 1 / i.
Step VII : i=i+1
Step VIII : GOTO step V.
Step IX : PRINT sum.
Step X : STOP.
Ø Flowchart : (Refer Flowchart 11)
Flowchart 11
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int i,n;
float sum=0.0;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
sum=sum+1.0/i;
getch();
Output
Enter a number: 4
Ø Algorithm
Step I : START.
Step II : PRINT "Enter the value of n".
Step III : INPUT n.
Step IV : i = 1, fact = 1, sum = 0.
Step V : IF i > n, THEN GOTO step IX.
Step VI : fact = fact * i
sum = sum + 1 / fact.
Step VII : i=i+1
Step VIII : GOTO step V.
Step IX : PRINT sum.
Step X : STOP.
Ø Flowchart : (Refer Flowchart 12)
Flowchart 12
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int i,n,fact=1;
float sum=0.0;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
fact=fact*i;
sum=sum+1.0/fact;
getch();
Output
Enter a number: 5
The value of the series is:1.716667
Flowchart 13
Ø Program
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
int i, fact=1,sign=–1;
float x,numerator,sum,term;
clrscr();
x=x*3.14/180;
term=x;
sum=term;
for(i=3;term>=0.0000001;i=i+2)
fact=fact*i*(i–1);
numerator=pow(x,i);
term=numerator/fact;
sign=sign*–1;
getch();
Output
Flowchart 14
Ø Program
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
int i, n, fact=1,sign=–1;
float x,numerator,sum,term;
clrscr();
value of n: ");
scanf("%f %d",&x,&n);
x=x*3.14/180;
term=x;
sum=term;
for(i=3;i<=n;i=i+2)
{
fact=fact*i*(i–1);
numerator=pow(x,i);
term=numerator/fact;
sign=sign*–1;
getch();
Output
10
Flowchart 15
Ø Program
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
int i,n;
float x;
clrscr();
scanf("%f %d",&x,&n);
for(i=2;i<=n;i++)
getch();
Output
3 raised to 2 is 9
3 raised to 3 is 27
3 raised to 4 is 81
3 raised to 5 is 243
Ø Algorithm
Step I : START.
Step II : PRINT "Enter the values of s0, v0 and a
(i.e. initial displacement, velocity and
acceleration)".
Step III : INPUT s0, v0, a.
Step IV : s = s0 + v0 + 0.5 * a
Step V : PRINT "1", s
Step VI : t = 5.
Step VII : IF t > 100, THEN GOTO step XII .
Step VIII : s = s0 + v0 + 0.5 * a * t * t.
Step IX : PRINT t, s
Step X : t = t + 5.
Step XI : GOTO step VII.
Step XII : STOP.
Ø Flowchart : (Refer Flowchart 16)
Flowchart 16
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
{
int t;
float s,s0,v0,a;
clrscr();
scanf("%f %f %f",&s0,&v0,&a);
printf("t\tS\n1\t%d\n",s0+v0+0.5*a);
for(t=5;t<=100;t+=5)
printf("%d\t%f\n",t,s0+v0+0.5*a*t*t);
getch();
Output
t S
1 9.9
5 127.500002
10 495.00001
15 1107.500021
20 1965.000038
25 3067.50006
30 4415.000086
35 6007.500117
40 7845.000153
45 9927.500193
50 12255.000238
55 14827.500288
60 17645.000343
65 20707.500403
70 24015.000467
75 27567.500536
80 31365.00061
85 35407.500689
90 39695.000772
95 44227.500861
100 49005.000954
#include<conio.h>
void main()
int i,j;
clrscr();
for(i=1;i<=5;i++)
for(j=1;j<=2;j++)
printf("Hi ");
printf("\n");
getch();
}
Output
Hi Hi
Hi Hi
Hi Hi
Hi Hi
Hi Hi
#include<stdio.h>
#include<conio.h>
void main()
int i,j;
clrscr();
for(i=1;i<=5;i++)
for(j=1;j<=2;j++)
{
printf("Hello ");
printf("Hi \n");
getch();
Output
Hello Hello Hi
Hello Hello Hi
Hello Hello Hi
Hello Hello Hi
Hello Hello Hi
#include<conio.h>
void main()
int i,j;
clrscr();
for(i=1;i<=2;i++)
for(j=1;j<=3;j++)
printf("Hello ");
printf("\n");
getch();
}
Output
#include<conio.h>
void main()
int i,j;
clrscr();
for(i=1;i<=5;i++)
for(j=1;j<=i;j++)
printf("1");
printf("\n");
getch();
Output
1
11
111
1111
11111
Flowchart 20
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int i,j;
clrscr();
for(i=1;i<=5;i++)
for(j=1;j<=i;j++)
printf("*");
printf("\n");
getch();
Output
*
**
***
****
*****
#include<stdio.h>
#include<conio.h>
void main()
int i,j,n;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
printf("*");
printf("\n");
getch();
Output
**
***
****
*****
******
*******
#include<stdio.h>
#include<conio.h>
void main()
int i,j,n;
clrscr();
for(i=1;i<=n;i++)
for(j=1;j<=n–i;j++)
printf(" ");
for(j=1;j<=i;j++)
printf("*");
printf("\n");
getch();
Output
Enter the number of lines:8
*
**
***
****
*****
******
*******
********
#include<stdio.h>
#include<conio.h>
void main()
int i,j,n;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n–i;j++)
printf(" ");
for(j=1;j<=i;j++)
printf("*");
printf("\n");
for(i=n–1;i>=1;i– –)
for(j=1;j<=n–i;j++)
printf(" ");
}
for(j=1;j<=i;j++)
printf("*");
printf("\n");
getch();
Output
**
***
****
*****
******
*****
****
***
**
#include<stdio.h>
#include<conio.h>
void main()
int i,j,n;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n–i;j++)
printf(" ");
for(j=1;j<=i;j++)
printf("* ");
printf("\n");
for(i=n–1;i>=1;i– –)
for(j=1;j<=n–i;j++)
printf(" ");
}
for(j=1;j<=i;j++)
printf("* ");
printf("\n");
getch();
Output
**
***
****
*****
******
*******
******
*****
****
***
**
#include<stdio.h>
#include<conio.h>
void main()
int i,j,n;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n–i;j++)
{
printf(" ");
for(j=1;j<=2*i–1;j++)
printf("*");
printf("\n");
for(i=n–1;i>=1;i– –)
for(j=1;j<=n–i;j++)
printf(" ");
for(j=1;j<=2*i–1;j++)
{
printf("*");
printf("\n");
getch();
Output
***
*****
*******
*********
*******
*****
***
*
Ø Program 3.2.27 : Write a program to display the
following asking the user for the
number of lines.
1
12
123
1234
12345
123456
Ø Algorithm
Step I : START.
Step II : PRINT "Enter the number of lines"
Step III : INPUT n
Step IV : i = 1.
Step V : IF i > n, THEN GOTO step XIV .
Step VI : j = 1.
Step VII : IF j > i, THEN GOTO step XI
Step VIII : PRINT j
Step IX : j=j+1
Step X : GOTO step VII
Step XI : PRINT next line character.
Step XII : i = i + 1.
Step XIII : GOTO step V.
Step XIV : STOP.
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int i,j,n;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
getch();
Output
12
123
1234
12345
123456
#include<conio.h>
void main()
int i,j,n;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n–i;j++)
printf(" ");
for(j=1;j<=i;j++)
{
printf("%d",j);
for(j=i–1;j>=1;j– –)
printf("%d",j);
printf("\n");
getch();
Output
121
12321
1234321
123454321
12345654321
Explanation
Here we have to perform 3 operations in each line
and then next line i.e. printf("\n").
The first operation is blank spaces. The second
operation is printing j i.e. numbers from 1 to line
number. The third operation is to print the numbers
from i-1 (where i is line number) to 1. For each of
these three operations there is an inner for loop.
Ø Program 3.2.29 : Write a program to display the
following asking the user for the
number of lines.
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
Ø Algorithm
Step I : START.
Step II : PRINT "Enter the number of lines"
Step III : INPUT n
Step IV : i = 1.
Step V : IF i > n, THEN GOTO step XXIXV
Step VI : j=1
Step VII : IF j > n – i, THEN GOTO step XI
Step VIII : PRINT " "
Step IX : j=j+1
Step X : GOTO step VII
Step XI : j = 1.
Step XII : IF j > i, THEN GOTO step XVI
Step XIII : PRINT (j + 64) type casted to
character type data
Step XIV : j=j+1
Step XV : GOTO step XII
Step XVI : j=i–1
Step XVII : IF j < 1, THEN GOTO step XXI
Step XVIII : PRINT (j + 64) type casted to
character type data
Step XIX : j=j–1
Step XX : GOTO step XVII
Step XXI : PRINT next line character.
Step XXII : i = i + 1.
Step XXIII : GOTO step V.
Step XXIV : STOP.
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int i,j,n;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n–i;j++)
printf(" ");
for(j=1;j<=i;j++)
printf("%c",(char)(j+64));
}
for(j=i–1;j>=1;j– –)
printf("%c",(char)(j+64));
printf("\n");
getch();
Output
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,k;
clrscr();
scanf("%d",&n);
for(i=1,k=1;i<=n;i++)
for(j=1;j<=i;j++)
printf("%d ",k++);
printf("\n");
getch();
Output
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
#include<stdio.h>
#include<conio.h>
void main()
int i,j,n,k;
clrscr();
scanf("%d",&n);
for(i=1,k=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%c ",64+k++);
printf("\n");
getch();
Output
BC
DEF
GH IJ
K LM N O
PQRSTU
Ø Program 3.2.32 : Write a program to generate
following patterns.
A
CB
FED
JIHG
O N M L K (May 2016)
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int n,i,j,m=65,k=64;
printf(“Enter n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
k=k+i;
m=k;
for(j=0;j<=n-i;j++)
printf(“ ”);
for(j=1;j<=i;j++)
printf(“%c”, m--);
printf(“\n”);
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
if(i%2 !=0)
for(j=1;j<=i;j++)
printf(“%d ”,j);
else
for(j=i;j>=1;j--)
printf(“%d ”,j);
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
scanf("%d",&n);
i=1;
while(i<=n)
printf("%d\n",i);
i++;
getch();
Output
2
3
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
scanf("%d",&n);
i=1;
do
printf("%d\n",i);
i++;
}while(i<=n);
getch();
Output
3
4
#include<stdio.h>
#include<conio.h>
void main()
int n,digit,sum=0,product=1;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
while(n!=0)
digit=n%10;
sum=sum+digit;
product=product*digit;
n=n/10;
printf("Sum=%d\nProduct=%d\n",sum,product);
getch();
}
Output
Enter a number:2354
Sum=14
Product=120
#include<stdio.h>
#include<conio.h>
void main()
int n,digit,sum=0,product=1;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
do
digit=n%10;
sum=sum+digit;
product=product*digit;
n=n/10;
}while(n!=0);
printf("Sum=%d\nProduct=%d\n",sum,product);
getch();
Output
Enter a number:1345
Sum=13
Product=60
Flowchart 26
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int n,count=0;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
while(n!=0)
n=n/10;
count++;
printf("Number of digits=%d",count);
getch();
Output
Enter a number:7567
Number of digits=4
Ø Program 3.2.39 : Write a program to find the
number of digits before and after
the decimal point in a floating
point number.
Ø Algorithm
Step I : START.
Step II : PRINT "Enter a number".
Step III : INPUT n.
Step IV : x = n type casted to integer type data
Step V : count = 0
Step VI : IF x = 0, THEN GOTO step X.
Step VII : count = count + 1
Step VIII : x = x / 10
Step IX : GOTO step V
Step X : count1 = 0
Step XI : x = n type casted to integer type data
Step XII : n=n–x
Step XIII : IF x = 0 THEN GOTO step
Step XIV : count1 = count1 + 1
Step XV : n = n * 10
Step XVI : x = n type casted to integer type data
Step XVII : n = n – x
Step XVIII : GOTO step XIII
Step XIX : PRINT count, count1.
Step XX : STOP.
Ø Flowchart : (Refer Flowchart 27)
Flowchart 27
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int n,countBefore=0,countAfter=0;
float x;
clrscr();
printf("Enter a number:");
scanf("%f",&x);
n=(int)(x);
x=x–n;
while(n!=0)
{
n=n/10;
countBefore++;
while(x!=0)
x=x*10;
n=(int)x;
x=x–n;
countAfter++;
point=%d\n",countBefore);
getch();
}
Output
Enter a number:1234.625
#include<stdio.h>
#include<conio.h>
void main()
{
int n,reverse=0,digit;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
while(n!=0)
digit=n%10;
n=n/10;
reverse=reverse*10+digit;
getch();
Output
Enter a number:1234
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
int n,i;
clrscr();
scanf("%d",&n);
for(i=15;i>=0;i– –)
printf("%d",n/(int)pow(2,i));
n=n%(int)(pow(2,i));
getch();
Output 1
Enter a number : 8
#include<stdio.h>
#include<conio.h>
void main()
int i,j;
clrscr();
for(i=4;i>=1;i– –)
for(j=1;j<=4–i;j++)
printf(" ");
for(j=1;j<=i;j++)
{
printf("*");
printf("\n");
getch();
Output
****
***
**
#include<stdio.h>
#include<conio.h>
void main()
int i,j,k;
clrscr();
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
for(k=1;k<=3;k++)
printf("%d%d%d\n",i,j,k);
getch();
Output
111
112
113
121
122
123
131
132
133
211
212
213
221
222
223
231
232
233
311
312
313
321
322
323
331
332
333
#include<stdio.h>
#include<conio.h>
void main()
int i,j;
clrscr();
for(i=1;i<=5;i++)
for(j=1;j<=5–i;j++)
printf(" ");
}
for(j=1;j<=i;j++)
printf("%d",i);
printf("\n");
getch();
Output
22
333
4444
55555
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
char x='P';
clrscr();
for(i=1;i<=5;i++)
for(j=1;j<=i;j++)
printf("%c",(x+j–1));
printf("\n");
getch();
Output
PQ
PQR
PQRS
PQRST
#include<stdio.h>
#include<conio.h>
void main()
int i,j,n;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n–i;j++)
printf(" ");
for(j=i;j>=1;j– –)
printf("%d",j);
for(j=1;j<=i–1;j++)
printf("%c",(j+'A'–1));
printf("\n");
getch();
}
Output
21A
321AB
4321ABC
54321ABCD
#include<stdio.h>
#include<conio.h>
void main()
int i,j,sum=0,n,fact;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<=2 * n – 1;i+=2)
fact=1;
for(j=1;j<=i;j++)
fact=fact*j;
}
sum=sum+fact;
getch();
Output
Enter a number:4
#include<stdio.h>
#include<conio.h>
void main()
int i,j;
char x='A';
clrscr();
for(i=1;i<=5;i++)
for(j=1;j<=i;j++)
{
printf("%c",(x+j–1));
printf("\n");
getch();
Output
AB
ABC
ABCD
ABCDE
#include<conio.h>
#include<stdio.h>
void main ()
int i,j,n;
float sum = 0;
scanf("%d",&n);
for(i=1,j=1;i<=n; i++,j=j+2)
{
sum=sum+(float)(j)/(j+1);
printf("sum = %f",sum);
Output
Enter a number:3
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=1,k=5;i<=5;i++,k– –)
for(j=1;j<=i;j++)
printf("%d",k);
printf("\n");
getch();
Output
44
333
2222
11111
#include<stdio.h>
#include<conio.h>
void main()
int i,j;
clrscr();
for(i=1;i<=5;i++)
for(j=1;j<=i;j++)
{
printf("%d",i);
printf("\n");
getch();
Output
22
333
4444
55555
#include<stdio.h>
#include<conio.h>
void main()
int n,reverse=0,digit;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
while(n!=0)
digit=n%10;
n=n/10;
reverse=reverse*10+digit;
getch();
}
Output
Enter a number:1234
#include<conio.h>
#include<stdio.h>
void main ()
int i,j,n;
float sum = 0;
scanf("%d",&n);
for(i=1,j=1;i<=n; i++,j=j+2)
{
sum=sum+(float)(j)/(j+1);
printf("sum = %f",sum);
Output
Enter a number:3
#include<stdio.h>
#include<conio.h>
void main()
int i,j,n;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n-i;j++)
printf(" ");
for(j=1;j<=i;j++)
printf("%c ",(j+64));
}
printf("\n");
getch();
#include<stdio.h>
#include<conio.h>
void main()
float x,sum,term,denr;
int i,n;
clrscr();
for (i=1;i<n;i++)
denr = (2*i)*(2*i-1);
term = -term*x*x/denr;
getch();
Ø Program
(i)
#include<stdio.h>
#include<conio.h>
void main()
int i, j;
clrscr();
printf (“ ”);
printf (“%c”,(char)(j+64));
printf (“\n”);
}
getch();
}
(ii)
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
clrscr();
for (i=1; i<=4; i++)
{
for (j=1; j<=4–i; j++)
{
printf (“ ”);
}
for (j=1; j<=i; j++)
{
printf (“%d”, j);
}
for (j = 1; j <=i–1; j++)
{
printf (“%c”, (char) (j + 64));
}
printf (“%n”);
}
getch();
}
Ø Program 3.2.57 : Generate the following pattern
of digits using nested loops.
1
232
34543
4 5 6 7 6 5 4 (Dec. 2015)
Ø Program
#include <stdio.h>
int main()
int i,space,rows,k=0,count=0,count1=0;
scanf("%d",&rows);
for(i=1;i<=rows;++i)
{
for(space=1;space<=rows-i;++space)
printf(" ");
++count;
while(k!=2*i-1)
if (count<=rows-1)
printf("%d ",(i+k));
++count;
else
++count1;
}
++k;
count1=count=k=0;
printf("\n");
return 0;
❏❏❏
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
if(n%10==0)
{
printf("The number is divisible by 10");
else
getch();
Output
Enter a number:400
Flowchart 2
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int i=0,n=1;
clrscr();
while(i<10)
printf("%d\n",n);
i++;
}
n++;
getch();
Output
40
80
120
160
200
240
280
320
360
400
#include<stdio.h>
#include<conio.h>
void main()
int i=2,n;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
while(n%i!=0)
i++;
if(n==i)
printf("Prime Number");
else
{
printf("Not a prime number");
getch();
Output
Enter a number:17
Prime Number
#include<stdio.h>
#include<conio.h>
void main()
int i,n,x=1;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
while (n!=0)
x++;
i=2;
while(x%i!=0)
{
i++;
if(x==i)
printf("%d\n",x);
n--;
getch();
Output
Enter a number:10
7
11
13
17
19
23
29
#include<conio.h>
void main()
int sum=0,digit,x,copy;
clrscr();
printf("Enter a number:");
scanf("%d",&x);
copy=x;
while(x!=0)
digit=x%10;
sum=sum+digit*digit*digit;
x=x/10;
if(sum==copy)
{
printf("Armstrong Number");
else
getch();
Output
Enter a number:789
Flowchart 6
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int sum,digit,x=99,copy,n;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
while(n!=0)
x++;
copy=x;
sum=0;
while(x!=0)
digit=x%10;
sum=sum+digit*digit*digit;
x=x/10;
if(sum==copy)
{
printf("%d\n",copy);
n--;
x=copy;
getch();
Output
Enter a number:4
153
370
371
407
Flowchart 7
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int year;
clrscr();
printf("Enter a year:");
scanf("%d",&year);
printf("Leap Year");
else
getch();
Output
Enter a year:1900
Flowchart 8
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int n,i;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
printf("Factors are:\n");
i=2;
while(i<n)
if(n%i==0)
printf("%d\n",i);
i++;
getch();
}
Output
Enter a number:30
Factors are:
10
15
statements;
else
if (condition)
statements;
else
if (condition)
statements;
}
else
if(condition)
statements;
else
statements;
}
}
Algorithm
Step I : START.
Step II : PRINT "Enter marks out of 100".
Step III : INPUT marks
Step IV : IF marks > = 70, PRINT "Distinction"
AND GOTO step IX
Step V : IF marks > = 60, PRINT "First Class"
AND GOTO step IX
Step VI : IF marks > = 50, PRINT "Second Class"
AND GOTO step IX
Step VII : IF marks > = 40, PRINT "Pass Class"
AND GOTO step IX
Step VIII : PRINT "Fail"
Step IX : Stop.
Ø Flowchart : (Refer Flowchart 9)
Flowchart 9
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int marks;
clrscr();
scanf("%d",&marks);
if(marks>=70)
printf("Distinction");
else
if (marks>=60)
{
printf("First Class");
else
if (marks>=50)
printf("Second Class");
else
if(marks>=40)
printf("Pass Class");
else
{
printf("Fail");
getch();
Output
Distinction
break;
case label2:statements;
break;
case label3:statements;
break;
case labeln:statements;
break;
default : statements;
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
scanf("%d",&n);
switch(n)
case 0:printf("Zero");
break;
case 1:printf("One");
break;
case 2:printf("Two");
break;
case 3:printf("Three");
break;
case 4:printf("Four");
break;
case 5:printf("Five");
break;
case 6:printf("Six");
break;
case 7:printf("Seven");
break;
case 8:printf("Eight");
break;
case 9:printf("Nine");
break;
getch();
Output
Four
#include<stdio.h>
#include<conio.h>
void main()
int n,digit,rev=0;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
while(n!=0)
digit=n%10;
rev=rev*10+digit;
n=n/10;
while(rev!=0)
{
digit=rev%10;
rev=rev/10;
switch(digit)
break;
break;
break;
break;
break;
break;
break;
break;
break;
getch();
Output
Enter a number:513
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int no1,no2,result,choice;
clrscr();
scanf("%d %d",&no1,&no2);
printf("1.Add\n2.Subtract\n3.Multiply\n4.Divide\n5.Modulus\nEnter
your choice:");
scanf("%d",&choice);
switch(choice)
case 1:result=no1+no2;
printf("Sum= %d",result);
break;
case 2:result=no1–no2;
printf("Difference= %d",result);
break;
case 3:result=no1*no2;
printf("Product= %d",result);
break;
case 4:result=no1/no2;
printf("Quotient= %d",result);
break;
case 5:result=no1%no2;
printf("Remainder= %d",result);
break;
default:printf("Invalid choice");
getch();
Output
1.Add
2.Subtract
3.Multiply
4.Divide
5.Modulos
Product=72
Ø Program 4.1.13 : Write a menu driven program to
perform add / subtract / multiply /
divide based on the users choice.
The user will indicate the
operation to be performed using
the signs i.e. + for addition, - for
subtraction and so on.
Ø Algorithm
Step I : START.
Step II : PRINT "Enter two numbers".
Step III : INPUT a, b.
Step IV : PRINT "+. Addition, –. Subtraction, *.
Multiplication, /. Division %. Modulus.
Enter your choice".
Step V : INPUT choice.
Step VI : IF choice = + THEN
sum = a + b
PRINT sum AND
GOTO step XII
Step VII : IF choice = – THEN
difference = a -– b
PRINT difference AND
GOTO step XII
Step VIII : IF choice = * THEN
prod = a * b
PRINT prod AND
GOTO step XII
Step IX : IF choice = / THEN
quotient = a / b
PRINT quotient AND
GOTO step XII
Step X : IF choice = % THEN
remainder = a mod b
PRINT remainder AND
GOTO step XII
Step XI : PRINT "Invalid choice".
Step XII : STOP.
Ø Flowchart : (Refer Flowchart 12)
Flowchart 12
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int no1,no2,result;
char choice;
clrscr();
scanf("%d %d",&no1,&no2);
printf("+.Add\n-.Subtract\n*.Multiply\n/.Divide\n%.Modulus\nEnter
your choice:");
choice=getche();
printf("\n");
switch(choice)
case '+':result=no1+no2;
printf("Sum= %d",result);
break;
case '-':result=no1-no2;
printf("Difference= %d",result);
break;
case '*':result=no1*no2;
printf("Product= %d",result);
break;
case '/':result=no1/no2;
printf("Quotient= %d",result);
break;
case '%':result=no1%no2;
printf("Remainder= %d",result);
break;
default:printf("Invalid choice");
getch();
Output
Enter the numbers:4
+.Add
-.Subtract
*.Multiply
/.Divide
%.Modulus
Product=20
#include<stdio.h>
#include<conio.h>
void main()
int no1,no2,result;
char choice;
clrscr();
scanf("%d",&no1);
choice=getche();
scanf("%d",&no2);
switch(choice)
case '+':result=no1+no2;
printf("Sum= %d",result);
break;
case '–':result=no1–no2;
printf("Difference= %d",result);
break;
case '*':result=no1*no2;
printf("Product= %d",result);
break;
case '/':result=no1/no2;
printf("Quotient= %d",result);
break;
case '%':result=no1%no2;
printf("Remainder= %d",result);
break;
default:printf("Invalid choice");
getch();
Output
*
7
Product=21
Ø Algorithm
Step I : START.
Step II : PRINT "Enter marks out of 100".
Step III : INPUT marks
Step IV : IF marks = 100, PRINT "Distinction"
AND GOTO step XII
Step V : marks = marks / 10
Step VI : IF marks = 0, 1, 2, or 3 PRINT "Fail"
AND GOTO step XII
Step VII : IF marks = 4, PRINT "Pass Class"
AND GOTO step XII
Step VIII : IF marks = 5, PRINT "Second Class"
AND GOTO step XII
Step IX : IF marks = 6, PRINT "First" AND
GOTO step XII
Step X : IF marks = 7, 8, or 9 PRINT
"Distinction" AND GOTO step XII
Step XI : PRINT "Invalid marks"
Step XII : Stop.
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int marks;
clrscr();
scanf("%d",&marks);
if(marks==100)
printf("Distinction");
else
switch(marks/10)
case 0:
case 1:
case 2:
case 3: printf("Fail");
break;
break;
break;
break;
case 7:
case 8:
case 9:printf("Distinction");
break;
default:printf("Invalid marks");
getch();
Output
Distinction
void main()
int month;
clrscr();
scanf("%d",&month);
switch(month)
case 1:printf("January");
break;
case 2:printf("February");
break;
case 3:printf("March");
break;
case 4:printf("April");
break;
case 5:printf("May");
break;
case 6:printf("June");
break;
case 7:printf("July");
break;
case 8:printf("August");
break;
case 9:printf("September");
break;
case 10:printf("October");
break;
case 11:printf("November");
break;
getch();
Output
August
Fig. 4.1.1
Fig 4.1.2
#include<stdio.h>
#include<conio.h>
void main()
{
int n,total=0;
clrscr();
again:
printf("Enter a number:");
scanf("%d",&n);
total=total+n;
if(total<100)
goto again;
printf("Total=%d",total);
getch();
Output
Enter a number:56
Enter a number:12
Enter a number:33
Total=101
Ø Program 4.1.18 : Write a program to demonstrate
the use of break statement
or
Write a program to accept 10, 2-
digit numbers from user and add
them. If the user enters a three
digit number stop accepting the
numbers and display the sum.
Ø Algorithm
Step I : START
Step II : total = 0
Step III : PRINT "Enter a number"
Step IV : INPUT n.
Step V : IF n > 99, THEN GOTO step VIII
Step VI : total = total + n.
Step VII : GOTO step III
Step VIII : PRINT total
Step IX : Stop.
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,total=0,i;
clrscr();
for(i=1;i<=10;i++)
printf("Enter a number:");
scanf("%d",&n);
if(n>99)
break;
total=total+n;
printf("Total=%d",total);
getch();
Output
Enter a number:45
Enter a number:54
Enter a number:22
Enter a number:59
Enter a number:121
Total=180
#include<stdio.h>
#include<conio.h>
void main()
int n,total=0,i;
clrscr();
for(i=1;i<=5;i++)
printf("Enter a number:");
scanf("%d",&n);
if(n>99)
continue;
total=total+n;
printf("Total=%d",total);
getch();
Output
Enter a number:12
Enter a number:121
Enter a number:22
Enter a number:11
Enter a number:89
Enter a number:99
Total=233
Ø Program 4.1.20 : Write a program to find GCD of
two numbers.
Ø Algorithm
Step I : START.
Step II : PRINT "Enter two numbers".
Step III : INPUT n1, n2.
Step IV : IF n1 < n2 THEN gcd = n1 ELSE gcd =
n2.
Step V : IF (n1 mod gcd = 0 AND n2 mod gcd =
0), THEN GOTO step VIII.
Step VI : gcd = gcd - 1.
Step VII : GOTO step V
Step VIII : IF gcd = 1 THEN PRINT "GCD doesn't
exists" ELSE PRINT gcd
Step IX : STOP.
Ø Flowchart : (Refer Flowchart 13)
Flowchart 13
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int n1,n2,gcd;
clrscr();
scanf("%d %d",&n1,&n2);
if (n1<n2)
gcd=n1;
else gcd=n2;
while(n1%gcd!=0 || n2%gcd!=0)
gcd--;
if(gcd==1)
printf("GCD doesnt exists");
else
printf("GCD=%d",gcd);
getch();
Output
20
GCD=5
Flowchart 14
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int n1,n2,lcm;
clrscr();
scanf("%d %d",&n1,&n2);
if (n1>n2)
lcm=n1;
else lcm=n2;
while(lcm%n1!=0 || lcm%n2!=0)
lcm++;
printf("LCM= %d",lcm);
getch();
Output
10
#include<stdio.h>
#include<conio.h>
void main()
int n1,n2,lcm,gcd;
clrscr();
scanf("%d %d",&n1,&n2);
if (n1>n2)
lcm=n1;
gcd=n2;
}
else
lcm=n2;
gcd=n1;
while(lcm%n1!=0 || lcm%n2!=0)
lcm++;
while(n1%gcd!=0 || n2%gcd!=0)
gcd--;
printf("LCM= %d \nGCD=%d",lcm,gcd);
getch();
}
Output
10
LCM=50
GCD=5
#include<stdio.h>
void main()
int i=1,j=1;
for(;;)
if(i>3) break;
else j+=i;
printf("%d\n",j);
i+=j;
Output
#include<stdio.h>
#include<conio.h>
int main()
int i;
for(i=0;i<8;i++)
if(i%2==0)
printf("%d\n",i+1);
else if(i%3==0)
continue;
else if (i%5==0)
break;
Output
End of Program
End of Program
End of Program
End of Program
End of Program
Ø Program 4.1.25 : Write a program to display
Armstrong numbers between 1 to
1000. (May 2013)
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int sum,digit,x=1,copy;
clrscr();
while(x<=1000)
x++;
copy=x;
sum=0;
while(x!=0)
{
digit=x%10;
sum=sum+digit*digit*digit;
x=x/10;
if(sum==copy)
printf("%d\n",copy);
n--;
x=copy;
getch();
#include<conio.h>
void main()
int i,x=1;
clrscr();
while (x<=100)
x++;
i=2;
while(x%i!=0)
i++;
if(x==i)
printf("%d\n",x);
}
getch();
Output
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
#include<stdio.h>
#include<conio.h>
void main()
int units;
float charge;
clrscr();
scanf("%d",&units);
switch(units/200)
break;
break;
default:charge=(units-600) +390;
break;
printf("Charges=%f",charge);
getch();
❏❏❏
Flowchart 1
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int n1,n2,sum;
clrscr();
scanf("%d %d",&n1,&n2);
sum=add(n1,n2);
printf("Sum=%d",sum);
getch();
int c;
c=a+b;
return c;
Output
Sum=10
Flowchart 2
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int n1,n2;
scanf("%d %d",&n1,&n2);
add(n1,n2);
getch();
int c;
c=a+b;
printf("sum=%d",c);
Output
sum=9
Flowchart 3
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int n1,n2,n3;
clrscr();
scanf("%d %d %d",&n1,&n2,&n3);
avg(n1,n2,n3);
getch();
float average;
average=(a+b+c)/3.0;
printf("Average=%f",average);
}
Output
Average=4.333333
Flowchart 4
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int no,factorial;
clrscr();
printf("Enter a number:");
scanf("%d",&no);
factorial=fact(no);
printf("Factorial=%d",factorial);
getch();
int i,ans;
for(i=1,ans=1;i<=no;i++)
ans=ans*i;
return ans;
Output
Enter a number:4
Factorial=24
#include<stdio.h>
#include<conio.h>
void main()
int n,r,ncr;
clrscr();
scanf("%d %d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n–r));
printf("nCr=%d",ncr);
getch();
{
int i,ans;
for(i=1,ans=1;i<=no;i++)
ans=ans*i;
return ans;
Output
nCr=10
Flowchart 6
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,npr;
clrscr();
scanf("%d %d",&n,&r);
npr=fact(n)/fact(n–r);
printf("nPr=%d",npr);
getch();
int i,ans;
for(i=1,ans=1;i<=no;i++)
ans=ans*i;
}
return ans;
Output :
nPr=60
#include<stdio.h>
#include<conio.h>
void main()
int no1,no2,gcd;
clrscr();
scanf("%d %d",&no1,&no2);
gcd=GCD(no1,no2);
if(gcd==1)
else
printf("GCD=%d",gcd);
getch();
}
int gcd;
if(no1<no2)
gcd=no1;
else
gcd=no2;
while(no1%gcd!=0 || no2%gcd!=0)
gcd--;
return gcd;
Output
25
GCD=5
#include<stdio.h>
#include<conio.h>
void main()
int no1,no2,lcm;
clrscr();
scanf("%d %d",&no1,&no2);
lcm=LCM(no1,no2);
printf("LCM=%d",lcm);
getch();
int lcm;
if(no1>no2)
lcm=no1;
else
lcm=no2;
while(lcm%no1!=0 || lcm%no2!=0)
lcm++;
return lcm;
Output
LCM=14
Flowchart 7
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int no,factorial;
int fact (int no);
clrscr();
printf("Enter a number:");
scanf("%d",&no);
factorial=fact(no);
printf("Factorial=%d",factorial);
getch();
if(no==1)
return 1;
else
Output
Enter a number:5
Factorial=120
#include<stdio.h>
#include<conio.h>
void main()
int n,i;
clrscr();
scanf("%d",&n);
printf("0\n");
for(i=1;i<=n–1;i++)
printf("%d\n",fibo(0,1,i));
getch();
}
int fibo (int a, int b, int i)
if(i==1)
return b;
else
Output
13
21
34
Flowchart 8
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int n,x,y;
clrscr();
scanf("%d %d",&x,&n);
y=exponential(x,n);
getch();
if(n==1)
return x;
else
return (x*exponential(x,n–1));
Output
Flowchart 9
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int no,sum;
clrscr();
printf("Enter a number:");
scanf("%d",&no);
sum=recursion(no);
getch();
if(no==1)
return 1;
else
Output
Enter a number:10
#include<stdio.h>
#include<conio.h>
void main()
int n,r,ncr;
clrscr();
scanf("%d %d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n–r));
printf("nCr=%d",ncr);
getch();
int i,ans;
for(i=1,ans=1;i<=no;i++)
ans=ans*i;
return ans;
Output
nCr=10
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
scanf("%d",&n);
display(n);
getch();
int i;
for(i=1;i<=n;i++)
printf("%d\n",i);
}
}
Output
Enter a number: 7
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
scanf("%d",&n);
mul(n);
getch();
void mul(int n)
int i;
for(i=1;i<=10;i++)
printf("%d X %d = %d\n",n,i,(n*i));
Output
Enter a number: 5
5X1=5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
scanf("%d",&n);
odd(n);
getch();
void odd(int n)
int i;
for(i=0;i<=n-1;i++)
printf("%d\n",2*i+1);
Output
Enter a number: 6
11
#include<stdio.h>
#include<conio.h>
void main()
int i,n;
clrscr();
odd(n);
getch();
void odd(int n)
int i;
for(i=1;i<=n;i+=2)
printf("%d\n",i);
Output
Enter a number: 10
5
7
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
scanf("%d",&n);
printf("Fibonacci Series\n0\n1\n");
fibo(n);
getch();
}
void fibo(int n)
int a=0,b=1,c,i;
for(i=1;i<=n-2;i++)
c=a+b;
printf("%d\n",c);
a=b;
b=c;
Output
Enter a number: 10
Fibonacci Series
1
1
13
21
34
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
scanf("%d",&n);
pattern(n);
getch();
void pattern(int n)
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
printf("*");
printf("\n");
Output
**
***
****
*****
******
*******
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
scanf("%d",&n);
pattern(n);
getch();
void pattern(int n)
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
Output
12
123
1234
12345
123456
#include<stdio.h>
#include<conio.h>
void main()
int n;
void pattern(int n);
clrscr();
scanf("%d",&n);
pattern(n);
getch();
void pattern(int n)
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n-i;j++)
printf(" ");
for(j=1;j<=i;j++)
{
printf("%c",(char)(j+64));
for(j=i-1;j>=1;j--)
printf("%c",(char)(j+64));
printf("\n");
Output
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
prime(n);
getch();
void prime(int n)
{
int i=2;
while(n%i!=0)
i++;
if(n==i)
printf("Prime Number");
else
Output
Enter a number:17
Prime Number
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
digits(n);
getch();
void digits(int n)
{
int digit,rev=0;
while(n!=0)
digit=n%10;
rev=rev*10+digit;
n=n/10;
while(rev!=0)
digit=rev%10;
rev=rev/10;
switch(digit)
break;
break;
break;
break;
break;
break;
break;
break;
break;
}
Output
Enter a number:513
#include<stdio.h>
intmain()
intnum,reverse;
scanf("%d",&num);
reverse=rev(num);
return0;
}
intrev(intnum)
staticsum,r;
if(num)
r=num%10;
sum=sum*10+r;
rev(num/10);
else
return0;
returnsum;
Sample output
#include<stdio.h>
#include<math.h>
void main()
float R,P,CI;
int N;
float comp_int_calc(float,float,int);
clrscr();
scanf("%f",&P);
scanf("%d",&N);
scanf("%f",&R);
R =R/100;
CI =comp_int_calc(P,R,N);
getch();
float COMP_INT=0;
COMP_INT =pow(1+RATE,YEARS);
COMP_INT =AMT*COMP_INT;
return COMP_INT;
int num1,num2;
scanf("%d",&num1);
scanf("%d",&num2);
return(0);
#include<conio.h>
void incr()
int i=0;
i++;
printf("%d\n",i);
int main()
incr();
incr();
incr();
getch();
Output
1
1
#include<stdio.h>
#include<conio.h>
void incr()
i++;
printf("%d\n",i);
int main()
{
incr();
incr();
incr();
getch();
Output
#include<conio.h>
int a=5;
void main()
int a=10;
printf("%d\n",a);
printf("%d\n",::a);
a=::a+a;
printf("%d\n",a);
printf("%d\n",::a);
::a=a;
printf("%d\n",a);
printf("%d\n",::a);
getch();
}
Output
10
15
15
15
#include<stdio.h>
#include<conio.h>
int x;
void f1()
++x;
void main()
{
int x = 10;
f1();
x = ::x + 10;
printf("%d %d\n",x,::x);
getch();
Output
11 1
#include<stdio.h>
#include<conio.h>
void f1()
int n2 = 20;
n1 = n1 + 10;
n2 = n1 + n2;
n3 = n1 + n2;
printf("%d %d %d\n",n1,n2,n3);
int n3;
void main()
register int i;
getch();
Output
10 30 40
20 40 60
30 50 80
❏❏❏
Chapter 1 » Chapter 2 » Chapter 3 »
Chapter 4 » Chapter 5 » Chapter 6 »
CHAPTER Arrays, String, Structure,
#include<conio.h>
void main()
int n,i,a[100];
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
for(i=0;i<=n–1;i++)
printf("%d\n",a[i]);
}
getch();
Output
Enter a value:1
Enter a value:2
Enter a value:3
Enter a value:4
Enter a value:5
5
Ø Program 6.1.2 : Write a program to accept ‘n’
integers from user into an array
and display the average of these
numbers.
Ø Algorithm
Step I : START
Step II : PRINT "Enter number of elements".
Step III : INPUT n
Step IV : i=0
Step V : IF i > n – 1 THEN GOTO step X
Step VI : PRINT "Enter a number"
Step VII : INPUT a[i]
Step VIII : i=i+1
Step IX : GOTO step V
Step X : i = 0, sum = 0
Step XI : IF i > n – 1 THEN GOTO step XV
Step XII : sum = sum + a[i]
Step XIII : i=i+1
Step XIV : GOTO step XI
Step XV : avg = sum / n
Step XVI : PRINT avg
Step XVII : STOP
Ø Flowchart : (Refer Flowchart 2)
Flowchart 2
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int n,i,a[100],sum=0;
float avg;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
for(i=0;i<=n–1;i++)
{
sum=sum+a[i];
avg=sum;
avg=avg/n;
getch();
Output
Enter a value:7
Enter a value:6
Enter a value:5
Enter a value:9
#include<stdio.h>
#include<conio.h>
void main()
int n,i,a[100],even=0;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
for(i=0;i<=n–1;i++)
if (a[i]%2==0)
even++;
}
%d",even,(n–even));
getch();
Output
Enter a value:2
Enter a value:3
Enter a value:4
Enter a value:5
Enter a value:1
Enter a value:7
#include<stdio.h>
#include<conio.h>
void main()
int n,i,a[100],sum1=0,sum2=0,sum;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
for(i=0;i<=n–1;i++)
}
sum = sum1 – sum2 * sum2;
getch();
Output
Enter a value:1
Enter a value:2
Enter a value:3
Enter a value:4
Enter a value:5
Ø Algorithm
Step I : START
Step II : PRINT "Enter value of n".
Step III : INPUT n
Step IV : i=0
Step V : IF i > n – 1 THEN GOTO step X
Step VI : PRINT "Enter a value"
Step VII : INPUT a[i]
Step VIII : i=i+1
Step IX : GOTO step V
Step X : i = 0, sum = 0
Step XI : IF i > n – 1 THEN GOTO step XV
Step XII : sum = sum + a[i]
Step XIII : i=i+1
Step XIV : GOTO step XI
Step XV : avg = sum / n
Step XVI : i = 0, sum = 0
Step XVII : IF i > n – 1 THEN GOTO step XXI
Step XVIII : sum = sum + ( a[i] – avg ) * ( a[i] –
avg )
Step XIX : i=i+1
Step XX : GOTO step XVII
Step XXI : sum = sum / n
Step XXII : sum = square root of sum
Step XXIII : PRINT sum
Step XIV : STOP
Ø Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
int n,i,a[100],sum=0;
float avg,sum1=0,sd;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
sum=sum+a[i];
}
avg=sum;
avg=avg/n;
for(i=0;i<=n–1;i++)
sd = sum1/n;
sd = pow(sd,0.5);
getch();
Output
Enter a value:3
Enter a value:4
Enter a value:4
Enter a value:5
Enter a value:4
Ø Program
#include<stdio.h>
#include<conio.h>
void main( )
int n,i,a[100],large;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
large=a[0];
for(i=1;i<=n–1;i++)
if(large<a[i])
large=a[i];
getch();
Output
Enter a value:32
Enter a value:12
Enter a value:44
Enter a value:28
Enter a value:9
#include<stdio.h>
#include<conio.h>
void main()
int n,i,a[100],large;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
}
large=largest(a,n);
getch();
int i,large;
large=a[0];
for(i=1;i<=n–1;i++)
if(large<a[i])
large=a[i];
return large;
Output
Enter the number of elements:5
Enter a value:2
Enter a value:33
Enter a value:12
Enter a value:24
Enter a value:11
#include<stdio.h>
#include<conio.h>
void main()
int n,i,a[100],small;
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
small=smallest(a,n);
getch();
int i,small;
small=a[0];
for(i=1;i<=n–1;i++)
if(small>a[i])
small=a[i];
return small;
Output
Enter a value:12
Enter a value:32
Enter a value:7
Enter a value:123
Enter a value:76
#include<stdio.h>
#include<conio.h>
void main()
int n,i,a[100];
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
reverse(a,n);
getch();
int i,rev[100];
for(i=0;i<=n–1;i++)
rev[n–i–1]=a[i];
for(i=0;i<=n–1;i++)
printf("%d\n",rev[i]);
Output
Enter a value:23
Enter a value:43
Enter a value:12
Enter a value:22
22
12
43
23
#include<stdio.h>
#include<conio.h>
void main()
int n,i,a[100];
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
reverse(a,n);
getch();
int i,temp;
for(i=0;i<=(n–1)/2;i++)
temp=a[n–i–1];
a[n–i–1]=a[i];
a[i]=temp;
for(i=0;i<=n–1;i++)
printf("%d\n",a[i]);
Output
Enter a value:2
Enter a value:3
Enter a value:4
Enter a value:5
Enter a value:6
#include<stdio.h>
#include<conio.h>
void main()
int n,i,a[100],x,index;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
scanf("%d",&x);
index=search(a,n,x);
if(index==n)
printf("Not Found");
else
getch();
int i;
for(i=0;i<=n–1;i++)
if(x==a[i])
break;
return i;
}
Output
Enter a value:23
Enter a value:56
Enter a value:774
Enter a value:22
Enter a value:90
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[100],x,index;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
scanf("%d",&x);
index=search(a,n,x);
if(index==n)
printf("Not Found");
else
printf("The element %d is found in the index %d",x,index);
getch();
int i;
for(i=0;i<=n–1;i++)
if(x==a[i])
break;
return i;
#include<stdio.h>
#include<conio.h>
void main()
int n,i,a[100];
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
ascend(a,n);
getch();
int i,j,temp;
for(i=0;i<=n–2;i++)
for(j=0;j<=n–2;j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
printf("After sorting\n");
for(i=0;i<=n–1;i++)
printf("%d\n",a[i]);
Output
Enter a value:4
Enter a value:6
Enter a value:1
Enter a value:85
Enter a value:9
After sorting
85
#include<stdio.h>
#include<conio.h>
void main()
int n,i,a[100];
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
descend(a,n);
getch();
int i,j,temp;
for(i=0;i<=n–2;i++)
for(j=0;j<=n–2;j++)
if(a[j]<a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
printf("After sorting\n");
for(i=0;i<=n–1;i++)
{
printf("%d\n",a[i]);
Output
Enter a value:1
Enter a value:3
Enter a value:5
Enter a value:4
Enter a value:2
After sorting
1
Ø Program 6.1.15 : Write a program to sort float
numbers in descending order.
[ Note : Algorithm is same as program 6.1.14 ]
Program
#include<stdio.h>
#include<conio.h>
void main()
int n,i;
float a[100];
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%f",&a[i]);
}
descend(a,n);
getch();
int i,j;
float temp;
for(i=0;i<=n–2;i++)
for(j=0;j<=n–2;j++)
if(a[j]<a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
printf("After sorting\n");
for(i=0;i<=n–1;i++)
printf("%f\n",a[i]);
Output
Enter a value:1.1
Enter a value:3.4
Enter a value:0.3
Enter a value:2.7
After sorting
3.4
2.7
1.1
0.3
#include<conio.h>
void main()
int n,i,a[100],x,index,j;
clrscr();
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
scanf("%d",&x);
for(i=0;i<=n–1;i++)
if(x==a[i])
for(j=i;j<=n-2;j++)
a[j]=a[j+1];
}
n--;
for(i=0;i<=n-1;i++)
printf("%d\n",a[i]);
getch();
#include<stdio.h>
#include<conio.h>
void main()
int m,n,i,j,a[10][10];
clrscr();
scanf("%d %d",&m,&n);
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
printf("Enter a value:");
scanf("%d",&a[i][j]);
for(i=0;i<=m–1;i++)
{
for(j=0;j<=n–1;j++)
printf("%d\t",a[i][j]);
printf("\n");
getch();
Output
Enter a value:1
Enter a value:2
Enter a value:3
Enter a value:4
Enter a value:5
Enter a value:6
Enter a value:7
Enter a value:8
Enter a value:9
1 2 3
4 5 6
7 8 9
#include<stdio.h>
#include<conio.h>
void main()
int m,n,i,j,a[10][10],sum=0;
float avg;
clrscr();
scanf("%d %d",&m,&n);
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
printf("Enter a value:");
scanf("%d",&a[i][j]);
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
sum=sum+a[i][j];
}
avg=1.0 * sum/(m*n);
getch();
Output
Enter a value:1
Enter a value:2
Enter a value:3
Enter a value:4
Enter a value:5
Enter a value:6
#include<stdio.h>
#include<conio.h>
void main()
int m,n,i,j,a[10][10],large;
clrscr();
scanf("%d %d",&m,&n);
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
}
large=a[0][0];
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
if(large<a[i][j])
large=a[i][j];
getch();
Output
Enter a value:1
Enter a value:2
Enter a value:65
Enter a value:33
Enter a value:3
Enter a value:34
#include<stdio.h>
#include<conio.h>
void main()
int m,n,i,j,a[10][10],sum=0;
clrscr();
scanf("%d",&m);
n=m;
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
printf("Enter a value:");
scanf("%d",&a[i][j]);
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
if(i==j)
sum+=a[i][j];
}
}
getch();
Output
Enter a value:1
Enter a value:2
Enter a value:3
Enter a value:4
Enter a value:5
Enter a value:6
Enter a value:7
Enter a value:8
Enter a value:9
#include<stdio.h>
#include<conio.h>
void main()
int m,n,i,j,a[10][10],b[10][10],c[10][10];
clrscr();
scanf("%d %d",&m,&n);
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
printf("Enter a value:");
scanf("%d",&b[i][j]);
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
c[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
printf("%d\t",c[i][j]);
printf("\n");
getch();
Output
Enter a value:1
Enter a value:2
Enter a value:3
Enter a value:4
Enter a value:5
Enter a value:6
Enter a value:1
Enter a value:2
Enter a value:3
Enter a value:4
Enter a value:5
Enter a value:6
2 4 6
8 10 12
#include<stdio.h>
#include<conio.h>
void main()
int m,n,i,j,a[10][10];
clrscr();
scanf("%d %d",&m,&n);
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
printf("%d \t",a[i][j]);
printf("\n");
transpose(a,m,n);
getch();
{
int b[10][10],i,j;
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
b[j][i]=a[i][j];
for(i=0;i<=n–1;i++)
for(j=0;j<=m–1;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
Output
Enter a value:1
Enter a value:2
Enter a value:3
Enter a value:4
Enter a value:5
Enter a value:6
1 2
3 4
5 6
1 3 5
2 4 6
#include<stdio.h>
#include<conio.h>
void main()
int m,n,i,j,a[10][10];
clrscr();
scanf("%d",&m);
n=m;
for(i=0;i<=m–1;i++)
{
for(j=0;j<=n–1;j++)
printf("Enter a value:");
scanf("%d",&a[i][j]);
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
printf("%d\t",a[i][j]);
printf("\n");
transpose(a,m,n);
getch();
int i,j,temp;
for(i=0;i<=m–1;i++)
for(j=i;j<=n–1;j++)
temp=a[j][i];
a[j][i]=a[i][j];
a[i][j]=temp;
for(i=0;i<=m–1;i++)
{
for(j=0;j<=n–1;j++)
printf("%d\t",a[i][j]);
printf("\n");
Output
Enter a value:1
Enter a value:2
Enter a value:3
Enter a value:4
Enter a value:5
Enter a value:6
Enter a value:7
Enter a value:8
Enter a value:9
1 2 3
4 5 6
7 8 9
1 4 7
2 5 8
3 6 9
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,p,i,j,a[10][10],b[10][10];
clrscr();
scanf("%d %d",&m,&n);
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
printf("Enter a value:");
scanf("%d",&a[i][j]);
for(i=0;i<=n–1;i++)
for(j=0;j<=p–1;j++)
printf("Enter a value:");
scanf("%d",&b[i][j]);
MatMul(a,b,m,n,p);
getch();
int i,j,k,c[10][10];
for(i=0;i<=m–1;i++)
for(j=0;j<=p–1;j++)
c[i][j]=0;
for(k=0;k<=n–1;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
for(i=0;i<=m–1;i++)
for(j=0;j<=p–1;j++)
{
printf("%d\t",c[i][j]);
printf("\n");
Output
Enter a value:1
Enter a value:2
Enter a value:3
Enter a value:4
Enter a value:5
Enter a value:6
Enter a value:1
Enter a value:2
Enter a value:3
Enter a value:4
Enter a value:5
Enter a value:6
9 12 15
19 26 33
29 40 51
#include<stdio.h>
#include<conio.h>
void main()
{
char a[100];
clrscr();
printf("Enter a string\n");
gets(a);
getch();
Output
Enter a string
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
int l;
char a[100];
clrscr();
printf("Enter a string\n");
gets(a);
l=strlen(a);
getch();
Output
Enter a string
Hello
2. strcpy() function
This function copies the second string into the
first string passed to the function.
The second string remains unchanged. Only the
first string is changed and gets a copy of the
second string. For e.g. strcpy(str1,str2), will copy
the string “str2” into the string “str1”.
Ø Program 6.4.2 : Write a program to accept a
string, copy it into another string
and display this new string.
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
char a[100],b[100];
clrscr();
printf("Enter a string\n");
gets(a);
strcpy(b,a);
getch();
Output
Enter a string
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
char a[100],b[100];
clrscr();
gets(a);
gets(b);
if(strcmp(a,b)==0)
else if(strcmp(a,b)>0)
else
getch();
}
Output
Hello
Hi
Hi string is greater
4. strcat() function
This function concatenates (joins) the two string
variables passed to it. It returns a string of te
combination of the two in the first string
variable
Ø Program 6.4.4 : Write a program to accept two
strings, join them and display the
result.
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
char a[100],b[100];
clrscr();
gets(a);
gets(b);
strcat(a,b);
getch();
Output
Harish
Narula
#include<conio.h>
void main()
char a[12][10]=
{"January","February","March","April","May","June","July","August",
"September","October","November", "December"};
int m;
clrscr();
scanf("%d",&m);
getch();
Output
#include<conio.h>
#include<stdio.h>
void main()
char a[100];
int len=0;
clrscr();
printf("Enter a string:\n");
gets(a);
while(a[len]!='\0')
len++;
getch();
}
Output
Enter a string:
Hello
#include<conio.h>
#include<stdio.h>
void main()
char a[100];
int i,len=0,count=0;
clrscr();
printf("Enter a string:\n");
gets(a);
while(a[len]!=0)
len++;
for(i=0;i<=len–1;i++)
count++;
getch();
Output
Enter a string:
Hello
#include<stdio.h>
#include< conio.h>
Void main ( )
char a [100];
clrscr ( );
gets (a);
Count ++ ;
{
switch (a [i] )
break;
break;
break;
break;
break;
break;
break;
case ‘I’ = len ++;
break;
break;
break;
getch ( );
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
int l;
char a[100];
clrscr();
printf("Enter a string\n");
gets(a);
strrev(a);
getch();
Output
Enter a string
Hello
#include<stdio.h>
#include<conio.h>
void main()
int n=0;
char a[100];
clrscr();
printf("Enter a string:");
gets(a);
while(a[n]!='\0')
n++;
reverse(a,n);
getch();
int i;
char temp;
for(i=0;i<=(n–1)/2;i++)
temp=a[n–i–1];
a[n–i–1]=a[i];
a[i]=temp;
Output
Enter a string:Hello
#include<conio.h>
void main()
int n=0,i;
char a[100],temp;
clrscr();
printf("Enter a string:");
gets(a);
while(a[n]!='\0')
n++;
for(i=0;i<=(n–1)/2;i++)
{
temp=a[n–i–1];
a[n–i–1]=a[i];
a[i]=temp;
getch();
Output
Enter a string:Hello
#include<stdio.h>
#include<conio.h>
void main()
int n=0,i;
char a[100],rev[100];
clrscr();
printf("Enter a string:");
gets(a);
while(a[n]!='\0')
n++;
for(i=0;i<=(n–1);i++)
rev[n–i–1]=a[i];
for(i=0;i<=n–1;i++)
{
if(a[i]!=rev[i])
break;
if(i==n)
else
getch();
Output
Enter a string:nitin
#include<conio.h>
#include<stdio.h>
void main()
int n=0,i,j,k;
char a[100],rev[100];
clrscr();
printf("Enter a string:");
gets(a);
while(a[n]!='\0')
n++;
for(i=0,j=0;i<=n;i++)
{
for(k=0;j<=i;k++,j++)
rev[n–i+k]=a[j];
rev[k–1]=' ';
rev[n]='\0';
getch();
Output
#include<conio.h>
#include<stdio.h>
void main()
int n,i,a[100],temp;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value");
scanf("%d",&a[i]);
temp=a[n–1];
for(i=n–1;i>0;i––)
{
a[i]=a[i–1];
a[0]=temp;
for(i=0;i<=n–1;i++)
printf("%d\n",a[i]);
getch();
Output
Enter a value1
Enter a value2
Enter a value3
Enter a value4
Enter a value5
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
int i,fact=1,N,sign=–1;
float x,numerator,sum,term;
clrscr();
scanf("%f %d",&x,&N);
sum=SUMFUN(x,N);
getch();
int i,fact=1,sign=–1;
float numerator,sum,term;
term=x;
sum=term;
for(i=3;i<=N;i=i+2)
{
fact=fact*i*(i–1);
numerator=pow(x,i);
term=numerator/fact;
sign=sign*–1;
return(sum);
Output
#include<stdio.h>
#include<conio.h>
void main()
int m,n,i,j,a[10][10],b[10];
clrscr();
scanf("%d %d",&m,&n);
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
printf("Enter a value:");
scanf("%d",&a[i][j]);
for(i=0;i<=n–1;i++)
{
b[i]=0;
for(j=0;j<=m–1;j++)
b[i]=b[i]+a[j][i];
for(i=0;i<=m–1;i++)
for(j=0;j<=n–1;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("The sum of the columns are:\n");
for(i=0;i<=n–1;i++)
printf("%d\t",b[i]);
getch();
Output
Enter a value:1
Enter a value:2
Enter a value:3
Enter a value:4
Enter a value:5
Enter a value:6
Enter a value:7
Enter a value:8
Enter a value:9
1 2 3
4 5 6
7 8 9
12 15 18
#include<conio.h>
#include<stdio.h>
void main()
char a[100];
int i,len=0,vowels=0,spaces=0,digits=0,consonants=0;
clrscr();
printf("Enter a string:\n");
gets(a);
while(a[len]!=0)
len++;
for(i=0;i<=len–1;i++)
vowels++;
else
consonants++;
else
if(a[i]>='0' &&a[i]<='9')
digits++;
else
if(a[i]==' ')
spaces++;
getch();
Output
Enter a string:
#include<conio.h>
void main()
int n=0;
char a[100],b[100];
clrscr();
printf("Enter a string:");
gets(a);
while(a[n]!='\0')
n++;
copy(a,&b[0],n);
join(a,&b[0],n,n);
getch();
{
int i;
for(i=0;i<=n;i++)
*(p+i)=a[i];
int i;
for(i=n;i<=m+n;i++)
*(p+i)=a[i–n];
Output
Enter a string:HGN
#include<stdio.h>
#include<conio.h>
void main()
int n,i,a[100],sum;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
}
sum=addition(a,n);
getch();
for(i=1;i<=n–1;i++)
return sum;
Output
Enter a value:32
Enter a value:12
Enter a value:44
Enter a value:28
Enter a value:9
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[100], new[100], choice, temp;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
scanf("%d",&choice);
switch(choice)
for(i=n–1;i>=1;i––)
{
a[i] = a[i–1];
a[0] = temp;
for(i=0;i<=n–1;i++)
printf("%d\n",a[i]);
for(i=0;i<=n–2;i++)
a[i] = a[i+1];
a[n–1] = temp;
for(i=0;i<=n–1;i++)
{
printf("%d\n",a[i]);
getch();
#include<stdio.h>
#include<conio.h>
void main()
int m,n,i,j,a[10][10];
scanf("%d %d",&m,&n);
for(i=0;i<=m-1;i++)
for(j=0;j<=n-1;j++)
printf("Enter a value:");
scanf("%d",&a[i][j]);
for(i=0;i<=m-1;i++)
for(j=0;j<=n-1;j++)
{
printf("%d \t",a[i][j]);
printf("\n");
transpose(a,m,n);
getch();
int b[10][10],i,j,flag=0;
for(i=0;i<=m-1;i++)
for(j=0;j<=n-1;j++)
b[j][i]=a[i][j];
}
}
for(i=0;i<=n-1;i++)
for(j=0;j<=m-1;j++)
printf("%d\t",b[i][j]);
printf("\n");
for(i=0;i<=n-1;i++)
for(j=0;j<=m-1;j++)
if(a[i][j]!=b[i][j]) flag++;
}
}
Ø Program
#include<stdio.h>
#include<conio.h>
void main()
int i,j,a[100],sum;
float avg;
clrscr();
for(j=1;j<=4;j++)
sum=0;
for(i=0;i<=5;i++)
printf("Enter a value:");
scanf("%d",&a[i]);
sum=sum+a[i];
avg=sum;
avg=avg/n;
getch();
}
Ø Program 6.6.10 : Write a program in C to accept
an ARRAY A with n elements and
Separate it into two different
arrays B and C in such a way that
B contains Odd numbers and C
contains Even numbers. i.e, if
ARRAY A contains A={3,2,4,2,5,7,8}
then B={3,5,7} and C={2,4,2,8}. (May
2016)
#include <stdio.h>
void main()
int i,=0,=0, n;
scanf("%d", &n);
scanf("%d", &A[i]);
}
if (A[i] % 2 == 0)
C[j]=[i];
j++;
else
B[k]=[i];
k++;
printf("%d\n", B[i]);
printf("%d\n", C[i]);
getch();
#include<stdio.h>
longfact(int);
intmain(){
intline,i,j;
printf("Enter the no. of lines: ");
scanf("%d",&line);
for(i=0;i<line;i++){
for(j=0;j<line-i-1;j++)
printf(" ");
for(j=0;j<=i;j++)
printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
printf("\n");
return0;
longfact(intnum){
longf=1;
inti=1;
while(i<=num){
f=f*i;
i++;
returnf;
Output
11
121
1331
14641
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
#include<conio.h>
int i,j;
for(i=1;i<=row;i++)
scanf("%d",&a[i][j]);
}
void multiply_matrix(int m1[10][10],int m2[10][10],int m3[10][10],int
row,int col)
int i,j,k;
for(i=1;i<=row;i++)
for(j=1;j<=col;j++)
for (k=1;k<=row;k++)
{
int i,j;
for(i=1;i<=row;i++)
for(j=1;j<=col;j++)
printf("%d ",m[i][j]);
printf("\n");
void main()
int m1[10][10],m2[10][10],m3[10][10],m,n;
clrscr();
scanf("%d",&m);
printf("Enter number of colomns :");
scanf("%d",&n);
read_matrix(m1,m,n);
read_matrix(m2,m,n);
multiply_matrix(m1,m2,m3,m,n);
display_matrix(m3,m,n);
getch();
#include<stdio.h>
void main()
clrscr();
char a[100], b[100];
gets(a);
gets(b);
strcat(a,b);
getch();
char c[100],*z;
z=c;
while( *p != ‘/0’)
*z=*p;
p++;
z++;
*z=*q;
q++;
z++;
*z= ‘/0’;
(ii) strlen
#include<stdio.h>
void main()
void strlen(char * );
clrscr();
char a[100];
gets(a);
strlen(a);
getch();
int c=0;
while( *p != ‘/0’)
c++;
p++;
}
Syllabus Topic : Pointer - Introduction, Definition
and uses of Pointers , Pointer Variables, Void
Pointer, Array of Pointers
#include<stdio.h>
#include<conio.h>
void main()
int a,*p;
clrscr();
a=125;
p=&a;
printf("%d\n",a);
printf("%x\n",p);
printf("%d\n",*p);
getch();
Output
125
Address of variable a
125
#include<conio.h>
void main()
int a,*p,**p1;
clrscr();
a=125;
p=&a;
p1=&p;
printf("%d\n",a);
printf("%x\n",p);
printf("%x\n",p1);
printf("%d\n",*p);
printf("%x\n",*p1);
printf("%d\n",**p1);
getch();
Output
125
Address of variable a
125
Address of variable a
125
Syllabus Topic : Pointer - Pointer Arithmetic
#include<stdio.h>
#include<conio.h>
void main()
int a,*a1;
float b,*b1;
clrscr();
a1=&a;
b1=&b;
printf("%x \n%x\n",a1,b1);
a1++;
b1++;
printf("%x \n%x\n",a1,b1);
getch();
}
Output
Address of variable a
Address of variable b
ip= & x;
y=(*ip)++;
y=++x (*ip);
Output
20
21
22
22
int arr[4];
Fig. 6.8.1
#include<stdio.h>
#include<conio.h>
void main()
int i,a[2]={10,20};
clrscr();
for(i=0;i<=1;i++)
printf("%d\n",a[i]);
printf("%d\n",*(a+i));
printf("%d\n",*(i+a));
getch();
}
Output
10
10
10
20
20
20
#include<conio.h>
void main()
int a,b;
void sum (int *p1, int *p2);
clrscr();
scanf("%d %d",&a,&b);
sum(&a,&b);
getch();
int c;
c=*p1+*p2;
Output
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
scanf("%d %d",&a,&b);
swap(a,b);
getch();
int temp;
temp=a;
a=b;
b=temp;
Output
The values of a and b in the main function before calling the swap
function are 4 and 5
The values of a and b in the swap function after swapping are 5 and 4
The values of a and b in main function after calling the swap function
are 4 and 5
#include<conio.h>
void main()
int a,b;
clrscr();
scanf("%d %d",&a,&b);
swap(&a,&b);
getch();
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
Output
The values of a and b in the main function before calling the swap
function are 4 and 5
The values of a and b in the swap function after swapping are 5 and 4
The values of a and b in main function after calling the swap function
are 5 and 4
#include<stdio.h>
#include<conio.h>
void main ()
int *p,i,a[10];
clrscr();
p=&a;
printf("Enter 10 nos.\n");
for(i=0;i<=9;i++)
scanf("%d",(p+i));
for(i=0;i<=9;i++)
printf("%d\n",*(p+i));
getch();
Output
Enter 10 nos.
2
3
10
8
9
10
#include<stdio.h>
#include<conio.h>
void main ()
int *p,i,sum=0,a[10];
float avg;
clrscr();
p=a;
printf("Enter 10 nos.\n");
for(i=0;i<=9;i++)
scanf("%d",(p+i));
sum=sum+*(p+i);
avg=sum/10.0;
for(i=0;i<=9;i++)
printf("%d\n",*(p+i));
printf("Average=%f",avg);
getch();
Output
Enter 10 nos.
7
8
10
10
Average=5.5
struct structure_name
data_type variable_name;
};
For example a structure to store the some
information like name, roll number and fees of a student
in college can be declared as shown below.
struct student
char name[20];
int roll_no;
float fees;
};
#include<conio.h>
#include<stdio.h>
struct student
char name[20];
int roll_no;
float fees;
};
void main ()
clrscr();
gets(s1.name);
scanf("%d %f",&s1.roll_no,&s1.fees);
getch();
Output
Enter the student's name, roll number and fees paid:John Patrick
24
106000
Name:John Patrick
Roll number:24
Fees:106000
#include<stdio.h>
struct cricketer
char name[20];
};
void main ()
clrscr();
scanf("%d %d",&c1.runs,&c1.wickets);
getch();
Output
20000
150
Name:Sachin Tendulkar
Runs scored:20000
Wickets taken:150
#include<conio.h>
#include<stdio.h>
struct hockey
char name[20];
};
void main ()
clrscr();
gets(c1.name);
scanf("%d %d",&c1.matches,c1.goals);
getch();
Output
Enter the hockey player's name, matches played and goals scored:V.
Raghunath
67
41
Name:V. Raghunath
Matches played:67
Goals scored:41
#include<conio.h>
#include<stdio.h>
struct student
char name[20];
int roll_no;
int physics,chem,maths;
};
void main ()
int n,i,j;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
{
printf("Enter the student's name, roll number and marks in three
subjects:");
printf("Name\tRoll No\tPhysics\tChem\tMaths\n");
printf("----------------------------------------------\n");
for(i=0;i<=n–1;i++)
st[i].maths);
getch();
Output
Enter the student's name, roll number and marks in three subjects:Jay
1
40
40
80
Enter the student's name, roll number and marks in three subjects:Ajay
12
49
49
99
Enter the student's name, roll number and marks in three subjects:Vijay
24
45
45
90
5
50
50
100
---------------------------------------
Jay 1 40 40 80
Ajay 12 49 49 99
Vijay 24 45 45 90
Sujoy 5 50 50 100
#include<conio.h>
#include<stdio.h>
struct cricketer
char name[20];
int runs, wickets;
};
void main ()
int n,i;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
scanf("%s %d %d",c[i].name,&c[i].runs,&c[i].wickets);
printf("Name\tRuns\tWickets\n");
printf("-----------------------------------\n");
for(i=0;i<=n–1;i++)
printf("%s\t%d\t%d\n",c[i].name,c[i].runs,c[i].wickets);
getch();
Output
20000
150
500
500
10000
0
-----------------------------------
Dhoni 10000 0
#include<stdio.h>
struct student
char name[20];
int roll_no;
int physics,chem,maths,total;
};
void main ()
int n,i,j;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
}
for(i=0;i<=n–1;i++)
for(j=0;j<=n–2;j++)
if(s[j].total<s[j+1].total)
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
printf("Name\tRoll No\tPhysics\tChem\tMaths\tTotal\n");
printf("----------------------------------------------\n");
for(i=0;i<=n–1;i++)
getch();
Output
Enter the student's name, roll number and marks in three subjects:Jay
40
40
80
Enter the student's name, roll number and marks in three subjects:Ajay
12
49
49
99
Enter the student's name, roll number and marks in three subjects:Vijay
24
45
45
90
50
50
100
----------------------------------------------
Ajay 12 49 49 99 197
Vijay 24 45 45 90 180
Jay 1 40 40 80 160
#include<conio.h>
#include<stdio.h>
struct hockey
char name[20];
int matches,goals;
};
void main ()
int n,i,j;
clrscr();
printf("Enter the number of hockey players:");
scanf("%d",&n);
for(i=0;i<=n–1;i++)
for(i=0;i<=n–1;i++)
for(j=0;j<=n–2;j++)
if(s[j].goals<s[j+1].goals)
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
printf("Name\t\tMatches\tGoals\n");
printf("----------------------------------\n");
for(i=0;i<=n–1;i++)
printf("%s %d %d\n",s[i].name,s[i].matches,s[i].goals);
getch();
Output
100
500
Enter the player's name, matches played and goals scored:BalbirSingh
75
450
67
761
120
1058
----------------------------------
AjitPalSingh 67 761
BalbirSingh 75 450
#include<conio.h>
#include<stdio.h>
struct cricketer
char name[20];
int matches,runs;
};
void main ()
int n,i,j;
clrscr();
printf("Enter the number of cricketers:");
scanf("%d",&n);
for(i=0;i<=n–1;i++)
scanf("%s %d %d",s[i].name,&s[i].matches,&s[i].runs);
for(i=0;i<=n–1;i++)
for(j=0;j<=n–2;j++)
if(s[j].runs<s[j+1].runs)
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
printf("Name\t\tMatches\tRuns\n");
printf("----------------------------------\n");
for(i=0;i<=n–1;i++)
printf("%s\t%d\t%d\n",s[i].name,s[i].matches,s[i].runs);
getch();
Output
171
14006
125
10122
144
11581
131
11953
156
11174
----------------------------------
struct structure_name
data_type variable_name;
struct
data_type variable_name;
-
-
} internal_structure_name;
};
float radius;
struct
float x,y;
}centre;
#include<conio.h>
#include<stdio.h>
struct circle
float radius;
struct
float x,y;
}centre;
};
void main ()
struct circle c;
clrscr();
scanf("%f %f %f",&c.radius,&c.centre.x,&c.centre.y);
getch();
Output
2.3
3.6
Circle Information
Radius=5.6
Centre co-ordinates:(2.3,3.6)
Ø Program 6.11.2 : Write a nested structure to
store the information of a book i.e.
its title, author’s name, publisher’s
name, edition and ISBN number.
The author’s name and publisher’s
name must be a structure with two
elements each namely first name
and surname.
struct book
char title[20];
struct
char f_name[20],surname[20];
}author;
struct
char f_name[20],surname[20];
}publisher;
int edition;
float ISBN;
};
Q. What is Unions ?
Ans. :
Union is a collection of multiple data elements that
can be of different data types. But, only one of these
data items can be stored in the union variable.
The memory space required to store one variable of
union is equal to the memory space required by the
largest element in the union.
Let us see the syntax of a union declaration.
Syntax of structure declaration:
union union_name
data_type variable_name;
-
};
char name[20];
int roll_no;
float id;
};
#include<conio.h>
#include<stdio.h>
union info
{
char name[20];
long id;
};
void main ()
int choice;
clrscr();
scanf("%d",&choice);
switch(choice)
scanf("%s",i1.name);
break;
break;
default:printf("Invalid Choice");
if(choice==1)
else if(choice==2)
getch();
Output
1. Name
2.ID number
#include<conio.h>
#include<stdio.h>
struct hockey
char name[20],country[20];
int matches,goals;
};
void main ()
int n,i,j,k;
clrscr();
scanf("%d",&n);
for(i=0;i<=n–1;i++)
scanf("%s %s %d %d",s[i].name,s[i].country,&s[i].matches,&s
[i].goals);
for(i=0;i<=n–1;i++)
for(j=0;j<=n–2;j++)
{
if(s[j].goals<s[j+1].goals)
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
printf("------------------------------------------\n");
for(i=0;i<=n–1;i++)
for(i=0;i<=n–1;i++)
{
for(j=0;j<=n–2;j++)
if(s[j].matches<s[j+1].matches)
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
printf("------------------------------------------\n");
for(i=0;i<=n–1;i++)
for(i=0;i<=n–1;i++)
for(j=0;j<=n–2;j++)
for(k=0;s[j].name[k]!='\0';k++)
if(s[j].name[k]!=s[j+1].name[k])
if(s[j].name[k]>s[j+1].name[k])
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
break;
}
printf("\nList according to
Name\nName\t\tCountry\tMatches\tGoals\n");
printf("------------------------------------------\n");
for(i=0;i<=n–1;i++)
for(i=0;i<=n–1;i++)
for(j=0;j<=n–2;j++)
for(k=0;s[j].name[k]!='\0';k++)
{
if(s[j].country[k]!=s[j+1].country[k])
if(s[j].country[k]>s[j+1].country[k])
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
break;
printf("\nList according to
Country\nName\t\tCountry\tMatches\tGoals\n");
printf("------------------------------------------\n");
for(i=0;i<=n–1;i++)
getch();
Output
Sweden
705
208
USA
407
160
Canada
915
285
---------------------------------------------
---------------------------------------------
----------------------------------
----------------------------------
#include<stdio.h>
struct employee
char name[20];
int salary,code;
struct
int date,month,year;
}date_joining;
};
void main ()
int i;
clrscr();
for(i=0;i<=9;i++)
scanf("%s %d %d %d %d %d",e[i].name,&e[i].code,&e[i].salary,
&e[i].date_joining.date,
&e[i].date_joining.month,&e[i].date_joining.year);
printf("---------------------------------------------------\n");
for(i=0;i<=9;i++)
printf("%s\t%d\t%d\t%d/%d/%d\n",e[i].name,e[i].code,e[i].salary,
e[i].date_joining.date,e[i].date_joining.month,e[i].date_joining.year);
getch();
}
Output
Enter the employee's name, code, salary and date of joining as date,
month and year separately:Manish
0001
30000
2005
Enter the employee's name, code, salary and date of joining as date,
month and year separately:Satish
0002
25000
2009
Enter the employee's name, code, salary and date of joining as date,
month and year separately:Girish
0003
29500
10
2009
Enter the employee's name, code, salary and date of joining as date,
month and year separately:Sunit
0004
21000
10
2010
Enter the employee's name, code, salary and date of joining as date,
month and year separately:sumit
0005
29300
1
2001
Enter the employee's name, code, salary and date of joining as date,
month and year separately:saumil
0006
26636
12
2000
Enter the employee's name, code, salary and date of joining as date,
month and y ear separately:santosh
0007
12500
2011
Enter the employee's name, code, salary and date of joining as date,
month and year separately:Prashant
8
18250
2011
Enter the employee's name, code, salary and date of joining as date,
month and year separately:Ruchi
12500
2011
Enter the employee's name, code, salary and date of joining as date,
month and year separately:Rashmi
10
12000
2009
Employee list
---------------------------------------------------
#include<conio.h>
#include<stdio.h>
struct employee
char name[20];
int salary,exp,number;
};
void main ()
int i;
clrscr();
for(i=0;i<=9;i++)
scanf("%s %d %d
%d",e[i].name,&e[i].number,&e[i].exp,&e[i].salary);
printf("--------------------------\n");
for(i=0;i<=9;i++)
printf("%s\t%d\n",e[i].name,e[i].number);
getch();
Output
1234123
30000
Enter the employee's name, number, exp and salarySatish
1235151
12
12000
12414515
5000
12424352
9000
123135213
12000
Enter the employee's name, number, exp and salarySaumil
12414124
26636
124124
8500
1241241
8500
1241221
9900
Enter the employee's name, number, exp and salaryAjay
7893911
16
8900
Name Number
--------------------------
Sunit 12424352
Prashant 1241241
Rahul 1241221
Ajay 7893911
#include<stdio.h>
struct cricketer
int best;
float avg;
};
void main ()
int i,j;
float dummy;
clrscr();
for(i=0;i<=24;i++)
{
scanf("%s %s %d
%f",c1[i].name,c1[i].country,&c1[i].best,&dummy);
c1[i].avg=dummy;
for(i=0;i<=24;i++)
for(j=0;j<=23;j++)
if(c1[j].avg<c1[j+1].avg)
temp=c1[j];
c1[j]=c1[j+1];
c1[j+1]=temp;
}
}
for(i=0;i<=24;i++)
getch();
Output
Enter the cricketer's name, country, best score and average runs
scored:Sachin
India
200
45.16
Enter the cricketer's name, country, best score and average runs
scored:Jayasuriya
SriLanka
189
32.36
Enter the cricketer's name, country, best score and average runs
scored:Ponting
Australia
164
42.69
Enter the cricketer's name, country, best score and average runs
scored:Inzamam
Pakistan
137
39.52
Enter the cricketer's name, country, best score and average runs
scored:Ganguly
India
183
41.02
Enter the cricketer's name, country, best score and average runs
scored:Kallis
SouthAfrica
139
45.45
Enter the cricketer's name, country, best score and average runs
scored:Dravid
India
153
39.43
Enter the cricketer's name, country, best score and average runs
scored:Lara
WestIndies
169
40.48
Enter the cricketer's name, country, best score and average runs
scored:Jayawardene
SriLanka
144
33.44
Enter the cricketer's name, country, best score and average runs
scored:Yousuf
Pakistan
141
41.71
Enter the cricketer's name, country, best score and average runs
scored:Gilchrist
Australia
172
35.89
Enter the cricketer's name, country, best score and average runs
scored:Sangakkara
SriLanka
138
38.15
Enter the cricketer's name, country, best score and average runs
scored:Azharuddin
India
153
36.92
Enter the cricketer's name, country, best score and average runs
scored:DeSilva
SriLanka
145
34.90
Enter the cricketer's name, country, best score and average runs
scored:Anwar
Pakistan
194
39.21
Enter the cricketer's name, country, best score and average runs
scored:Chanderpaul
WestIndies
150
41.60
Enter the cricketer's name, country, best score and average runs
scored:Haynes
WestIndies
152
41.37
Enter the cricketer's name, country, best score and average runs
scored:Atapattu
SriLanka
132
37.57
Enter the cricketer's name, country, best score and average runs
scored:Waugh
Australia
173
39.35
Enter the cricketer's name, country, best score and average runs
scored:Gibbs
SouthAfrica
175
36.13
Enter the cricketer's name, country, best score and average runs
scored:Gayle
WestIndies
153
39.06
Enter the cricketer's name, country, best score and average runs
scored:Yuvraj
India
139
37.62
Enter the cricketer's name, country, best score and average runs
scored:Fleming
NewZealand
134
32.40
Enter the cricketer's name, country, best score and average runs
scored:Shewag
India
175
35.11
Enter the cricketer's name, country, best score and average runs
scored:Waugh
Australia
120
32.9
#include<stdio.h>
struct employee
char name[20];
int salary,code;
};
void main ()
int i,j;
clrscr();
for(i=0;i<=99;i++)
{
scanf("%s %d %d ",e[i].name,&e[i].code,&e[i].salary);
for(i =0;i<=98;i++)
temp=e[j];
e[j]=e[j+1];
e[j+1]=temp;
}
printf("\nEmployee list\nName\tCode\tSalary\n");
printf("---------------------------------------------------\n");
for(i=0;i<=99;i++)
printf("%s\t%d\t%d\n",e[i].name,e[i].code,e[i].salary);
getch();
Output
Enter the employee's name, code, salary and date of joining as date,
month and year separately:Manish
0001
30000
2005
Enter the employee's name, code, salary and date of joining as date,
month and year separately:Satish
0002
25000
2009
Enter the employee's name, code, salary and date of joining as date,
month and year separately:Girish
0003
29500
10
2009
Enter the employee's name, code, salary and date of joining as date,
month and year separately:Sunit
0004
21000
10
9
2010
Enter the employee's name, code, salary and date of joining as date,
month and year separately:sumit
0005
29300
2001
Enter the employee's name, code, salary and date of joining as date,
month and year separately:saumil
0006
26636
12
2000
Enter the employee's name, code, salary and date of joining as date,
month and y ear separately:santosh
0007
12500
2011
Enter the employee's name, code, salary and date of joining as date,
month and year separately:Prashant
18250
2011
Enter the employee's name, code, salary and date of joining as date,
month and year separately:Ruchi
12500
1
2011
Enter the employee's name, code, salary and date of joining as date,
month and year separately:Rashmi
10
12000
2009
Employee list
---------------------------------------------------
struct cricketer
char name[20];
float average;
};
#include<stdio.h>
#include<conio.h>
struct patient
char fname[20],sname[20],mname[20],birthdate[20],disease[20];
};
void main ()
{
struct patient s[100];
int n,i,j,k;
char d[20];
clrscr();
scanf("%d",&n);
for(i=0;i<=n-1;i++)
scanf("%s %s %s %s
%s",s[i].fname,s[i].mname,s[i].sname,s[i].birthdate,s[i].disease);
scanf("%s",d);
for(j=0;j<=n-1;j++)
{
for(k=0;d[k]!='\0';k++)
if(d[k]!=s[j].disease[k])
break;
if(d[k]=='\0') printf("%s %s
%s\n",s[j].fname,s[j].mname,s[j].sname);
getch();
#include<conio.h>
#include<stdio.h>
struct employee
char name[20],dept[20];
int salary;
struct
int date,month,year;
}date_joining;
};
void main ()
int i;
clrscr();
for(i=0;i<=9;i++)
scanf("%s %s %d %d %d %d",e[i].name,&e[i].dept,&e[i].salary,
&e[i].date_joining.date, &e[i].date_joining.month,
&e[i].date_joining.year);
printf("---------------------------------------------------\n");
for(i=0;i<=9;i++)
if(e[i].salary>50000)
printf("%s\t%d\t%d\t%d/%d/%d\n",e[i].name,e[i].dept,e[i].salary,
e[i].date_joining.date,e[i].date_joining.month,e[i].date_joining.year);
getch();
}
Output
Computers
60000
2005
Computers
25000
2009
69500
10
2009
Mechanical
21000
10
2010
Electronics
29300
1
1
2001
Civil
26636
12
2000
Mechanical
12500
2011
88250
2011
Computers
12500
2011
Civil
12000
1
2009
Employee list
---------------------------------------------------
# include<stdio.h>
# include<conio.h>
# include "malloc.h"
struct student
int roll_no;
float percentage;
};
void main()
int i;
struct student p;
float dummy;
clrscr();
for(i=0;i<=9;i++)
scanf("%d %f",&s[i].roll_no,&dummy);
s[i].percentage=dummy;
if(i<9)
s[i].next=&s[i+1];
else
s[i].next=0;
p.next=&s[0];
printf("Roll No.\tPercentage\n");
while(p.next!=0)
p=*p.next;
printf("%d\t\t%f\n",p.roll_no,p.percentage);
getch();
Output
98
76
87
93
81
90
67
37
1 98
2 56
3 76
4 87
5 93
6 81
7 90
8 67
9 37
10 75
Q. Explain how to read the contents of file and write into the
file with Syntax. (May 2013)
OR What do you mean by FILE ? What are the different
functions available to read data from the file ? Specify the
different modes in which files can be opened along with
syntax. (Dec. 2013, May 2014, Dec. 2014, Dec. 2015, May
2016)
Ans. :
For file handling or accessing the contents of file,
there are certain predefined functions available in
the C programming language.
An important thing required to access files is the
"FILE pointer". This pointer is used to point to the
values stored in the file. A file pointer is hence to be
created for accessing the files. The syntax for creating
a file pointer is as given below:
FILE *<identifier for pointer>;
For e.g. FILE *fp;
Hence in every program we write in this section to
access files, we will use this kind of pointer
declaration. This pointer is used to point the data to
be accessed in the file i.e. whenever a data is read or
written in the file, it is from the location pointed by
the file pointer "fp".
Let us see these functions and there use in the
programming.
1. fopen()
This function is used to open a file to be accessed
in the program. The file to be opened is to be
passes as a string parameter to the function and
also the mode of opening the file is to be passed
as the string to the function. Hence the syntax of
the function with parameters is as given below :
<file pointer identifier> = fopen("<file name>", "
<mode of opening the file>")
For e.g. fp=fopen("test.txt","w");
This example statement opens the file "test.txt"
in write mode and the pointer used along with
the function to read/write is the file pointer "fp".
the various modes in which a file can be opened
are as listed below :
(i) "r" indicates that the file is to be opened
indicates in the read mode.
(ii) "w" indicates that the file is to be opened
indicates in the write mode. When a file is
opened in write mode the data written in
the file overwrites the previously stored
data in the file.
(iii) "a" indicates that the file is to be opened in
the append mode. In this mode the data
written into the file is appended towards
the end of the already stored data in the
file. The earlier stored data remains as it is.
(iv) "w+" indicates that the file is to be opened
in write and read mode
(v) "r+" indicates that the file is to be opened
in read and write mode
2. fclose()
This function is used to close the file opened
using the file pointer passed to the function. The
syntax with parameters to call this function is as
given below :
fclose(<file pointer identifier>);
For e.g. fclose(fp);
This statement closes the file opened using the
file pointer variable "fp". It closes the file opened
using the function fopen(). The file opened to be
accessed in whatsoever mode doesn't matters, it
is closed using the fclose() function.
3. feof()
This function returns true or false based on
whether the pointer pointing to the file has
reached the end of the file or not. the pointer
used to point the file has to be passed as a
parameter to the function feof().
Also the file has to be opened pointed using the
pointer "fp", before using this function. The
syntax of the function with the parameters is as
shown below :
feof(<file pointer identifier>);
For e.g. feof(fp);
4. fputc()
This function is used to put a character type data
into the opened file using the fopen() function,
pointed by a file pointer.
The character to be put into the file as well as
the file pointer are to be passed as the
parameters to this function.
The syntax to call this function along with the
parameters to be passed is as shown below :
fputc(<char type data>, <file pointer
identifier>);
For e.g. : fputc(c,fp);
This example will store the character value of
the char type data variable "c" into the opened
file and pointed by the file pointer fp, at the
position pointed by the pointer fp in the file.
5. getc()
This function is used to get a character from the
file pointed by the corresponding file pointer
passed to the function. It is exactly opposite the
fputc() function.
This function brings the character from the file
opened and pointed by the file pointer variable
passed to the function.
The syntax of the function call with the
parameters to be passed is as given below :
getc(<file pointer identifier>);
For e.g. getc(fp);
This function brings the character type data from
the opened file using the pointer fp; from the
location pointed by the pointer "fp" in that file.
6. rewind()
This function is used to rewind or bring the file
pointer variable to point to the beginning of the
file from wherever it is currently pointing in the
file. The syntax of the function call with the
paramters to be passed is as given below:
rewind(<file pointer identifier>);
For e.g. rewind(fp);
This function rewinds or brings back the pointer
"fp" to the beginning of the file from wherever it
was pointing in the file opened using the pointer
"fp".
7. fprintf()
This function is used to store the different data
types in the file as the fputc() function is used to
store the character in the file.
This can be used to store integer, float, string
etc types of data into the file opened. The
function is similar to printf(), except that it
writes to the file instead of the monitor.
The other difference is in the syntax, that the
file pointer variable is also to be passed to the
function along with the data types and the
parameter values or the variables. The syntax
shown below explains the concept:
fprintf(<file pointer identifier>, "<format
specifiers>",<variable names>);
For e.g.: fprintf(fp,"%d",x);
This function will print or store the value of the
integer variable "x" in the file opened using the
pointer "fp". The data will be stored at the
location pointed by the pointer variable "fp".
8. fscanf()
The function is used to read the different types of
data as the getc() function is used to read a
character from the file.
This function can be used to read an integer,
float string etc types of data into the file opened.
The function is similar to scanf(), except that it
reads from the file instead of the keyboard.
The other difference is in the syntax, that the file
pointer variable is also to be passed to the
function along with the data types and the
parameter values or the variables. The syntax
shown below explains the concept:
fscanf(<file pointer variable>,"<format
specifiers>",<address of the variables in which
the data is to be read>);
For e.g.: fscanf(fp,"%d",&x);
This function will read the integer type data
from the file pointed by the file pointer variable
"fp" and store this value in the variable "x". The
value will be brought from the file at the position
pointed by the file pointer in the file.
Ø Program 6.15.1 : Write a program to accept a set
of characters from user until the
user presses the full stop (' . ') and
store it in a text file. Read from
the file and display the contents of
the file. (May 2013)
# include<stdio.h>
# include<conio.h>
void main()
FILE *fp;
clrscr();
fp=fopen("test.txt","w");
printf("Write data to be stored in the file and once completed press the
full stop (.):\n");
while(c!='.')
scanf("%c",&c);
fputc(c,fp);
fclose(fp);
fp=fopen("test.txt","r");
while(!feof(fp))
printf("%c",getc(fp));
fclose(fp);
getch();
Output
Write data to be stored in the file and once completed press the full
stop (.):
# include<stdio.h>
# include<conio.h>
void main()
FILE *fp;
clrscr();
fp=fopen("test.txt","r");
while(!feof(fp))
{
printf("%c",getc(fp));
fclose(fp);
fp=fopen("test.txt","a");
while(c!='.')
scanf("%c",&c);
fputc(c,fp);
fclose(fp);
fp=fopen("test.txt","r");
while(!feof(fp))
printf("%c",getc(fp));
}
fclose(fp);
getch();
Output
Write data to be stored in the file and once completed press the full
stop (.):
This is a program that appends the previously added data in the file
test.
# include<stdio.h>
# include<conio.h>
void main()
{
FILE *fp;
clrscr();
fp=fopen("test.txt","r");
while(!feof(fp))
printf("%c",getc(fp));
fclose(fp);
fp=fopen("test.txt","w+");
while(c!='.')
scanf("%c",&c);
fputc(c,fp);
}
rewind(fp);
while(!feof(fp))
printf("%c",getc(fp));
fclose(fp);
getch();
Output
Write data to be stored in the file and once completed press the full
stop (.):
# include<stdio.h>
# include<conio.h>
void main()
FILE *fp;
char name[20],temp1[20];
int roll,temp2;
clrscr();
fp=fopen("Student.txt","w+");
scanf("%s %d",name,&roll);
fprintf(fp,"%s %d",name,roll);
printf("Name\tRoll no.\n");
rewind(fp);
fscanf(fp,"%s %d",temp1,&temp2);
printf("%s\t%d",temp1,temp2);
fclose(fp);
getch();
Output
24
Ajay 24
# include<stdio.h>
# include<conio.h>
void main()
FILE *fp;
char name[20];
int roll,choice=0;
clrscr();
while(choice!=3)
scanf("%d",&choice);
switch(choice)
case 1: fp=fopen("Student.txt","a");
scanf("%s %d",name,&roll);
fprintf(fp,"%s %d",name,roll);
fclose(fp);
break;
case 2: fp=fopen("Student.txt","r");
printf("Name\tRoll no.\n");
while(!feof(fp))
fscanf(fp,"%s %d",name,&roll);
printf("%s\t%d\n",name,roll);
fclose(fp);
break;
case 3: break;
getch();
}
Output
1.New Entry
3.Exit
Ajay 24
Hiral 33
Harish 24
Khushi 55
1.New Entry
3.Exit
1.New Entry
3.Exit
23
1.New Entry
3.Exit
Ajay 24
Hiral 33
Harish 24
Khushi 55
Girish 11
Harsha 23
1.New Entry
3.Exit
# include<stdio.h>
# include<conio.h>
void main()
FILE *fp,*fp1;
char c;
clrscr();
fp=fopen("test.txt","r");
fp1=fopen("test1.txt","w");
while(!feof(fp))
c=getc(fp);
fputc(c,fp1);
fclose(fp1);
fclose(fp);
fp1=fopen("test1.txt","r");
while(!feof(fp1))
printf("%c",getc(fp1));
fclose(fp1);
getch();
}
Output
# include<stdio.h>
# include<conio.h>
void main()
FILE *fp;
int n=0;
clrscr();
fp=fopen("test.txt","r");
while(!feof(fp))
getc(fp);
n++;
n--;
fclose(fp);
getch();
Output
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
char ch;
clrscr();
while (1) {
ch = fgetc(fp1);
ch=ch+32;
if (ch == EOF)
break;
else
putc(ch, fp2);
fclose(fp2);
❏❏❏