0% found this document useful (0 votes)
159 views91 pages

Programming Through 'C' Language

This document provides a detailed table of contents and reference for a programming guide through C language. It includes 27 conceptual programs covering basic input/output, mathematical operations, conditional statements, and more. It also lists 34 sample programs for common tasks like area calculations, number comparisons, arrays, strings, file handling and more. The programming guide covers key C concepts like control statements, functions, recursion, searching, sorting, pointers, structures and linked lists in detail across multiple pages.

Uploaded by

ahky7
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
159 views91 pages

Programming Through 'C' Language

This document provides a detailed table of contents and reference for a programming guide through C language. It includes 27 conceptual programs covering basic input/output, mathematical operations, conditional statements, and more. It also lists 34 sample programs for common tasks like area calculations, number comparisons, arrays, strings, file handling and more. The programming guide covers key C concepts like control statements, functions, recursion, searching, sorting, pointers, structures and linked lists in detail across multiple pages.

Uploaded by

ahky7
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 91

PROGRAMMING THROUGH C LANGUAGE

Paritosh Mohapatra
Roll No.-9
A-Level
OCAC
paritoshohapatra!"ahoo.#o
CONTENT$
Sl. No. Subjects Page No.
1. Conceptual 4
2. Control Statements 7
3. Triangles 23
4. Arrays 26
. !atri" 3#
6. String or Array o$ C%aracters 32
7. Number System 37
&. 'unction 4#
(. )ecursion 4
1#. Sorting 4&
11. Searc%ing #
12. Pointers 2
13. Structure (
14. *in+ *ist 6#
1. 'ile 74
16. ,-.ACC ."amination /uestions 0 Ans1ers 76
2
%&i#' Re(ere)#e
Sl. No. Programs Page No.
1. Area -$ Circle. 6
2. C%ec+ing 'or *eap 2ear. 11
3. Printing T%e -33 0 .4en Numbers. 16
4. Sum -$ ,igits. 1
. )e4ersing A Number. 15 46
6. .4aluating "
n
. 1&5 425 475 &1
7. Calculating 'actorial. 1(5 45 77
&. 6enerating 'ibonacci Series. 1(5 4
(. 6enerating ASC77 C%art. 1&
1#. Prime Numbers. 2#5 215 43
11. Armstrong Numbers. 23
12. Palin3rome Numbers. 225 7
13. Printing T%e 'loy38s Triangles. 26
14. Computing !ean5 9ariance 0 Stan3ar3 ,e4iation. 2(
1. Searc% 'or T%e :ig%est 0 *o1est Number 7n An Array. 27
16. Con4erting A String To *o1er Case. 36
17. Con4erting A String To ;pper Case. 36
1&. Con4erting A String To 'irst Case. 37
1(. )e4ersing A String. 335 7
2#. C%ec+ing 'or String Palin3rome. 34
21. Counting Number -$ 9o1els 7n A String. 3
22. Counting Number -$ <or3s 7n A String. 345 3
23. Con4ersion -$ ,ecimal To =inary Number. 37
24. Con4ersion -$ =inary To ,ecimal Number. 3(
2. Con4ersion -$ ,ecimal To -ctal Number. 3&
26. Con4ersion -$ ,ecimal To :e"a3ecimal Number. 3&
27. 6enerating Number System. &(
2&. T%e To1er -$ :anoi. 47
2(. Sorting An Array ;sing =ubble Sort Algorit%m. 4&5 &4
3#. Sorting An Array ;sing /uic+ Sort Algorit%m. 4(
31. Searc%ing A >ey 9alue 7n An Array ;sing Se?uential Searc%. #
32. Searc%ing A >ey 9alue 7n An Array ;sing ,ictionary Searc%. 1
33. Concatenating T1o Strings.
34. Processing A *in+ *ist. 6#5 715 73
3. 7nserting A Ne1 No3e 7n A *in+ *ist. 625 635 64
36. ,eleting A No3e 'rom A *in+ *ist. 675 6&5 7#
37. Counting T%e No3es -$ A *in+ *ist. 61
3&. 6enerating A =oo+ Structure. (
3(. )ea3ing T%e Contents -$ A 'ile. 74
4#. <riting To A 'ile. 74
3
Conceptual
1) Write a program to print HELLO WORLD.
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf("\nHELLO WORLD");
getch();
}
2) Write a program to compute the addition, subtraction, product, quotient and
remainder of two given numbers.
#include <stdio.h>
#include <conio.h>
void main()
{
int n1,n2,add,sub,prod,quot,remain;
clrscr();
printf("\nENTER NUMBER-1: " );
scanf("%d",&n1);
printf("\nENTER NUMBER-2: " );
scanf("%d",&n2);
add=n1+n2;
sub=n1-n2;
prod=n1*n2;
quot=n1/n2;
remain=n1%n2;
printf("\nADDTON OF THE NUMBERS: %d",add);
printf("\nSUBSTRACTON OF THE NUMBERS: %d",sub);
printf("\nPRODUCTON OF THE NUMBERS: %d",prod);
printf("\nQUOTENT OF THE NUMBERS: %d",quot);
printf("\nREMANDER OF THE NUMBERS: %d",remain);
getch();
}
3) Write a program to compute the average of three given numbers.
#include <stdio.h>
#include <conio.h>
void main()
{
int n1,n2,n3;
4
float avg;
clrscr();
printf("\nENTER THREE NUMBERS: " );
scanf("%d %d %d",&n1,&n2,&n3);
avg=(n1+n2+n3)/3;
printf("\nAVERAGE: %0.2f",avg);
getch();
}
4) Write a program to compute the seconds from a given age.
#include <stdio.h>
#include <conio.h>
void main()
{
long unsigned age,sec;
clrscr();
printf("\nENTER YOUR AGE: ");
scanf("%lu",&age);
sec=age*365*24*60*60;
printf("\nAGE N SECONDS: %lu",sec);
getch();
}
5) Write a program to compute simple interest and compound interest from the
given principal, time period and interest rate.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int p,t;
float si,ci,r,i;
clrscr( );
printf("\nENTER PRNCPAL: ");
scanf("%d",&p);
printf("\nENTER TME PEROD: ");
scanf("%d",&t);
printf("\nENTER RATE OF NTEREST: ");
scanf("%f",&r);
si=(p*t*r)/100;
i=r/100;
ci=p*pow((1+i),t);
printf("\nSMPLE NTEREST: %0.2f",si);
printf("\n\nCOMPOUND NTEREST: %0.2f",ci);

getch();
}
Note: - To use the pow() function <math.h> header file must be included.
6) Write a program to compute the area of a circle from the given radius.
Hint: - Area of the circle= x (radius)
2
.
#include <stdio.h>
#include <conio.h>
void main()
{
float pi=3.14,rad,area;
clrscr();
printf("\nENTER THE RADUS OF THE CRCLE: ");
scanf("%f",&rad);
area=pi*rad*rad;
printf("\nTHE AREA OF THE CRCLE S: %0.2f",area);
getch();
}
7) Write a program to swap the values of two variables.
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nENTER TWO NUMBERS FOR a AND b:\n");
scanf("%d %d",&a,&b);
printf("\nBEFORE SWAPNG THE VALUE OF a=%d AND b=%d",a,b);
c=a;
a=b;
b=c;
printf("\nAFTER SWAPNG THE VALUE OF a=%d AND b=%d",a,b);
getch();
}
8) Write a program to swap the values of two variables without using a third
variable.
#include <stdio.h>
#include <conio.h>
void main()
{
6
int a,b;
clrscr();
printf("\nENTER TWO NUMBERS FOR a AND b:\n");
scanf("%d %d",&a,&b);
printf("\nBEFORE SWAPNG THE VALUE OF a=%d AND b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAFTER SWAPNG THE VALUE OF a=%d AND b=%d",a,b);
getch();
}
Control Statements
A) Conditional Control Statements
1) If Conditions
1) Write a program to compute net amount from the given quantity purchased
and rate per quantity. Discount @10% is allowed if quantity purchased exceeds
100.
Net Amount = (Quantity Purchased x Rate Per Quantity) Discount.
#include <stdio.h>
#include <conio.h>
void main()
{
int qty,rate;
float disc=0.0,net;
clrscr();
printf("\nENTER QUANTTY: ");
scanf("%d",&qty);
printf("\nENTER RATE: ");
scanf("%d",&rate);
if (qty>100)
disc=qty*rate*10/100;
net=(qty*rate)-disc;
printf("\nNET AMOUNT: %0.2f",net);
getch();
}
2) Find out the highest number from three given numbers.
#include <stdio.h>
#include <conio.h>
7
void main()
{
int a,b,c,h;
clrscr();
printf("\nENTER THREE NUMBERS:\n");
scanf("%d %d %d",&a,&b,&c);
h=a;
if(b>h)
h=b;
if(c>h)
h=c;
printf("\nHGHEST NUMBER S %d",h);
getch();
}
Alternative Method
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nENTER THREE NUMBERS:\n");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("\nHGHEST NUMBER S %d",a);
if(b>c && b>a)
printf("\nHGHEST NUMBER S %d",b);
if(c>a && c>b)
printf("\nHGHEST NUMBER S %d",c);
getch();
}
3) Find out the lowest number from three given numbers.
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c,l;
clrscr();
printf("\nENTER THREE NUMBERS:\n");
scanf("%d %d %d",&a,&b,&c);
l=a;
if(b<l)
&
l=b;
if(c<l)
l=c;
printf("\nLOWEST NUMBER S %d",l);
getch();
}
Alternative Method
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nENTER THREE NUMBERS:\n");
scanf("%d %d %d",&a,&b,&c);
if(a<b && a<c)
printf("\nLOWEST NUMBER S %d",a);
if(b<c && b<a)
printf("\nLOWEST NUMBER S %d",b);
if(c<a && c<b)
printf("\nLOWEST NUMBER S %d",c);
getch();
}
4) Write a program to check a given number is odd or even.
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&a);
if(a%2==0)
printf("\nTHE NUMBER S AN EVEN NUMBER");
else
printf("\nTHE NUMBER S AN ODD NUMBER");
getch();
}
5) Write a program to find out a person is insured or not. The person is insured if
following conditions are satisfied: -
(
i) f the person is married.
ii) f the person is unmarried, male & above 30 years of age.
iii) f the person is unmarried, female & above 25 years of age.
n all other cases the person is not insured.
#include <stdio.h>
#include <conio.h>
void main()
{
char ms,sex;
int age;
clrscr();
printf("\nEnter Marital Status [M/U],Sex [M/F],Age: ");
scanf("%c %c %d",&ms,&sex,&age);
if((ms=='M') || (ms=='U' && age>30 && sex=='M') || (ms=='U' && age>25
&& sex=='F'))
printf("\nTHE EMPLOYEE S NSURED");
else
printf("\nTHE EMPLOYEE S NOT NSURED");
getch();
}
6) Write a program to find out the gross amount from the given basic pay.
Gross = Basic + DA + HRA
DA & HRA can be calculated as follows: -
f Basic Pay is greater than or equal to 8000 DA is 20% of Basic Pay & HRA is
25% of Basic Pay, otherwise DA is 15% of Basic Pay & HRA is 20% of Basic
Pay.
#include <stdio.h>
#include <conio.h>
void main()
{
float basic,da,hra,gross;
clrscr();
printf("\nENTER BASC PAY: ");
scanf("%f",&basic);
if(basic>=8000)
{
da=basic*20/100;
hra=basic*25/100;
}
1#
else
{
da=basic*15/100;
hra=basic*20/100;
}
gross=basic+da+hra;
printf("\nGROSS AMOUNT: %0.2f",gross);
getch();
}
7) Write a program to compute the division from the given marks of 5 subjects.
The division can be calculated as follows: -
Average Mark Division
>=60 First
>=50 Second
>=40 Third
<40 Fail
#include <stdio.h>
#include <conio.h>
void main()
{
int m1,m2,m3,m4,m5,per;
clrscr();
printf("\nENTER THE MARKS OF THE SUBJECTS:\n");
scanf("%d %d %d %d %d",&m1,&m2,&m3,&m4,&m5);
per=(m1+m2+m3+m4+m5)/5;
if(per>=60)
printf("\nFRST DVSON");
else
{
if(per>=50)
printf("\nSECOND DVSON");
else
{
if(per>=40)
printf("\nTHRD DVSON");
else
printf("\nFAL");
}
}
getch();
}
8) Write a program to check whether a given year is leap year or not.
11
#include <stdio.h>
#include <conio.h>
void main()
{
int year,n;
clrscr();
printf("\nENTER A YEAR: ");
scanf("%d",&year);
if(year%4==0 && year%100 !=0 || year%400==0)
printf("\n%d S A LEAP YEAR",year);
else
printf("\n%d S NOT A LEAP YEAR",year);
getch();
}
2) Switch Case
1) Write a program to ask the user to enter a number. f the number is 1 print
One, if the number is 2 print Two, if the number is 3 print Three, otherwise
print Other than One, Two or Three.
#include <conio.h>
#include <stdio.h>
void main()
{
int i;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&i);
switch(i)
{
case 1:
printf("\nNUMBER S ONE");
break;
case 2:
printf("\nNUMBER S TWO");
break;
case 3:
printf("\nNUMBER S THREE");
break;
default:
printf("\nNUMBER S OTHER THAN ONE, TWO OR
THREE");
}
getch();
12
}
2) Write a program to ask the user to enter a character. f the user enter 'a' or 'A'
print The Character is a or A, if the user enter 'b' or 'B' print The Character is b
or B, if the user enter 'c' or 'C' print The Character is c or C, otherwise print
Other than a, b, c, A, B, C.
#include <stdio.h>
#include <conio.h>
void main()
{
char ch;
clrscr();
printf("\nEnter A Character: ");
scanf("%c",&ch);
switch(ch)
{
case 'a':
case 'A':
printf("\nCharacter s a Or A");
break;
case 'b':
case 'B':
printf("\nCharacter s b Or B");
break;
case 'c':
case 'C':
printf("\nCharacter s c or C");
break;
default:
printf("\nCharacter s Other Than a,b,c,A,B,C");
}
getch();
}
B) Loop Control Statements
1) While Loop
1) Write a program to print natural numbers up to a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1,n;
13
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
printf("\nNATURAL NUMBERS UPTO %d ARE:\n",n);
while(i<=n)
{
printf("%d ",i);
i=i+1;
}
getch();
}
2) Write a program to ask the user to enter a series of marks of a student. f the
user enters 1, come out of the loop and print the average mark.
#include <stdio.h>
#include <conio.h>
void main()
{
int mark,num=0;
float sum,avg=0.0;
clrscr();
printf("\nEnter Mark Or -1 To Quit: \n");
scanf("%d",&mark);
while(mark!=-1)
{
sum+=mark;
num+=1;
scanf("%d",&mark);
}
avg=sum/num;
printf("\nAverage Mark: %0.2f",avg);
getch();
}
3) Write a program to ask the user for a number. Print the square of the number
and ask a confirmation from the user whether the user want to continue or not.
#include <stdio.h>
#include <conio.h>
void main()
{
char ch='y';
int num;
clrscr();
while (ch=='y'|| ch=='Y')
14
{
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
printf("\nTS SQUARE S: %d\n",num*num);
printf("\nDO YOU WANT TO CONTNUE [Y/N]: ");
ch=getche();
}
getch();
}
2) Do While Loop
1) Write a program to print the sum of digit of a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int digit;
unsigned long num,sum=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%lu",&num);
do
{
digit=num%10;
num=num/10;
sum=sum+digit;
}
while(num!=0);
printf("\nSUM OF THE DGTS S %lu",sum);
getch();
}
2) Write a program to print the reverse of a given number.
Example: - Reverse of 2565 is 5652.
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long num,rev;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%lu",&num);
1
printf("\nTHE REVERSE OF THE NUMBER %lu S ",num);
do
{
rev=num%10;
num=num/10;
printf("%lu",rev);
}
while(num!=0);
getch();
}
3) For Loop
1) Write a program to print the odd numbers within a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
printf("\nODD NUMBERS BETWEEN 1 AND %d ARE: \n",n);
for(i=1;i<=n;i+=2)
{
printf("%d ",i);
}
getch();
}
2) Write a program to print the even numbers within a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
printf("\nEVEN NUMBERS BETWEEN 1 AND %d ARE: \n",n);
for(i=2;i<=n;i+=2)
{
printf("%d ",i);
}
16
getch();
}
3) Write a program to add the odd numbers within a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n;
unsigned long sum=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
for(i=1;i<=n;i+=2)
{
sum=sum+i;
}
printf("\nSUM OF THE ODD NUMBERS BETWEEN 1 TO %d S
%lu",n,sum);
getch();
}
4) Write a program to add the even numbers within a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n;
unsigned long sum=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
for(i=2;i<=n;i+=2)
{
sum=sum+i;
}
printf("\nSUM OF THE EVEN NUMBERS BETWEEN 1 TO %d S
%lu",n,sum);
getch();
}
5) Write a program to print the multiplication table of a given number.
#include <stdio.h>
17
#include <conio.h>
void main()
{
int n,i;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
printf("\nMULTPLCATON TABLE OF %d\n",n);
for(i=1;i<=10;i++)
{
printf("\n%2d x %2d = %3d",n,i,n*i);
}
getch();
}
6) Write a program to print the ASC chart within a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int n,i;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
for(i=0;i<=n;i++)
printf("%d=%c ",i,i);
getch();
}
7) Write a program to evaluate x
n
.
#include <stdio.h>
#include <conio.h>
void main()
{
int num,temp,power,i;
unsigned long res=1;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
printf("\nENTER THE POWER: ");
scanf("%d",&power);
temp=num;
for(i=1;i<=power;i++)
{
1&
res=res*num;
}
printf("\nTHE %d POWER OF %d S %lu",power,temp,res);
getch();
}
8) Write a program to evaluate
1
/3 +
2
/5 +
3
/7 .. +
n
/((n*2) + 1).
#include <stdio.h>
#include <conio.h>
void main()
{
float n,i,sum=0.0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%f",&n);
for(i=1;i<=n;i++)
sum=sum+(i/((i*2)+1));
printf("\nTHE SUMMATON S: %0.2f",sum);
getch();
}
9) Write a program to compute the factorial of a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1,n;
unsigned long fact=1;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
for(i=1;i<=n;i+=1)
{
fact=fact*i;
}
printf("\nFACTORAL OF %d S %lu",n,fact);
getch();
}
10) Write program to print the fibonacci series up to a given number.
#include <stdio.h>
#include <conio.h>
void main()
1(
{
int num,i;
unsigned long n1=0,n2=1,s;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
printf("\nFBONACC SERES UPTO %d NUMBERS S: \n",num);
printf("%lu %lu",n1,n2);
for(i=1;i<=num-2;i++)
{
s=n1+n2;
printf(" %lu",s);
n1=n2;
n2=s;
}
getch();
}
11) Write a program to evaluate
1
/1 Factorial +
2
/2 Factorial +
n
/n Factorial.
#include <stdio.h>
#include <conio.h>
void main()
{
float n,i,j,sum=0.0,fact=1.0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%f",&n);
for(i=1;i<=n;i++)
{
fact=1.0;
for(j=1;j<=i;j++)
{
fact=fact*j;
}
sum=sum+(i/fact);
}
printf("\nTHE SUMMATON S: %0.2f",sum);
getch();
}
C) Combination of Loop Conditional Statements
1) Write a program to print the prime numbers within a given number.
#include <stdio.h>
2#
#include <conio.h>
main()
{
int n,i,j,c;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
printf("\nPRME NUMBERS WTHN %d\ ARE:\n",n);
for(i=1;i<=n;i++)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
if(c==2)
printf("%d ",i);
}
getch();
}
2) Write a program to check a given number is prime or not.
#include <stdio.h>
#include <conio.h>
void main()
{
int n,i,c=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
printf("\n%d S A PRME NUMBER",n);
else
printf("\n%d S NOT A PRME NUMBER",n);
getch();
}
Alternative Method
21
#include <stdio.h>
#include <conio.h>
void main()
{
int n,i,flag=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i!=0)
flag=1;
}
if(flag==0)
printf("\n%d S A PRME NUMBER",n);
else
printf("\n%d S NOT A PRME NUMBER",n);
getch();
}
3) Write a program to check a given number is Palindrome or not. A number is
said to be Palindrome if the reverse of the number is equal to the number.
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long num,temp,pal,sum=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%lu",&num);
temp=num;
do
{
pal=num%10;
num=num/10;
sum=(sum*10)+pal;
}
while(num!=0);
if(temp==sum)
printf("\n%lu S A PALNDROM NUMBER",temp);
else
printf("\n%lu S NOT A PALNDROM NUMBER",temp);
getch();
}
22
4) Write a program to check a given number is Armstrong or not. A number is
said to be Armstrong if sum of the cube of the individual digit is equal to the
number.
Example: - 153 = (1)
3
+ (5)
3
+ (3)
3
.
#include <stdio.h>
#include <conio.h>
void main()
{
int num,num1,digit,arm=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
num1=num;
do
{
digit=num%10;
num=num/10;
arm=arm+(digit*digit*digit);
}
while(num!=0);
if(arm==num1)
printf("\n%d S AN AMSTORNG NUMBER",num1);
else
printf("\n%d S NOT AN AMSTRONG NUMBER",num1);
getch();
}
Triangles
1) Write a program to print a triangle like the following: -
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
#include <stdio.h>
#include <conio.h>
void main()
{
int num,i,j;
clrscr();
printf("\nENTER THE NUMBER OF LNES: ");
23
scanf("%d",&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
{
printf("1 ");
}
printf("\n");
}
getch();
}
2) Write a program to print a triangle like the following: -
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include <stdio.h>
#include <conio.h>
void main()
{
int num,i,j;
clrscr();
printf("\nENTER THE NUMBER OF LNES: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
getch();
}
3) Write a program to print a triangle like the following: -
1
2 3
4 5 6
7 8 9 10
11 12 13 14
24
#include <stdio.h>
#include <conio.h>
void main()
{
int num,i,j,k=1;
clrscr();
printf("\nENTER THE NUMBER OF LNES: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",k);
k++;
}
printf("\n");
}
getch();
}
4) Write a program to print a triangle like the following: -
1
1 1 1
1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
#include <stdio.h>
#include <conio.h>
void main()
{
int num,i,j,k,s=40;
clrscr();
printf("\nENTER THE NUMBER OF LNES: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=s;j++)
printf(" ");
for(k=1;k<=i;k++)
printf("1");
for(k=i-1;k>0;k--)
printf("1");
printf("\n");
2
s--;
}
getch();
}
5) Write a program to print a triangle (Floyd's Triangle) like the following: -
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
#include <stdio.h>
#include <conio.h>
void main()
{
int num,i,j,k,s=40;
clrscr();
printf("\nENTER THE NUMBER OF LNES: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=s;j++)
printf(" ");
for(k=1;k<=i;k++)
printf("%d",k);
for(k=i-1;k>0;k--)
printf("%d",k);
printf("\n");
s--;
}
getch();
}
Arrays
1) Write a program to create an array. Print the values and addresses of each
elements of the array.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[5];
int i,n=5;
26
clrscr();
for(i=0;i<n;i++)
{
printf("\nENTER THE NUMBER-%d: ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
printf("ARRAY ELEMENT-%d: VALUE=%d, ADDRESS=
%u\n",i+1,a[i],&a[i]);
getch();
}
2) Write a program to create an array of student's ages. Print the average age.
#include <stdio.h>
#include <conio.h>
void main()
{
int age[10],i ;
float avg,sum=0.0;
clrscr();
for(i=0;i<10;i++)
{
printf("\nENTER AGE-%d: ",i+1);
scanf("%d",&age[i]);
sum=sum+age[i];
}
printf("\nNPUT AGES ARE: ");
for(i=0;i<10;i++)
printf("%d ",age[i]);
avg=sum/10;
printf("\nTHE AVERAGE AGE S: %0.2f",avg);
getch();
}
3) Write a program to create an array. Print the highest and lowest number in the
array.
#include <stdio.h>
#include <conio.h>
void main()
{
int val[5],h,l,i;
clrscr();
for(i=0;i<5;i++)
{
27
printf("\nENTER VALUE-%d: ",i+1);
scanf("%d",&val[i]);
}
l=val[0];
h=val[0];
for(i=0;i<5;i++)
{
if(val[i]>h)
h=val[i];
else
{
if(val[i]<l)
l=val[i];
}
}
printf("\nHGHEST VALUE S %d",h);
printf("\nLOWEST VALUE S %d",l);
getch();
}
4) Write a program to insert a given number in the array in a given position.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[5],i,j=5,k,val,pos;
clrscr();
for(i=0;i<5;i++)
{
printf("\nENTER NUMBER-%d: ",i+1);
scanf("%d",&a[i]);
}
printf("\nENTER VALUE TO NSERT: ");
scanf("%d",&val);
printf("\nENTER POSTON TO NSERT: ");
scanf("%d",&pos);
j++;
for(k=j;k>=pos;k--)
{
a[k]=a[k-1];
}
a[--pos]=val;
printf("\nTHE ARRAY S: ");
for(i=0;i<j;i++)
{
2&
printf("\n%d",a[i]);
}
getch();
}
5) Write a program to create an array. Compute the Mean, Variance & Standard
Deviation of the array.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int a[10],i,n=5;
float mean,temp,var,sd,sum=0.0;
clrscr();
for(i=0;i<n;i++)
{
printf("\nENTER NUMBER-%d: ",i+1);
scanf("%d",&a[i]);
sum=sum+a[i];
}
mean=sum/n;
sum=0.0;
for(i=0;i<n;i++)
{
temp=a[i]-mean;
sum=sum+(temp*temp);
}
var=sum/n;
sd=sqrt(var);
printf("\nMEAN = %0.2f",mean);
printf("\nVARANCE = %0.2f",var);
printf("\nSTANDARD DAVATON = %0.2f",sd);
getch();
}
Note: - To use the sqrt() function <math.h> header file must be included.
6) There are 5 groups of employees in an organization. Write a program to draw
a histogram showing given number of employees in each group.
#include <stdio.h>
#include <conio.h>
#define n 5
void main()
2(
{
int g[n],i,j;
clrscr();
for(i=0;i<n;i++)
{
printf("\nENTER HOW MANY EMPLOYEES ARE N GROUP-%d:
",i+1);
scanf("%d",&g[i]);
}
printf("\n");
for(i=0;i<n;i++)
{
printf("GROUP-%d",i+1);
printf("%c",221);
for(j=0;j<=g[i];j++)
{
printf("%c",220);
}
printf(" %d",g[i]);
printf("\n");
}
getch();
}
Matrix
1) Write a program to create a matrix and display a matrix.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[2][3],i,j;
clrscr();
printf("\nENTER VALUES FOR THE MATRX:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nTHE VALUES OF THE MATRX ARE:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%5d",a[i][j]);
printf("\n");
}
getch();
3#
}
2) Write a program to create two matrixes. Add the values of the two matrixes
and store it in another matrix. Display the new matrix.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[2][3],b[2][3],c[2][3],i,j;
clrscr();
printf("\nENTER VALUES FOR MATRX A:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nENTER VALUES FOR MATRX B:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<2;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nTHE VALUES OF MATRX C ARE:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%5d",c[i][j]);
printf("\n");
}
getch();
}
3) Write a program to create two matrixes. Multiply the values of the two matrixes
and store it in another matrix. Display the new matrix.
#include <stdio.h>
#include <conio.h>
#define max 3
void main()
{
int a[max][max],b[max][max],c[max][max],i,j,k;
printf("\nENTER VALUES FOR MATRX-A\n");
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
31
printf("ENTER VALUE FOR A(%d,%d): ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\nENTER VALUES FOR MATRX-B\n");
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
printf("ENTER VALUE FOR B(%d,%d): ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
c[i][j]=0;
for(k=0;k<max;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
printf("\nVALUES OF MATRX-A\n");
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\nVALUES OF MATRX-B\n");
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
printf("\nVALUES OF MATRX-C\n");
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
32
{
printf("%d ",c[i][j]);
}
printf("\n");
}
getch();
}
4) Write a program to create a matrix. Add the diagonal elements of the matrix.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[3][3],trace=0,i,j;
clrscr();
printf("\nENTER VALUES OF THE MATRX:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nTHE VALUES OF THE MATRX ARE:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%5d",a[i][j]);
printf("\n");
}
for(i=0;i<3;i++)
trace+=a[i][i];
printf("THE SUMMATON OF THE DAGONAL ELEMENTS OF THE
MATRX S %d",trace);
getch();
}
String or Array of Characters
1) Write a program which will accept Name, Age, Sex and Qualification and
display the same.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int i,j;
char label[5][20]={"NAME","AGE","SEX","QUALFCATON"},a[5][10];
33
clrscr();
for(i=0;i<4;i++)
{
printf("\nENTER YOUR %s: ",label[i]);
gets(a[i]);
}
for(i=0;i<4;i++)
printf("\n%s S YOUR %s\n",a[i],label[i]);
getch();
}
2) Write a program to find out the length of a given string without using the library
function strlen().
#include <stdio.h>
#include <conio.h>
void main()
{
char str[50];
int len;
clrscr();
printf("\nENTER A STRNG: ");
gets(str);
for(len=0;str[len]!=NULL;len++);
printf("\nTHE LENGTH OF THE STRNG S %d",len);
getch();
}
3) Write a program to print the reverse of a given string.
#include <stdio.h>
#include <conio.h>
void main()
{
char ch[100];
int i,len;
clrscr();
printf("\nENTER A STRNG: ");
gets(ch);
len=strlen(ch);
printf("\nTHE STRNG N THE REVERSE ORDER: ");
for(i=len-1;i>=0;i--)
printf("%c",ch[i]);
getch();
}
34
4) Write a program to check if a given string is palindrome or not. A string is said
to be palindrome if the reverse of the string is equal to the string.
#include <stdio.h>
#include <conio.h>
void main()
{
char a[100];
int i,len,flag=0;
clrscr();
printf("\nENTER A STRNG: ");
gets(a);
len=strlen(a);
for(i=0;i<len;i++)
{
if(a[i]==a[len-i-1])
flag=flag+1;
}
if(flag==len)
printf("\nTHE STRNG S PALNDROM");
else
printf("\nTHE STRNG S NOT PALNDROM");
getch();
}
5) Write a program to count the number of words including spaces and excluding
spaces in a given string. Two words are separated by single space.
#include <stdio.h>
#include <conio.h>
void main()
{
char a[100];
int len,i,sp=0;
clrscr();
printf("\nENTER A STRNG: ");
gets(a);
for(len=0;len<=a[len];len++);
for(i=0;i<len;i++)
{
if(a[i]==' ')
sp=sp+1;
}
printf("\nNUMBER OF WORDS NCLUDNG SPACES: %d",len);
printf("\nNUMBER OF WORDS EXCLUDNG SPACES: %d",len-sp);
getch();
3
}
6) Write a program to count the number of vowels in a given string.
#include <stdio.h>
#include <conio.h>
void main()
{
char a[100];
int len,i,vow=0;
clrscr();
printf("\nENTER A STRNG: ");
gets(a);
len=strlen(a);
for(i=0;i<len;i++)
{
if(a[i]=='a' || a[i]=='A' || a[i]=='e' || a[i]=='E' || a[i]=='i' || a[i]=='' ||
a[i]=='o' || a[i]=='O' || a[i]=='u' || a[i]=='U')
vow=vow+1;
}
printf("\nTHERE ARE %d VOWELS N THE STRNG",vow);
getch();
}
7) Write a program to count the number of words in a given string. Two words are
separated by one or more blank spaces.
#include <stdio.h>
#include <conio.h>
void main()
{
char a[100];
int len,i,word=1;
clrscr();
printf("\nENTER A STRNG: ");
gets(a);
len=strlen(a);
for(i=0;i<len;i++)
{
if(a[i]!=' ' && a[i+1]==' ')
word=word+1;
}
printf("\nTHERE ARE %d WORDS N THE STRNG",word);
getch();
}
36
8) Write a program to print a given string in lower case.
#include <stdio.h>
#include <conio.h>
void main()
{
char s[100];
int len,i;
clrscr();
printf("\nENTER A SENTENSE: ");
gets(s);
len=strlen(s);
printf("\n");
for(i=0;i<len;i++)
{
if(s[i]>=65 && s[i]<=90)
printf("%c",s[i]+32);
else
printf("%c",s[i]);
}
getch();
}
9) Write a program to print a given string in upper case.
#include <stdio.h>
#include <conio.h>
void main()
{
char s[100];
int len,i;
clrscr();
printf("\nENTER A SENTENSE: ");
gets(s);
len=strlen(s);
printf("\n");
for(i=0;i<len;i++)
{
if(s[i]>=97 && s[i]<=122)
printf("%c",s[i]-32);
else
printf("%c",s[i]);
}
getch();
}
37
10) Write a program to print the string in first case.
#include <stdio.h>
#include <conio.h>
void main()
{
char s[100];
int len,i;
clrscr();
printf("\nENTER A SENTENSE: ");
gets(s);
len=strlen(s);
printf("\n");
for(i=0;i<len;i++)
{
if((i==0 && s[i]>=97 && s[i]<=122) || (s[i-1]==32 && s[i]>=97 &&
s[i]<=122))
printf("%c",s[i]-32);
else
{
if(i!=0 && s[i-1]!=32 && s[i]>=65 && s[i]<=90)
printf("%c",s[i]+32);
else
printf("%c",s[i]);
}
}
getch();
}
Number System
1) Write a program to convert a decimal number to its equivalent binary number.
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long dec;
int a[25],c=0,i;
clrscr();
printf("\nENTER A DECMAL NUMBER: ");
scanf("%lu",&dec);
printf("\n%lu N BNARY FORMAT: ",dec);
while(dec>0)
{
a[c]=dec%2;
3&
dec=dec/2;
c++;
}
for(i=c-1;i>=0;i--)
printf("%d",a[i]);
getch();
}
2) Write a program to convert a decimal number to its equivalent octal number.
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long dec;
int a[25],c=0,i;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%lu",&dec);
printf("\n%lu N OCTAL FORMAT: ",dec);
while(dec>0)
{
a[c]=dec%8;
dec=dec/8;
c++;
}
for(i=c-1;i>=0;i--)
printf("%d",a[i]);
getch();
}
3) Write a program to convert a decimal number to its equivalent hexadecimal
number.
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long dec;
int a[25],c=0,i;
clrscr();
printf("\nENTER A DECMAL NUMBER: ");
scanf("%lu",&dec);
printf("\n%lu N HEXADECMAL FORMAT: ",dec);
while(dec>0)
{
3(
a[c]=dec%16;
dec=dec/16;
c++;
}
for(i=c-1;i>=0;i--)
{
if(a[i]>=10)
printf("%c",a[i]+55);
else
printf("%d",a[i]);
}
getch();
}
4) Write a program to convert a binary number to its equivalent decimal number.
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long num;
int digit,i,pos=0,pow=1,dec=0;
clrscr();
printf("\nENTER A BNARY NUMBER: ");
scanf("%lu",&num);
printf("\nDECMAL EQUVALANT OF %lu S ",num);
while(num!=0)
{
pow=1;
digit=num%10;
num=num/10;
for(i=1;i<=pos;i++)
pow=pow*2;
pos++;
dec=dec+(pow*digit);
}
printf("%d",dec);
getch();
}
Function
1) Write a program to print Hello World in the main function and Welcome To
C in another function.
#include <stdio.h>
4#
#include <conio.h>
void message(void);
main()
{
clrscr();
printf("\nHELLO WORLD");
message();
getch();
}
void message()
{
printf("\nWELCOME TO C");
}
2) Write a program to add three given numbers using function.
#include <stdio.h>
#include <conio.h>
sum(int,int,int);
void main()
{
int a,b,c,d;
clrscr();
printf("\nACCEPT VALUE FOR a,b,c:\n");
scanf("%d %d %d",&a,&b,&c);
d=sum(a,b,c);
printf("\nSUM OF %d,%d and %d S %d",a,b,c,d);
getch();
}
sum(int x,int y,int z)
{
int temp;
temp=x+y+z;
return(temp);
}
3) Write a program to calculate the tax of n given number of employees. Use a
separate function to calculate the tax. Tax is 20% of basic if basic is less than
9000 otherwise tax is 25% of basic.
#include <stdio.h>
#include <conio.h>
void cal(void);
void main()
{
int i,n;
41
clrscr();
printf("\nENTER THE NUMBER OF THE EMPLOYEES: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
cal();
getch();
}
void cal()
{
int basic;
float tax;
printf("\nENTER THE AMOUNT OF BASC: ");
scanf("%d",&basic);
if(basic<9000)
tax=basic*20/100;
else
tax=basic*25/100;
printf("\nTHE AMOUNT OF TAX S %0.2f\n",tax);
}
4) Write a program to compute the root of a number using function.
#include <stdio.h>
#include <conio.h>
unsigned long root(int);
void main()
{
int x;
unsigned long res;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&x);
res=root(x);
printf("\nROOT OF %d S %lu",x,res);
getch();
}
unsigned long root(int a)
{
unsigned long b;
b=a*a;
return(b);
}
5) Write a program to evaluate a
b
using function.
#include <stdio.h>
42
#include <conio.h>
unsigned long power(int,int);
void main()
{
int num,pow;
unsigned long res;
clrscr();
printf("\nENTER THE NUMBER: ");
scanf("%d",&num);
printf("\nENTER THE POWER: ");
scanf("%d",&pow);
res=power(num,pow);
printf("\n%d TO THE POWER %d S %lu",num,pow,res);
getch();
}
unsigned long power(int a,int b)
{
int i;
unsigned long prod=1;
for(i=1;i<=b;i++)
prod=prod*a;
return(prod);
}
6) Write a program to print the following series using function.
9 25 57 121 249 505 1017 2041.
#include <stdio.h>
#include <conio.h>
void main()
{
int num=9,i;
clrscr();
printf("%d ",num);
for(i=4;i<=10;i++)
{
num=num+pow(2,i);
printf("%d ",num);
}
getch();
}
pow(int a,int b)
{
int prod=1,j;
for(j=1;j<=b;j++)
43
prod=prod*a;
return(prod);
}
7) Write a program to check a number is prime or not using function.
#include <stdio.h>
#include <conio.h>
void main()
{
int num,res=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
res=prime(num);
if(res==0)
printf("\n%d S A PRME NUMBER",num);
else
printf("\n%d S NOT A PRME NUMBER",num);
getch();
}
int prime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i!=0)
continue;
else
return 1;
}
return 0;
}
8) Write a program to find out the maximum number in an array using function.
#include <stdio.h>
#include <conio.h>
max(int [],int);
void main()
{
int a[]={10,5,45,12,19};
int n=5,m;
clrscr();
m=max(a,n);
printf("\nMAXMUM NUMBER S %d",m);
44
getch();
}
max(int x[],int k)
{
int t,i;
t=x[0];
for(i=1;i<k;i++)
{
if(x[i]>t)
t=x[i];
}
return(t);
}
9) Write a program to evaluate 1/Factorial of 1 + 2/Factorial of 2 . + n/Factorial
of n using function.
#include <stdio.h>
#include <conio.h>
fact(float);
void main()
{
float i,sum=0.0,n;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%f",&n);
for(i=1;i<=n;i++)
sum=sum+(i/fact(i));
printf("\nTHE SUMMATON S: %0.2f",sum);
getch();
}
fact(float x)
{
if(x==1)
return(1);
else
return(x*fact(x-1));
}
Recursion
1) Write a recursive function to print the factorial of a number.
#include <stdio.h>
#include <conio.h>
unsigned long fact(int);
4
void main()
{
unsigned long f;
int n;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
f=fact(n);
printf("\nFACTORAL OF %d S %ld",n,f);
getch();
}
unsigned long fact(int a)
{
unsigned long fac;
if(a==1)
return(1);
else
fac=a*fact(a-1);
return(fac);
}
2) Write a recursive function to print the fibonacci series up to a given number.
#include <stdio.h>
#include <conio.h>
unsigned long fib(int);
void main()
{
int n,i;
unsigned long f;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
printf("\nTHE FBONNAC SERES UPTO %d NUMBERS S:\n",n);
for(i=0;i<n;i++)
{
f=fib(i);
printf("%lu ",f);
}
getch();
}
unsigned long fib(int x)
{
unsigned long res;
if(x==0)
return(0);
46
else
if(x==1)
return(1);
else
{
res=fib(x-1)+fib(x-2);
return(res);
}
}
3) Write a recursive function to print a given number in reverse order.
#include <stdio.h>
#include <conio.h>
int reverse(unsigned long);
void main()
{
unsigned long num;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%lu",&num);
printf("\nREVERSE OF %lu S ",num);
reverse(num);
getch();
}
int reverse(unsigned long n)
{
int dig;
if(n==0)
return 1;
else
{
dig=n%10;
n=n/10;
printf("%d",dig);
reverse(n);
}
}
4) Write a recursive function to evaluate x
y
.
#include <stdio.h>
#include <conio.h>
pow(int,int);
void main()
{
47
int x,y,pow;
clrscr();
printf("\nENTER A NUMBER FOR x: ");
scanf("%d",&x);
printf("\nENTER A NUMBER FOR y: ");
scanf("%d",&y);
pow=power(x,y);
printf("\nx TO THE POWER y S: %d",pow);
getch();
}
power(int a,int b)
{
int prod;
if(b==0)
return(1);
else
{
prod=a*power(a,b-1);
return(prod);
}
}
5) Write a program to implement Tower Of Hanoi.
#include <stdio.h>
#include <conio.h>
void hanoi(char,char,char,int);
void main()
{
int num;
clrscr();
printf("\nENTER NUMBER OF DSKS: ");
scanf("%d",&num);
printf("\nTOWER OF HANO FOR %d NUMBER OF DSKS:\n", num);
hanoi('A','B','C',num);
getch();
}
void hanoi(char from,char to,char other,int n)
{
if(n<=0)
printf("\nLLEGAL NUMBER OF DSKS");
if(n==1)
printf("\nMOVE DSK FROM %c TO %c",from,other);
if(n>1)
{
hanoi(from,other,to,n-1);
4&
hanoi(from,to,other,1);
hanoi(to,from,other,n-1);
}
}
Sorting
1) Write a program to sort an array using Bubble Sort Algorithm.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[15],i,j,n=10,temp;
clrscr();
printf("\nENTER VALUES FOR THE ARRAY:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nTHE SORTED ARRAY S:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}
2) Write a program to sort an array using Quick Sort Algorithm.
#include <stdio.h>
#include <conio.h>
void qsort();
int n;
void main()
{
int a[100],i,l,r;
clrscr();
4(
printf("\nENTER THE SZE OF THE ARRAY: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nENTER NUMBER-%d: ",i+1);
scanf("%d",&a[i]);
}
printf("\nTHE ARRAY ELEMENTS BEFORE SORTNG: \n");
for(i=0;i<n;i++)
{
printf("%5d",a[i]);
}
l=0;
r=n-1;
qsort(a,l,r);
printf("\nTHE ARRAY ELEMENTS AFTER SORTNG: \n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}
void qsort(int b[],int left,int right)
{
int i,j,p,tmp,finished,k;
if(right>left)
{
i=left;
j=right;
p=b[left];
finished=0;
while (!finished)
{
do
{
++i;
}
while ((b[i]<=p) && (i<=right));
while ((b[j]>=p) && (j>left))
{
--j;
}
if(j<i)
finished=1;
else
{
tmp=b[i];
b[i]=b[j];
#
b[j]=tmp;
}
}
tmp=b[left];
b[left]=b[j];
b[j]=tmp;
qsort(b,left,j-1);
qsort(b,i,right);
}
return;
}
Searching
1) Write a program to search a key number in an array using Sequential Search
Method.
#include <stdio.h>
#include <conio.h>
main()
{
int arr[]={12,23,78,98,67,56,45,19,65,9},key,i,flag=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&key);
for(i=0;i<10;i++)
{
if(key==arr[i])
flag=1;
}
if(flag==1)
printf("\nTHE NUMBER %d EXSTS N THE ARRAY",key);
else
printf("\nTHE NUMBER %d DOES NOT EXST N THE
ARRAY",key);
getch();
}
2) Write a program to search a key number in an array using Binary Search
Method or Dictionary Search Method.
#include <stdio.h>
#include <conio.h>
void sort(int *);
int search(int *,int);
void main()
1
{
int a[10],i,j,temp,key,flag;
clrscr();
for(i=0;i<10;i++)
{
printf("\nENTER NUMBER-%d: ",i+1);
scanf("%d",&a[i]);
}
sort(a);
printf("\nTHE SORTED ARRAY S: ");
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\nENTER A NUMBER TO SEARCH: ");
scanf("%d",&key);
flag=search(a,key);
if(flag==1)
printf("\nTHE NUMBER %d EXSTS",key);
else
printf("\nTHE NUMBER %d DOES NOT EXST ARRAY",key);
getch();
}
void sort(int *x)
{
int i,j,temp;
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(x[i]>x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
}
int search(int *x,int k)
{
int low=0,high=10,mid,res=0;
while(high>=low)
{
mid=(high+low)/2;
if(k==x[mid])
{
res=1;
2
break;
}
else
{
if(k>x[mid])
low=mid+1;
else
high=mid-1;
}
}
return res;
}
Pointers
1) Write a program to give examples & (Address Of) and * (Value At Address)
operator.
#include <stdio.h>
#include <conio.h>
void main()
{
int i=3,*j;
clrscr();
j=&i;
printf("\nVALUE OF i S [i]: %d",i);
printf("\nVALUE OF i S [*(&i)]: %d",*(&i));
printf("\nADDRESS OF i S [&i]: %u",&i);
printf("\nVALUE OF j S [*(&j)]: %u",*(&j));
printf("\nADDRESS OF j S [&j]: %u",&j);
printf("\nADDRESS OF i S [j]: %u",j);
getch();
}
2) Write a program to swap the address of two variables.
#include <stdio.h>
#include <conio.h>
void swap(int *,int *);
void main()
{
int a=10,b=20;
clrscr();
printf("\nVALUES OF a AND b BEFORE SWAPNG ARE %d %d",a,b);
swap(&a,&b);
printf("\nVALUES OF a AND b AFTER SWAPNG ARE %d %d",a,b);
3
getch();
}
void swap(x,y)
int *x,*y;
{
int t;
t=*x;
*x=*y;
*y=t;
}
3) Write a program to compute the average of n given numbers using pointers.
#include <stdio.h>
#include <conio.h>
void main()
{
int n,*p,sum=0,i;
float avg;
clrscr();
printf("\nHOW MANY NUMBERS: ");
scanf("%d",&n);
p=(int *) malloc(n*2);
if(p==NULL)
{
printf("\nMEMORY ALLOCATON UNSUCCCESSFUL");
exit();
}
for(i=0;i<n;i++)
{
printf("\nENTER NUMBER %d: ",i+1);
scanf("%d",(p+i));
}
for(i=0;i<n;i++)
sum=sum+*(p+i);
avg=(float)sum/n;
printf("\nTHE AVERAGE OF THE NUMBERS S %0.2f",avg);
getch();
}
4) Write a program to sort n given numbers using pointers.
Hint: - Use Bubble Sort Algorithm.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
4
void main()
{
int n,*p,i,j,temp;
clrscr();
printf("\nHOW MANY NUMBER: ");
scanf("%d",&n);
p=(int *) malloc(n*2);
if(p==NULL)
{
printf("\nMEMORY ALLOCATON UNSUCCESSFUL");
exit();
}
for(i=0;i<n;i++)
{
printf("\nENTER NUMBER %d: ",i+1);
scanf("%d",p+i);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(*(p+i)<*(p+j))
{
temp=*(p+i);
*(p+i)=*(p+j);
*(p+j)=temp;
}
}
}
printf("\nTHE SORTED NUMBERS ARE:\n");
for(i=0;i<n;i++)
printf("%d ",*(p+i));
getch();
}
5) Write a program to concatenate two given string without using the library
function strcat().
#include <stdio.h>
#include <conio.h>
void strconc(char *,char *);
char *s3;
void main()
{
char *str1,*str2;
clrscr();

printf("\nENTER THE FRST STRNG: ");


gets(str1);
printf("\nENTER THE SECOND STRNG: ");
gets(str2);
strconc(str1,str2);
printf("\nTHE NEW STRNG S: %s",s3);
getch();
}
void strconc(char *s1,char *s2)
{
int ls1,ls2,i;
ls1=strlen(s1);
ls2=strlen(s2);
s3=s1;
for(i=0;i<ls2;i++)
{
s3[ls1++]=s2[i];
}
s3[ls1++]=NULL;
}
6) Write a program to find the maximum number within n given numbers using
pointers.
#include <stdio.h>
#include <conio.h>
void main()
{
int n,*p,i,h=0;
clrscr();
printf("\nHOW MANY NUMBERS: ");
scanf("%d",&n);
p=(int *) malloc(n*2);
if(p==NULL)
{
printf("\nMEMORY ALLOCATON UNSUCCCESSFUL");
exit();
}
for(i=0;i<n;i++)
{
printf("\nENTER NUMBER %d: ",i+1);
scanf("%d",(p+i));
}
h=*p;
for(i=1;i<n;i++)
{
6
if(*(p+i)>h)
h=*(p+i);
}
printf("\nTHE HGHEST NUMBER S %d",h);
getch();
}
7) Write a program to search a given key number within n given numbers using
pointers. f the key number exists print found otherwise print not found.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
void main()
{
int n,*p,i,num,flag=0;
clrscr();
printf("\nHOW MANY NUMBER: ");
scanf("%d",&n);
p=(int *) malloc(n*2);
if(p==NULL)
{
printf("\nMEMORY ALLOCATON UNSUCCESSFUL");
exit();
}
for(i=0;i<n;i++)
{
printf("\nENTER NUMBER %d: ",i+1);
scanf("%d",p+i);
}
printf("\nENTER A NUMBER TO SEARCH: ");
scanf("%d",&num);
for(i=0;i<n;i++)
{
if(num==*(p+i))
flag=1;
}
if(flag==1)
printf("\nTHE NUMBER %d S FOUND",num);
else
printf("\nTHE NUMBER %d DOES NOT EXST",num);
getch();
}
8) Write a program to reverse a string using pointers.
7
#include <stdio.h>
#include <conio.h>
void main()
{
char *s;
int len,i;
clrscr();
printf("\nENTER A STRNG: ");
gets(s);
len=strlen(s);
printf("\nTHE REVERSE OF THE STRNG S:");
for(i=len;i>=0;i--)
printf("%c",*(s+i));
getch();
}
9) Write a program to find out whether a given string is palindrome or not using
pointers.
#include <stdio.h>
#include <conio.h>
void main()
{
char *a;
int i,len,flag=0;
clrscr();
printf("\nENTER A STRNG: ");
gets(a);
len=strlen(a);
for(i=0;i<len;i++)
{
if(a[i]==a[len-i-1])
flag=flag+1;
}
if(flag==len)
printf("\nTHE STRNG S PALNDROM");
else
printf("\nTHE STRNG S NOT PALNDROM");
getch();
}
10) Write a program to sort n given numbers of cities using pointers.
#include <stdio.h>
#include <conio.h>
#define items 5
&
void main()
{
char *str[items],*temp;
int i,j;
clrscr();
for(i=0;i<items;i++)
{
printf("\nENTER CTY-%d: ",i+1);
gets(str[i]);
}
for(i=0;i<items;i++)
{
for(j=i+1;j<items;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
}
printf("\nCTES N SORTED ORDER:\n");
for(i=0;i<items;i++)
printf("\n%s",str[i]);
getch();
}
11) Write a program to reverse the order of each word of the string using
pointers.
Example: - NPUT: Orissa Computer Application Centre
OUTPUT: Centre Application Computer Orissa
#include <stdio.h>
#include <conio.h>
void main()
{
char *s;
int len,i,j,sp=0,start,end;
clrscr();
printf("\nENTER A STRNG: ");
gets(s);
printf("\nTHE STRNG AFTER CHANGNG THE ORDER OF EACH
WORD:\n");
(
len=strlen(s);
end=len-1;
for(i=len-1;i>=0;i--)
{
if(s[i]==32 || i==0)
{
if(i==0)
start=0;
else
start=i+1;
for(j=start;j<=end;j++)
printf("%c",s[j]);
end=i-1;
printf(" ");
}
}
getch();
}
Structure
1) Write a program to create a book structure having name, author, page and
price.
#include <stdio.h>
#include <conio.h>
void main()
{
struct book
{
char name[20];
char auth[20];
int page;
float price;
};
struct book b;
clrscr();
printf("\nENTER THE NAME OF THE BOOK: ");
gets(b.name);
printf("\nENTER THE NAME OF THE AUTHOR: ");
gets(b.auth);
printf("\nENTER THE NUMBER OF PAGES: ");
scanf("%d",&b.page);
printf("\nENTER THE PRCE OF THE BOOK: ");
scanf("%f",&b.price);
printf("\nNAME OF THE BOOK: %s",b.name);
6#
printf("\nNAME OF THE AUTHOR: %s",b.auth);
printf("\nNUMBER OF PAGES: %d",b.page);
printf("\nPRCE OF THE BOOK: %0.2f",b.price);
getch();
}
Link List
1) Write a program to create a singly link list.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct linklist
{
int number;
struct linklist *next;
};
typedef struct linklist node;
void create(node *);
void display(node *);
node *head;
void main()
{
clrscr();
head=(node *) malloc(sizeof(node));
create(head);
display(head);
getch();
}
void create(node *list)
{
char conf='y';
printf("\nENTER A NUMBER: ");
scanf("%d",&list->number);
printf("\nWANT TO CONTNUE[Y/N]: ");
conf=getche();
printf("\n");
if(conf=='n' || conf=='N')
list->next=NULL;
else
{
list->next=(node *) malloc(sizeof(node));
create(list->next);
}
}
61
void display(node *disp)
{
printf("\nTHE VALUES OF THE LNK LST ARE:\n");
while(disp!=NULL)
{
printf("%d ",disp->number);
disp=disp->next;
}
}
2) Write a program to count the number of nodes in a link list.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct linklist
{
int number;
struct linklist *next;
};
typedef struct linklist node;
void create(node *);
void count(node *);
node *head;
void main()
{
clrscr();
head=(node *) malloc(sizeof(node));
create(head);
printf("\n");
count(head);
getch();
}
void create(node *list)
{
char conf='y';
int i;
printf("\nENTER A NUMBER: ");
scanf("%d",&list->number);
printf("\nWANT TO CONTNUE[Y/N]: ");
conf=getche();
printf("\n");
if(conf=='n' || conf=='N')
list->next=NULL;
else
{
62
list->next=(node *) malloc(sizeof(node));
create(list->next);
}
}
void count(node *first)
{
int i=0;
while(first!=NULL)
{
first=first->next;
i++;
}
printf("THERE ARE %d NODES N THE LNK LST",i);
}
3) Write a program to insert a node before the first node of a link list.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct link
{
int num;
struct link *next;
};
typedef struct link node;
void create(node *);
void insert(node *);
void display(node *);
node *head;
void main()
{
clrscr();
head=(node *) malloc(sizeof(node));
create(head);
display(head);
insert(head);
display(head);
getch();
}
void create(node *list)
{
char conf='y';
int i;
printf("\nENTER A NUMBER: ");
scanf("%d",&list->num);
63
printf("\nWANT TO CONTNUE[Y/N]: ");
conf=getche();
printf("\n");
if(conf=='n' || conf=='N')
list->next=NULL;
else
{
list->next=(node *) malloc(sizeof(node));
create(list->next);
}
}
void display(node *disp)
{
printf("\nTHE VALUES OF THE LNK LST ARE:\n");
while(disp!=NULL)
{
printf("%d ",disp->num);
disp=disp->next;
}
}
void insert(node *ins)
{
node *newnode;
int newnum;
printf("\n\nENTER A NUMBER YOU WANT TO NSERT: ");
scanf("%d",&newnum);
newnode=(node *) malloc(sizeof(node));
newnode->num=newnum;
newnode->next=ins;
head=newnode;
}
4) Write a program to insert a node after the last node of a link list.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct link
{
int num;
struct link *next;
};
typedef struct link node;
void create(node *);
void insert(node *);
void display(node *);
64
node *head;
void main()
{
clrscr();
head=(node *) malloc(sizeof(node));
create(head);
display(head);
insert(head);
display(head);
getch();
}
void create(node *list)
{
char conf='y';
int i;
printf("\nENTER A NUMBER: ");
scanf("%d",&list->num);
printf("\nWANT TO CONTNUE[Y/N]: ");
conf=getche();
printf("\n");
if(conf=='n' || conf=='N')
list->next=NULL;
else
{
list->next=(node *) malloc(sizeof(node));
create(list->next);
}
}
void display(node *disp)
{
printf("\nTHE VALUES OF THE LNK LST ARE:\n");
while(disp!=NULL)
{
printf("%d ",disp->num);
disp=disp->next;
}
}
void insert(node *ins)
{
node *newnode;
int newnum;
while(ins->next!=NULL)
{
ins=ins->next;
}
printf("\n\nENTER A NUMBER YOU WANT TO NSERT: ");
6
scanf("%d",&newnum);
newnode=(node *) malloc(sizeof(node));
newnode->num=newnum;
newnode->next=NULL;
ins->next=newnode;
}
5) Write a program to insert a node before a given key value of a link list.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct link
{
int num;
struct link *next;
};
typedef struct link node;
void create(node *);
void insert(node *);
void display(node *);
node *find(node *,int);
node *head;
void main()
{
clrscr();
head=(node *) malloc(sizeof(node));
create(head);
display(head);
insert(head);
display(head);
getch();
}
void create(node *list)
{
char conf='y';
int i;
printf("\nENTER A NUMBER: ");
scanf("%d",&list->num);
printf("\nWANT TO CONTNUE[Y/N]: ");
conf=getche();
printf("\n");
if(conf=='n' || conf=='N')
list->next=NULL;
else
{
66
list->next=(node *) malloc(sizeof(node));
create(list->next);
}
}
void display(node *disp)
{
printf("\nTHE VALUES OF THE LNK LST ARE:\n");
while(disp!=NULL)
{
printf("%d ",disp->num);
disp=disp->next;
}
}
void insert(node *ins)
{
node *newnode,*n1;
int newnum,key;
printf("\n\nENTER A NUMBER YOU WANT TO NSERT: ");
scanf("%d",&newnum);
printf("\nENTER THE VALUE OF KEY NUMBER: ");
scanf("%d",&key);
if(ins->num==key)
{
newnode=(node *) malloc(sizeof(node));
newnode->num=newnum;
newnode->next=ins;
head=newnode;
}
else
{
n1=find(ins,key);
if(n1==NULL)
printf("\nKEY VALUE NOT FOUND N THE LST\n");
else
{
newnode=(node *) malloc(sizeof(node));
newnode->num=newnum;
newnode->next=n1->next;
n1->next=newnode;
}
}
}
node *find(node *list,int key)
{
if(list->next->num==key)
return(list);
67
else
if(list->next->next==NULL)
return(NULL);
else
find(list->next,key);
}
6) Write a program to delete the first node of a link list.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct link
{
int num;
struct link *next;
};
typedef struct link node;
void create(node *);
void delet(node *);
void display(node *);
node *head;
void main()
{
clrscr();
head=(node *) malloc(sizeof(node));
create(head);
display(head);
delet(head);
printf("\n");
display(head);
getch();
}
void create(node *list)
{
char conf='y';
int i;
printf("\nENTER A NUMBER: ");
scanf("%d",&list->num);
printf("\nWANT TO CONTNUE[Y/N]: ");
conf=getche();
printf("\n");
if(conf=='n' || conf=='N')
list->next=NULL;
else
{
6&
list->next=(node *) malloc(sizeof(node));
create(list->next);
}
}
void display(node *disp)
{
printf("\nTHE VALUES OF THE LNK LST ARE:\n");
while(disp!=NULL)
{
printf("%d ",disp->num);
disp=disp->next;
}
}
void delet(node *del)
{
head=del->next;
free(del);
}
7) Write a program to delete the last node of a link list.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct link
{
int num;
struct link *next;
};
typedef struct link node;
void create(node *);
void delet(node *);
void display(node *);
node *head;
void main()
{
clrscr();
head=(node *) malloc(sizeof(node));
create(head);
display(head);
delet(head);
printf("\n");
display(head);
getch();
}
void create(node *list)
6(
{
char conf='y';
int i;
printf("\nENTER A NUMBER: ");
scanf("%d",&list->num);
printf("\nWANT TO CONTNUE[Y/N]: ");
conf=getche();
printf("\n");
if(conf=='n' || conf=='N')
list->next=NULL;
else
{
list->next=(node *) malloc(sizeof(node));
create(list->next);
}
}
void display(node *disp)
{
printf("\nTHE VALUES OF THE LNK LST ARE:\n");
while(disp!=NULL)
{
printf("%d ",disp->num);
disp=disp->next;
}
}
void delet(node *del)
{
while(del!=NULL)
{
if(del->next->next==NULL)
{
del->next=NULL;
free(del->next->next);
}
del=del->next;
}
}
8) Write a program to delete a node having the given key value of a link list.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct link
{
int num;
7#
struct link *next;
};
typedef struct link node;
void create(node *);
void delet(node *);
void display(node *);
node *find(node *,int);
node *head;
void main()
{
clrscr();
head=(node *) malloc(sizeof(node));
create(head);
display(head);
delet(head);
display(head);
getch();
}
void create(node *list)
{
char conf='y';
int i;
printf("\nENTER A NUMBER: ");
scanf("%d",&list->num);
printf("\nWANT TO CONTNUE[Y/N]: ");
conf=getche();
printf("\n");
if(conf=='n' || conf=='N')
list->next=NULL;
else
{
list->next=(node *) malloc(sizeof(node));
create(list->next);
}
}
void display(node *disp)
{
printf("\nTHE VALUES OF THE LNK LST ARE:\n");
while(disp!=NULL)
{
printf("%d ",disp->num);
disp=disp->next;
}
}
void delet(node *del)
{
71
node *newnode,*n1;
int key;
printf("\n\nENTER THE VALUE OF KEY NUMBER: ");
scanf("%d",&key);
if(del->num==key)
{
head=del->next;
free(del);
}
else
{
n1=find(del,key);
if(n1==NULL)
printf("\nKEY VALUE NOT FOUND N THE LST\n");
else
{
newnode=n1->next->next;
free(n1->next);
n1->next=newnode;
}
}
}
node *find(node *list,int key)
{
if(list->next->num==key)
return(list);
else
if(list->next->next==NULL)
return(NULL);
else
find(list->next,key);
}
9) Write a program to create a doubly link list.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct linklist
{
struct linklist *prev;
int num;
struct linklist *next;
};
typedef struct linklist node;
void create(node *);
72
void display(node *);
void main()
{
node *head;
clrscr();
head=(node *) malloc(sizeof(node));
head->prev=NULL;
create(head);
printf("\n");
display(head);
getch();
}
void create(node *list)
{
char conf='y';
int i;
printf("\nENTER A NUMBER: ");
scanf("%d",&list->num);
list->next->prev=list;
printf("\nWANT TO CONTNUE[Y/N]: ");
conf=getche();
printf("\n");
if(conf=='n' || conf=='N')
list->next=NULL;
else
{
list->next=(node *) malloc(sizeof(node));
create(list->next);
}
}
void display(node *disp)
{
printf("THE ADDRESS-PREVOUS ADDRESS-VALUE-NEXT ADDRESS
OF THE LNK LST ARE:\n");
while(disp!=NULL)
{
printf("%u-%u-%u-%u\n",disp,disp->prev,disp->num,disp->next);
disp=disp->next;
}
}
10) Write a program to create a circular link list.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
73
struct linklist
{
int number;
struct linklist *next;
};
typedef struct linklist node;
void create(node *);
void display(node *);
node *head;
void main()
{
clrscr();
head=(node *) malloc(sizeof(node));
create(head);
display(head);
getch();
}
void create(node *list)
{
char conf='y';
int i;
printf("\nENTER A NUMBER: ");
scanf("%d",&list->number);
printf("\nWANT TO CONTNUE[Y/N]: ");
conf=getche();
printf("\n");
if(conf=='n' || conf=='N')
list->next=head;
else
{
list->next=(node *) malloc(sizeof(node));
create(list->next);
}
}
void display(node *disp)
{
printf("\nTHE ADDRESS-VALUE-NEXT ADDRESS OF THE LNK LST
ARE:\n");
while(disp->next!=head)
{
printf("%u-%u-%u\n",disp,disp->number,disp->next);
disp=disp->next;
}
printf("%u-%u-%u",disp,disp->number,disp->next);
}
74
File
1) Write a program to read a text file print it on the screen.
#include <stdio.h>
#include <conio.h>
void main()
{
FLE *fp;
char s;
clrscr();
fp=fopen("text.txt","r");
if(fp==NULL)
{
printf("\nCAN NOT OPEN FLE");
getch();
exit();
}
do
{
s=getc(fp);
printf("%c",s);
}
while(s!=EOF);
fclose(fp);
getch();
}
2) Write a program to enter a sentence from the keyboard and store it in a text
file.
#include <stdio.h>
#include <conio.h>
void main()
{
FLE *fp;
char ch[50];
clrscr();
fp=fopen("output.txt","w");
printf("\nENTER YOUR LNES: (PRESS ^Z TO EXT)\n");
while(strlen(gets(ch))!=0)
{
fputs(ch,fp);
fputs("\n",fp);
}
fclose(fp);
7
getch();
}
3) Write a program to copy a file. Use command line arguments to specify the
source file and target file.
#include <stdio.h>
#include <conio.h>
void main(int argc,char *args[])
{
FLE *fpr,*fpw;
char ch;
fpr=fopen(args[1],"r");
fpw=fopen(args[2],"w");
do
{
ch=getc(fpr);
putc(ch,fpw);
}
while(ch!=EOF);
fclose(fpr);
fclose(fpw);
printf("\nFLE COPED\n");
}
4) Write a program to read a file and print the number of vowels and number of
words in the file. Assume that a word is a sequence of letters ending with a
blank, or a tab, or an end of line marker or end of file or punctuation symbols
such as ,, ., ! and ?.
#include <stdio.h>
#include <conio.h>
void count(char[]);
void main()
{
FLE *fp;
char str[100],s;
int word=0,vow=0;
clrscr();
fp=fopen("text.txt","r");
if(fp==NULL)
{
printf("\nCAN NOT OPEN FLE");
getch();
exit();
}
76
do
{
s=getc(fp);
if(s==32 || s=='\t' || s=='\n' || s==EOF || s==',' || s=='.' || s=='!' ||
s=='?')
word++;
if(s=='a' || s=='e' || s=='i' || s=='o' || s=='u' || s=='A' || s=='E' || s=='' ||
s=='O'|| s=='U')
vow++;
}
while(s!=EOF);
printf("\nNUMBER OF WORDS N THE STRNG ARE %d",word);
printf("\nNUMBER OS VOWELS N THE STRNG ARE %d",vow);
fclose(fp);
getch();
}
DOEACC Examination Questions & Answers
!an"ar# 2$$3
5) The first few numbers of Lucas sequences which is a variation of Fibonacci
sequence are sequence are
1 3 4 7 11 18 29
a) Design a program in C to generate Lucas sequence.
b) Develop a function to calculate sum of first n odd integers.
a)
#include <stdio.h>
#include <conio.h>
void main()
{
int num,i;
unsigned long n1=1,n2=3,s;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
printf("\nLUCAS SEQUENCE UPTO %d NUMBERS S: \n",num);
printf("%lu %lu",n1,n2);
for(i=1;i<=num-2;i++)
{
s=n1+n2;
printf(" %lu",s);
n1=n2;
77
n2=s;
}
getch();
}
b)
#include <stdio.h>
#include <conio.h>
unsigned long sumodd(int);
void main()
{
int i,n;
unsigned long sum;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
sum=sumodd(n);
printf("\nSUM OF THE ODD NUMBERS BETWEEN 1 TO %d S
%lu",n,sum);
getch();
}
unsigned long sumodd(int x)
{
int i;
unsigned long res=0;
for(i=1;i<=x;i+=2)
{
res=res+i;
}
return res;
}
6) Write recursive and iterative functions for computation of factorial of a number.
a) /* Recursive function for computation of factorial */
#include <stdio.h>
#include <conio.h>
unsigned long fact(int);
void main()
{
unsigned long f;
int n;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
7&
f=fact(n);
printf("\nFACTORAL OF %d S %ld",n,f);
getch();
}
unsigned long fact(int a)
{
unsigned long fac;
if(a==1)
return(1);
else
fac=a*fact(a-1);
return(fac);
}
b) /* terative function for computation of factorial */
#include <stdio.h>
#include <conio.h>
unsigned long factor(int);
void main()
{
int n;
unsigned long result;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
result=factor(n);
printf("\nFACTORAL OF %d S %lu",n,result);
getch();
}
unsigned long factor(int x)
{
int i;
unsigned long fact=1;
for(i=1;i<=x;i++)
{
fact=fact*i;
}
return(fact);
}
7) Each node of a singly linked list stores the salary of employees in one
company Write a C program for the following operations.
a) Create a list in order of employee's salary.
b) Print the contents of created list.
7(
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct linklist
{
int salary;
struct linklist *next;
};
typedef struct linklist node;
void create(node *);
void display(node *);
void sort(node *);
node *head;
void main()
{
clrscr();
head=(node *) malloc(sizeof(node));
create(head);
sort(head);
display(head);
getch();
}
void create(node *list)
{
char conf='y';
printf("\nENTER A NUMBER: ");
scanf("%d",&list->salary);
printf("\nWANT TO CONTNUE[Y/N]: ");
conf=getche();
printf("\n");
if(conf=='n' || conf=='N')
list->next=NULL;
else
{
list->next=(node *) malloc(sizeof(node));
create(list->next);
}
}
void sort(node *list)
{
node *i,*j;
int temp;
for(i=list;i!=NULL;i=i->next)
{
for(j=i->next;j!=NULL;j=j->next)
&#
{
if(i->salary>j->salary)
{
temp=i->salary;
i->salary=j->salary;
j->salary=temp;
}
}
}
}
void display(node *disp)
{
printf("\nTHE VALUES OF THE LNK LST N SORTED ORDER ARE:\n");
while(disp!=NULL)
{
printf("%d ",disp->salary);
disp=disp->next;
}
}
8) Write a function that accepts an array and constant parameters and count how
many elements of array are less than the constant, equal to it and greater than it.
Return these three values either by way of pass by reference parameter or a
three-element array parameter.
#include <stdio.h>
#include <conio.h>
int *gle(int *,int);
int *res;
void main()
{
int a[10]={12,51,42,72,32,58,65,95,62,21},key;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&key);
res=gle(a,key);
printf("\n%d ELEMENTS ARE LESS THAN THE KEY VALUE",*res);
printf("\n%d ELEMENTS ARE EQUAL TO THE KEY VALUE",*(res+1));
printf("\n%d ELEMENTS ARE GREATER THAN THE KEY
VALUE",*(res+2));
getch();
}
int *gle(int *x,int k)
{
int i;
&1
*(res)=*(res+1)=*(res+2)=0;
for(i=0;i<10;i++)
{
if(k>x[i])
(*res)++;
if(k==x[i])
(*(res+1))++;
if(k<x[i])
(*(res+2))++;
}
return res;
}
9)
a) Write a program in 'C' to find the value of x
n
where 'x' and 'n' are positive
integers.
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
unsigned int x,n;
unsigned long res=1;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%u",&x);
printf("\nENTER THE POWER: ");
scanf("%u",&n);
for(i=1;i<=n;i++)
{
res=res*x;
}
printf("\n%u TO THE POWER %u S %lu",x,n,res);
getch();
}
b) Write 'C' function to find the largest difference between two numbers of an
integer array.
#include <stdio.h>
#include <conio.h>
int lardif(int *);
void main()
{
int a[10]={12,51,56,72,10,58,65,92,62,12},result;
&2
clrscr();
result=lardif(a);
printf("\nTHE LARGEST DFFERENCE S %d",result);
getch();
}
int lardif(int *x)
{
int res,i,j;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(x[i]-x[j]>res)
{
res=x[i]-x[j];
}
}
}
return res;
}
!"l# 2$$2
5) Write C function in each case that will work exactly as following without using
any built-in string manipulation function.
a) strcpy()
b) stccmp()
a)
#include <stdio.h>
#include <conio.h>
void strcopy(char[],char[]);
void main()
{
char a[10]="Paritosh",b[10];
clrscr();
strcopy(a,b);
getch();
}
void strcopy(char x[],char y[])
{
int i,len;
for(len=0;x[len]!=0;len++);
for(i=0;i<=len;i++)
y[i]=x[i];
&3
printf("\na=%s\nb=%s",x,y);
}
b)
#include <stdio.h>
#include <conio.h>
#include <string.h>
strcmpr(char[],char[]);
void main()
{
char a[10]="Computer",b[10]="Compute";
int result,temp;
clrscr();
result=strcmpr(a,b);
printf("%d",result);
getch();
}
strcmpr(char x[],char y[])
{
int len,i,res=0;
for(len=0;x[len]!=0;len++);
for(i=0;i<len+1;i++)
{
if(x[i]==y[i])
continue;
else
{
res=x[i]-y[i];
break;
}
}
return(res);
}
!an"ar# 2$$2
5) Write a 'C' function to sort an array using bubble sort algorithm.
#include <stdio.h>
#include <conio.h>
void sort(int *);
void main()
{
int a[10]={52,17,82,65,45,96,24,7,37,78},i;
clrscr();
printf("\nTHE NUMBERS N THE ARRAY BEFORE SORTNG:\n");
&4
for(i=0;i<10;i++)
printf("%d ",a[i]);
sort(a);
printf("\n\nTHE NUMBERS N THE ARRAY AFTER SORTNG:\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
getch();
}
void sort(int *x)
{
int i,j,temp;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(x[i]<x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
}
6) Suppose that two arrays, a and b, contain two sets of integers. Neither of
these sets contains any duplication. Write 'C' functions to find out the union of
these two sets. For example, if a is {2, 7, 1, 5} and b is {9, 2, 8, 5} then their
union is {2, 7, 1, 5, 9, 8}.
#include <stdio.h>
#include <conio.h>
void *uni(int *,int *,int);
int c[50],k;
void main()
{
int a[25],b[25],size,i;
clrscr();
printf("\nENTER THE SZE OF THE ARRAY: ");
scanf("%d",&size);
for(i=0;i<size;i++)
{
printf("ENTER VALUE FOR a[%d]: ",i);
scanf("%d",&a[i]);
printf("ENTER VALUE FOR b[%d]: ",i);
scanf("%d",&b[i]);
&
}
uni(a,b,size);
printf("\nTHE UNON OF THESE TWO ARRAYS S:\n");
for(i=0;i<k;i++)
printf("%d ",c[i]);
getch();
}
void *uni(int *x,int *y,int n)
{
int i,j,flag=0;
for(i=0;i<n;i++)
c[i]=x[i];
k=n;
for(i=0;i<n;i++)
{
flag=0;
for(j=0;j<n;j++)
{
if(y[i]==x[j])
flag=1;
}
if(flag==0)
{
c[k]=y[i];
k++;
}
}
}
7) Define a structure for a student having name, roll number and marks obtained
in six subjects. Assume that allStudents is an array of students. Write 'C'
functions to print the name and roll numbers of the students who have secured
highest marks in each subject.
#include <stdio.h>
#include <conio.h>
void hmark(struct student *);
struct student
{
char name[20];
int roll;
int s1;
int s2;
int s3;
int s4;
int s5;
&6
};
struct student std[5];
int n=5;
void main()
{
int i;
clrscr();
for(i=0;i<n;i++)
{
printf("\nENTER THE NAME OF THE STUDENT: ");
scanf("%s",std[i].name);
printf("\nENTER THE ROLL NO. OF THE STUDENT: ");
scanf("%d",&std[i].roll);
printf("\nENTER THE MARKS FOR 5 SUBJECTS:\n");
scanf("%d %d %d %d
%d",&std[i].s1,&std[i].s2,&std[i].s3,&std[i].s4,&std[i].s5);
}
hmark(std);
getch();
}
void hmark(struct student *x)
{
int i,hm1,hm2,hm3,hm4,hm5,h1,h2,h3,h4,h5;
h1=h2=h3=h4=h5=0;
hm1=x[0].s1;
hm2=x[0].s2;
hm3=x[0].s3;
hm4=x[0].s4;
hm5=x[0].s5;
for(i=0;i<n;i++)
{
if(x[i].s1>hm1)
{
hm1=x[i].s1;
h1=i;
}
if(x[i].s2>hm2)
{
hm2=x[i].s2;
h2=i;
}
if(x[i].s3>hm3)
{
hm3=x[i].s3;
h3=i;
}
&7
if(x[i].s4>hm4)
{
hm4=x[i].s4;
h4=i;
}
if(x[i].s5>hm5)
{
hm5=x[i].s5;
h5=i;
}
}
clrscr();
printf("\nHGHEST MARK OBTANED BY STUDENTS N EACH
SUBJECTS\n");
printf("\nSUBJECT-1\nNAME: %s\nROLL NO.:
%d\n",x[h1].name,x[h1].roll);
printf("\nSUBJECT-2\nNAME: %s\nROLL NO.:
%d\n",x[h2].name,x[h2].roll);
printf("\nSUBJECT-3\nNAME: %s\nROLL NO.:
%d\n",x[h3].name,x[h3].roll);
printf("\nSUBJECT-4\nNAME: %s\nROLL NO.:
%d\n",x[h4].name,x[h4].roll);
printf("\nSUBJECT-5\nNAME: %s\nROLL NO.:
%d\n",x[h5].name,x[h5].roll);
}
8) Write 'C' functions to count the number of words in a file; Assume that a word
is a sequence of letters ending with a blank, or a tab, or an end of line marker or
end of file or punctuation symbols such as ,, ., ! and ?.
#include <stdio.h>
#include <conio.h>
int count(FLE *);
void main()
{
int w;
FLE *fp;
clrscr();
fp=fopen("text.txt","r");
if(fp==NULL)
{
printf("\nTHE FLE DOES NOT EXST");
getch();
exit();
}
w=count(fp);
&&
printf("\nTHERE ARE %d WORDS N THE FLE",w);
getch();
}
int count(FLE *fo)
{
char ch;
int word=0;
do
{
ch=getc(fo);
if(ch==32 || ch=='\t' || ch=='\n' || ch==EOF || ch==',' || ch=='.' ||
ch=='!' || ch=='?')
{
word++;
}
}
while(ch!=EOF);
return word;
}
9) Define a self-referential structure for representing a simple linked list of
integers. Write a 'C' function to split the list into two lists so that the first list
contains all even numbered elements and the second list contains only odd
numbered elements. For example, if the original list is {2, 8, 1, 14, 6, 18, 0, 17}
then the resultant first list would be {8, 14, 18, 17} and the second list would be
{2, 1, 6, 0}.
!"l# 2$$1
6) Using command line argument concept write a 'C' program for the following
task:
Read the content of a given input file. Assume that the input file contains only
upper case letters and/or special characters. Transfer each character from input
file to the specified output file according to the following transfer rules:
Rule-1: Alphabet character has to be replaced by its next lower alphabet
character in cyclic order. For example, A will be replaced by b, B will be replaced
by c, .., Z will be replaced by a.
Rule-2: Replace Space/Blank Character with 'B'.
Rule-3: Other characters remain unchanged.
For example,
&(
nput: THE PRCE OF THS TEM S $100.
Output: uifBBqsjdfBBpgBBuijtBBjufnBBjtBB$100.
#include <stdio.h>
#include <conio.h>
void main(int argc,char *argv[])
{
int i;
char ch;
FLE *sfp,*tfp;
clrscr();
if(argc<3)
printf("\nPARAMETER MSSNG");
if(argc>3)
printf("\nTOO MANY PARAMETERS");
sfp=fopen(argv[1],"r");
tfp=fopen(argv[2],"w");
while(ch!=EOF)
{
ch=getc(sfp);
if(ch>=65 && ch<=90)
putc(ch+33,tfp);
else
{
if(ch==32)
putc(66,tfp);
else
putc(ch,tfp);
}
}
}
7) Write a C function, which takes arguments as a base and a number in
decimal, then converts the number to its equivalent number in a new number
system with a given base.
For example,
nput: Base = 2, Number = 10 (in decimal)
Output: Output = 1010
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long num;
(#
int base,n[25],c=0,i;
clrscr();
printf("\nENTER A DECMAL NUMBER: ");
scanf("%lu",&num);
printf("\nENTER THE BASE: ");
scanf("%d",&base);
printf("\n%lu TO THE BASE %d S ",num,base);
while(num!=0)
{
n[c]=num%base;
num=num/base;
c++;
}
for(i=c-1;i>=0;i--)
{
if(n[i]<10)
printf("%d",n[i]);
else
printf("%c",n[i]+55);
}
getch();
}
9) Write a C function to reverse a sub-string within the main string. Pointers to
the main string and the sub-string are passed as arguments to this function.
(1

You might also like