Sie sind auf Seite 1von 151

C++ Mind Game Source Code

#include<iostream>
#include<conio.h>
#include<iomanip.h>
using namespace std;
int tenb=0, oneb=0, ten, one;
int value=0, currvalue;
class mindgame
{
char ans;
public:
void table1()
{
cout<<\"Here is the listing for table 1:\\n\";
for (int i=8;i<=9;i++)
{
for (int k=0;k<=9;k++)
cout<<setw(2)<<int(i*10+k)<<\" \";
cout<<endl;
}
}
void table2()
{
cout<<\"Here is the listing for table 2:\\n\";
for (int i=4;i<=7;i++)
{
for (int k=0;k<=9;k++)
cout<<setw(2)<<int(i*10+k)<<\" \";
cout<<endl;
}
}
void table3()
{
cout<<\"Here is the listing for table 3:\\n\";

for (int i=2;i<=3;i++)


{
for (int k=0;k<=9;k++)
cout<<setw(2)<<int(i*10+k)<<\" \";
cout<<endl;
}
for (int i=6;i<=7;i++)
{
for (int k=0;k<=9;k++)
cout<<setw(2)<<int(i*10+k)<<\" \";
cout<<endl;
}
}
void table4()
{
cout<<\"Here is the listing for table 4:\\n\";
for (int i=1;i<=9;i+=2)
{
for (int k=0;k<=9;k++)
cout<<setw(2)<<int(i*10+k)<<\" \";
cout<<endl;
}
}
void table5()
{
cout<<\"Here is the listing for table 5:\\n\";
for (int i=0;i<=9;i++)
{
for (int k=8;k<=9;k++)
cout<<setw(2)<<int(i*10+k)<<\" \";
cout<<endl;
}
}
void table6()
{
cout<<\"Here is the listing for table 6:\\n\";
for (int i=0;i<=9;i++)

{
for (int k=4;k<=7;k++)
cout<<setw(2)<<int(i*10+k)<<\" \";
cout<<endl;
}
}
void table7()
{
cout<<\"Here is the listing for table 7:\\n\";
for (int i=0;i<=9;i++)
{
for (int k=2;k<=3;k++)
cout<<setw(2)<<int(i*10+k)<<\" \";
cout<<endl;
}
for (int i=0;i<=9;i++)
{
for (int k=6;k<=7;k++)
cout<<setw(2)<<int(i*10+k)<<\" \";
cout<<endl;
}
}
void table8()
{
cout<<\"Here is the listing for table 6:\\n\";
for (int i=0;i<=9;i++)
{
for (int k=1;k<=9;k+=2)
cout<<setw(2)<<int(i*10+k)<<\" \";
cout<<endl;
}
}
void brain()
{
cout<<\"\\nIs your number present in this table:\";
ans = getche();
cout<<endl;

switch (ans)
{
case \'y\': case \'Y\':
currvalue=1; break;
case \'n\': case \'N\':
currvalue=0; break;
default:
cout<<\"Please enter Y or N\";
brain();
}
value = value * 10 + currvalue;
}
void calc()
{
table1();
brain();
system(\"cls\");
table2();
brain();
system(\"cls\");
table3();
brain();
system(\"cls\");
table4();
brain();
system(\"cls\");
tenb=value;
value=0;
table5();
brain();
system(\"cls\");
table6();
brain();
system(\"cls\");
table7();
brain();
system(\"cls\");
table8();
brain();

system(\"cls\");
oneb=value;
}
}M;
int main()
{
cout<<\"Here is a small mind detecting game.\";
cout<<\"\\nJust think of a number between 0 and 99 (both inclusive)\\n\";
cout<<\"and enter whether the no. is present in the given 8 tables or not\\n\";
cout<<\"individually and your no. will be revealed at the end.\";
cout<<\"\\n\\nPress any key to begin...\";
getch();
error:;
system(\"cls\");
M.calc();
switch (tenb)
{
case 0: ten=0;break;
case 1: ten=1;break;
case 10: ten=2;break;
case 11: ten=3;break;
case 100: ten=4;break;
case 101: ten=5;break;
case 110: ten=6;break;
case 111: ten=7;break;
case 1000: ten=8;break;
case 1001: ten=9;break;
}
switch (oneb)
{
case 0: one=0;break;
case 1: one=1;break;
case 10: one=2;break;
case 11: one=3;break;
case 100: one=4;break;
case 101: one=5;break;
case 110: one=6;break;
case 111: one=7;break;
case 1000: one=8;break;

case 1001: one=9;break;


}
int no = ten*10 + one;
cout<<\"Your no. is:\"<<no;
cout<<\"\\n\\n\\n\\n\\n\\n\\nThis program is created by Raman.\";
getch();
}
/*this game actually makes use of binary coded decimals or BCD for short. it forcibly makes the
user enter the bcd equivalent to the digit the thought number. for more info on bcd just google it!*/

Write a C Program to Convert a Decimal number into Binary number


#include "stdio.h"
#include "conio.h"
#include "alloc.h"
struct node
{
int data;
struct node *link;
};
void atbeg(struct node**,int);
void display(struct node*);
int count(struct node*);
void main()
{
int n,i;
struct node *p;
p=NULL;
clrscr();
printf("enter the decimal number:");
scanf("%d",&n);
while(n!=0)
{
i=n%2;
atbeg(&p,i);
n=n/2;
}

display(p);
printf("\nnumber of elements=%d",count(p));
getch();
}
void atbeg(struct node **q,int i)
{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->data=i;
temp->link=*q;
*q=temp;
}
void display(struct node *q)
{
printf("\n");
while(q!=NULL)
{
printf("%d",q->data);
q=q->link;
}
}
int count(struct node *q)
{
int c=0;
while(q!=NULL)
{
q=q->link;
c++;
}
return c;
}

Write a C Program to Generates the Prime Factors of a given number


"/" is used instead of "<" and ">."
#include/iostream.h/
#include/iomanip.h/
#include/conio.h/
#include/math.h/
void main()

{ clrscr();
unsigned long int x;
char c='y';
do
{
cout<<"enter the no....."< cin>>x;
cout<<"Factors are...";
do
{
for(unsigned long i=2;i<=x;i++)
{
long f=x%i;
if(f==0)
{
cout<<<" ";
x=x/i;
break;
}}}
while(x!=1);
getch();
cout<<"continue...(y/n)....";
cin>>c;
cout<}
while(c=='y');
}

Write a C Program to Count CAPITAL Alphabets


This program will count the number of capital alphabets in the given string.
#include "stdio.h"
#include "conio.h"
#include "string.h"

void main()
{
static char *str[]={Enter any string};
printf("No. of capitals=%d",capcount(s,n)); /*n=no of string*/
}

capcount(s,x)
char **s;
int c;
{
int i,cap=0;
char *t;
for(i=0;i=65 && *t<=90) cap++; t++; } } return(cap);
}

C Program to Print ARMSTRONG NUMBER


* Generate all ARMSTRONG number between 1 t0 500 */
An Armstrong number is that whose sum of cube of each digit is equal to the number.
"/" is used instead of "<" and ">".

#include/stdio.h/
#include/conio.h/
main()
{
int i=1,a,b,c;
clrscr();
printf("Armstrong number are\n");
while(i<=500)
{
a=i%10; /*extract last digit*/ b=i%100; b=(b-a)/10; /*extract second digit*/ c=i/100; /*extract first
digit*/ if((a*a*a)+(b*b*b)+(c*c*c)==i) printf("%d",i); i++; } printf("\n\n\nPress any key to exit");
getch();
}

Calender in C++
Description : You can see the calender of any month of any year with this program.
#include<iostream.h>
#include<conio.h>

int step1 (int);


void month_name(int);
int no_days(int,int);
int leap(int);

void main()

{
restart:
clrscr();
cout<<"Enter year : ";
unsigned int y,m;
cin>>y;
int x;
x=step1(y);
int month[14][12]= {1,4,4,7,2,5,7,3,6,1,4,6,
2,5,5,1,3,6,1,4,7,2,5,7,
3,6,6,2,4,7,2,5,1,3,6,1,
4,7,7,3,5,1,3,6,2,4,7,2,
5,1,1,4,6,2,4,7,3,5,1,3,
6,2,2,5,7,3,5,1,4,6,2,4,
7,3,3,6,1,4,6,2,5,7,3,5,
1,4,5,1,3,6,1,4,7,2,5,7,
2,5,6,2,4,7,2,5,1,3,6,1,
3,6,7,3,5,1,3,6,2,4,7,2,
4,7,1,4,6,2,4,7,3,5,1,3,
5,1,2,5,7,3,5,1,4,6,2,4,
6,2,3,6,1,4,6,2,5,7,3,5,
7,3,4,7,2,5,7,3,6,1,4,6};
cout<<"Enter month (1 - 12) : ";
month_input:
cin>>m;
if(m<1 || m>12)
{
cout<<"Enter a valid month (1 - 12) : ";
goto month_input;
}
cout<<"
";
month_name(m);
cout<<' '<<y<<"
Mon Tue Wed Thu Fri Sat Sun<BR>;
int j=month[x-1][m-1], days=no_days(m,y);
for (int i=0; i<j-1; i++)
cout<<' ';
for(i=1; i<=days; i++)
{
cout<<i<<' ';
if(j==7)
{
cout<<'
';
j=1;
}

else
j++;
}
cout<<"
Try for another month? (y/n) : ";
char ch;
cin>>ch;
if(ch=='y' || ch=='Y')
goto restart;
}
int step1 (int y)

int x=y%7;
x+=(y-1)/4;
x-=(y-1)/100;
x+=(y-1)/400;
x=x%7;
if(x==0)
x=7;
if(leap(y))
x+=7;
return x;
}
void month_name(int m)

{
switch(m)
{
case 1 : cout<<"January";
break;
case 2 : cout<<"February";
break;
case 3 : cout<<"March";
break;
case 4 : cout<<"April";
break;
case 5 : cout<<"May";
break;
case 6 : cout<<"June";
break;
case 7 : cout<<"July";
break;
case 8 : cout<<"August";
break;
case 9 : cout<<"September";
break;
case 10: cout<<"October";
break;
case 11: cout<<"November";
break;
case 12: cout<<"December";

break;
}
}
int no_days(int mon, int y)

int d;
switch(mon)
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10:
case 12: d=31;
break;
case 4 :
case 6 :
case 9 :
case 11: d=30;
break;
case 2 : if(leap(y))
d=29;
else
d=28;
break;
}
return d;
}
int leap (int y)

{
if (y%100==0)
if (y%400==0)
return 1;
else
return 0;
else
if (y%4==0)
return 1;
else
return 0;
}

C++ Program for Overloading the difference operator for complex arithmetic

#include<iostream.h>
#include<conio.h>
class complex

float real;
float img;
public:

complex()
{
real = 0; img = 0;
}
complex(float a,float b)
{
real =a ; img = b;
}
complex operator -(complex );
void disp();
};
// Fn.for overloading of - operator for complex arithmetic
complex complex::operator-(complex a)
{
return complex(real-a.real,img-a.img);
}
// function for display of Real & Imaginary Parts
void complex::disp()

{
cout<<"
The real part is : "<<real;
cout<<"
The imaginary part is: "<<img;
}
void main(void)

{
complex c1(12.0,4.5),c2(8,6),c3;
cout<<"
The value of c1 is:<BR>;
c1.disp();
cout<<"
The value of c2 is:<BR>;
c2.disp();
cout<<"
The value of c3 is:<BR>;
c3.disp();
c3=c1-c2;
cout<<"
After c3=c1-c2, c3 is : <BR>;
c3.disp();
getch();
}

C++ Program illustrating function overloading

# include<iostream.h>
# include<conio.h>
int area(int side)

{
return side*side;
}
int area(int l , int b)

{
return l*b;
}
void main()

{
clrscr();
int (*p1)(int);
int (*p2)(int,int);
p1=area;
p2=area;
cout<<"Address of area(int)="<<(unsigned int)p1<<endl;
cout<<"Address of area(int,int)="<<(unsigned int)p2<<endl;
cout<<"Invoking area(int) via p1 "<<p1(20)<<endl;
cout<<"Invoking area(int,int) via p2 "<<p2(10,20);
getch();
}

C++ Class for storing residential addresses

// Implement a class for storing residential addresses.


# include<iostream.h>
# include<conio.h>
# include<string.h>
class address

{
char name[20];
char houseno[10];
char street[20];
char city[20];
long int pin;
public:

address()
{
strcpy(name,"Unspecified");
strcpy(houseno,"NA");
strcpy(street,"NA");
strcpy(city,"NA");
pin=0;
}
void put_data()
{
cout<<"
Enter the name of the person";

cin.getline(name,19);
cout<<"
Enter the House number";
cin.getline(houseno,9);
cout<<"
Enter the street address";
cin.getline(street,19);
cout<<"
Enter the city";
cin.getline(city,19);
cout<<"
Enter the pin code";
cin>>pin;
}
void get_data()
{
cout<<"***********************************************"<<endl;
cout<<"NAME : "<<name<<endl;
cout<<"HOUSE NO.: "<<houseno<<endl;
cout<<"STREET : "<<street<<endl;
cout<<"CITY : "<<city<<endl;
cout<<"PIN : "<<pin<<endl;
cout<<"***********************************************";
}
};
void main()

{
clrscr();
address obj;
obj.put_data();
obj.get_data();
getch();
}

C++ Class for storing a Person's details.

# include<iostream.h>
# include<string.h> // for strcpy
# include<conio.h>
class person

{
char name[20];
int yob; //Year of birth
int yod; // Year of death
public:

person()
{ strcpy(name,"N.A.");
yob=2000; yod=2000;

}
~person()
{
delete []name;
}
void getdata()
{
cout<<"
Enter the name : ";
cin.getline(name,19);
cout<<"
Enter the year of birth";
cin>>yob;
cout<<"
Enter the year of death";
cin>>yod;
}
void print()
{
cout<<"
Name:"<<name;
cout<<"
Year of Birth: "<<yob;
cout<<"
Year of death: "<<yod;
}
}; //end of the class
void main()

{
clrscr();
// demonstrating the working for single object
person p;
//getting the input
p.getdata();
//displaying the i/p data
p.print();
getch();
}

C++ Complete implementation of Publication Class


# include<iostream.h>
# include<conio.h>
class publication

{
public:

char title[30];
float price;

void getdata()
{
cout<<"Enter the title :
"; cin>>title;
cout<<"Enter the price :
"; cin>>price;
}
void putdata()
{
cout<<"The title is : "<<title<<endl;
cout<<"Price is : "<<price<<endl;
}
};
class sales

{
public:

float s1,s2,s3; // getting the sale of last three momths


void getdata()
{
cout<<"Enter the sale of first month:<BR>;
cin>>s1;
cout<<"Enter the sale of second month:<BR>;
cin>>s2;
cout<<"Enter the sale of third month:<BR>;
cin>>s3;
}
void putdata()
{
cout<<"Sale in first month : $"<<s1<<endl;
cout<<"Sale in sec. month : $"<<s2<<endl;
cout<<"Sale in third month : $"<<s3<<endl;
}

};
class book: private publication, private sales

{
public:

int pages;
void getdata()
{
cout<<" BOOK DETAILS <BR>;
publication::getdata();
sales::getdata();
cout<<"Enter the number of pages <BR>;
cin>>pages;
}
void putdata()
{
cout<<" BOOK DETAILS<BR>;
publication::putdata();
cout<<"Number of pages : " <<pages<<endl;
sales::putdata();

}
};
class tape: private publication,private sales

{
public:

float time;
void getdata()
{
cout<<" TAPE DETAILS<BR>;
publication::getdata();
sales::getdata();
cout<<"Enter the time length of the casette"; cin>>time;
}
void putdata()
{
cout<<" TAPE DETAILS ";
publication::putdata();
cout<<"Time length :"<< time<<endl;
sales::putdata();
}

};
void main()

{
book b;
tape t;
b.getdata();
t.getdata();
b.putdata();
t.putdata();
}

C++ - Create a C++ class 'rational' with data-member(num,den) and appropriate functionmember to add ,sub,div,multiply 2 numbers
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class rational
{
int num;
int den;
public:
void getrat();
void disprat();
rational addrat(rational &rr);
rational subrat(rational &rr);
rational mulrat(rational &rr);
rational divrat(rational &rr);
};
void main()
{
rational r1,r2,r3;
cout<<"enter the first rational number";
r1.getrat();
cout<<"enter the second rational number";
r2.getrat();

cout<<"addition of two rational nos is";


r3=r1.addrat(r2);
r3.disprat();
cout<<"substraction of two rational nos is";
r3=r1.subrat(r2);
r3.disprat();
cout<<"multiplication of two rational nos is";
r3=r1.mulrat(r2);
r3.disprat();
cout<<"division of two rational nos is";
r3=r1.divrat(r2);
r3.disprat();
getch();
}
void rational:: getrat()
{
cin>>num>>den;
}
void rational::disprat()
{
cout<<"\nrational numberis "<<num<<"/"<<den;
}
rational rational :: subrat(rational &rr)
{
rational temp;
if(den==rr.den)
{
temp.num=num-rr.num;
temp.den=den;
}
else
{
temp.num=num*rr.den-den*rr.num;
temp.den=den*rr.den;
}
return temp;
}
rational rational :: addrat(rational &rr)
{
rational temp;
if(den!=0 && rr.den!=0)
{
if(den==rr.den)
{
temp.num=num+rr.num;
temp.den=den;
}
else
{
temp.num=num*rr.den+den*rr.num;
temp.den=den*rr.den;
}
return temp;
}
else

exit(0);
}
rational rational :: mulrat(rational &rr)
{
rational temp;
temp.num=num*rr.num;
temp.den=den*rr.den;
return temp;
}
rational rational :: divrat(rational &rr)
{
rational temp;
temp.num=num*rr.den;
temp.den=den*rr.num;
return temp;
}

Result Management Program in CPP - (Mini Project)


Description : This code is designed to maintain the result record of students (in schools
and college). This code will work well on '50 Line' settings.
/* WELCOME TO THE PROJECT 'RESULT MANAGEMENT' */

#include<conio.h>
#include<process.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<dos.h>
class student
{
private:

// INCLUDING ALL THE HEADER FILES //

// CLASS NAMED 'STUDENT' TO CALCULATE GRADE,TOTAL AND PERCANTAGE //

public:
char grade(long int x,int y);
long int total(int a,int b,int c,int d,int e);
float percent(long int z);
};
char student::grade(long int x,int y)
{ // FUNCTION TO CALCULATE GRADE //
if((x*100/y)>74)
return('A');
else
{
if((x*100/y)>60)
return('B');
else

if((x*100/y)>33)
return('C');
else
return('E');

long int student::total(int a,int b,int c,int d,int e)


{ // FUNCTION TO CALCULATE TOTAL MARKS //
int t;
t=(a+b+c+d+e);
return (t);
}
float student::percent(long int z)
{ // FUNCTION TO CALCULATE PERCANTAGE //
float p=0;
p=(z*100)/410;
return(p);
}
// STRUCTURE TO INPUT DETAILS OF STUDENT //
struct studres
{
int clas;
char s;
int rollno;
char name[30];
char fatername[30];
char teachname[30];
int engmark;
int matmark;
int phymark;
int chemark;
int commark;
int total;
float percentage;
}stdres;
struct temp
{
int roll;
char name[30];
int clas;
char sec;
int total;
}temp;
// ALL THE FUNCTIONS INITIALIZED //
void create();
void add();
void search();
void modify();
void merit();
void merit1();
void delet();
char grade(long int x,int y);
long int total(int a,int b,int c,int d,int e);

float percent(long int z);


int checking(int z, char y, int x);
void menu();
void main()
{ // MAIN PROGRAM STARTS HERE //
clrscr();
gotoxy(20,6);
cout<<" R E S U L T M A N A G E M E N T";
gotoxy(10,10);
cout<<" By Suansh Singhal of class XII D";
getch();
clrscr();
menu(); // CALLING FUNCTION TO PRINT MENU //
}
void create()
{ // FUNCTION TO CREATE THE MAIN FILE //
clrscr();
char ans;
fstream result;
gotoxy(20,6);
cout<<"WARNING !!! ALL PREVIOUS RECORDS WILL BE DELETED";
gotoxy(10,10);
cout<<" DO YOU STILL WANT TO CONTINUE(Y/N):";
cin>>ans;
if(ans != 'N' || ans != 'n')
{
result.open("result.dat",ios::out|ios::binary);
result.close();
}
menu();
}
void add()
{ // FUNCTION TO ADD DETAILS OF THE STUDENT //
top:
clrscr();
int max1=100,max2=70,kar,l,m;
char ch,n;
cout<<"CLASS:";
gotoxy (16,1);
cout<<"SECTION:";
gotoxy(31,1);
cout<<"ROLL NO:";
gotoxy(47,1);
cout<<"STUDENT'S NAME:";
gotoxy(1,5);
cout<<"FATHER NAME:";
gotoxy(45,5);
cout<<"CLASS TEACHER:";
gotoxy(1,9);
cout<<"S.NO.";
gotoxy(15,9);
cout<<"SUBJECT";
gotoxy(30,9);
cout<<"MARKS";
gotoxy(40,9);

cout<<"MAXIMUM MARKS";
gotoxy(70,9);
cout<<"GRADE";
gotoxy(3,11);
cout<<"1";
gotoxy(15,11);
cout<<"ENGLISH";
gotoxy(3,13);
cout<<"2";
gotoxy(15,13);
cout<<"MATHS";
gotoxy(3,15);
cout<<"3";
gotoxy(15,15);
cout<<"PHYSICS";
gotoxy(3,17);
cout<<"4";
gotoxy(15,17);
cout<<"CHEMISTRY";
gotoxy(3,19);
cout<<"5";
gotoxy(15,19);
cout<<"COMPUTER";
gotoxy(3,25);
cout<<"6";
gotoxy(15,25);
cout<<"TOTAL";
gotoxy(3,27);
cout<<"7";
gotoxy(15,27);
cout<<"PERCENTAGE";
// getting student information //
gotoxy(8,1);
cin>>m;
gotoxy(25,1);
n=getchar();
fflush(stdin);
fstream result;
result.open("result.dat",ios::app|ios::binary);
gotoxy(40,1);
cin>>l;
kar=checking(m,n,l);
if(kar==1)
{
result.close();
goto top;
}
else
{
stdres.clas=m;
stdres.s=n;
stdres.rollno=l;
}
gotoxy(63,1);
gets(stdres.name);
fflush(stdin);
gotoxy(14,5);
gets(stdres.fatername);
fflush(stdin);
gotoxy(60,5);

gets(stdres.teachname);
gotoxy(47,11); /*marks table*/
cout<<"100";
gotoxy(32,11);
cin>>stdres.engmark;
gotoxy(72,11);
cout<<grade(stdres.engmark,max1);
gotoxy(47,13);
cout<<"100";
gotoxy(32,13);
cin>>stdres.matmark;
gotoxy(72,13);
cout<<grade(stdres.matmark,max1);
gotoxy(47,15);
cout<<" 70";
gotoxy(32,15);
cin>>stdres.phymark;
gotoxy(72,15);
cout<<grade(stdres.phymark,max2);
gotoxy(47,17);
cout<<" 70";
gotoxy(32,17);
cin>>stdres.chemark;
gotoxy(72,17);
cout<<grade(stdres.chemark,max2);
gotoxy(47,19);
cout<<" 70";
gotoxy(32,19);
cin>>stdres.commark;
gotoxy(72,19);
cout<<grade(stdres.commark,max2);
gotoxy(47,25);
cout<<"410";
gotoxy(31,25);
cout<<total(stdres.engmark,stdres.matmark,stdres.phymark,stdres.chemark,st
dres.commark);
stdres.total=total(stdres.engmark,stdres.matmark,stdres.phymark,stdres.che
mark,stdres.commark);
gotoxy(32,27);
stdres.percentage=percent(stdres.total);
cout<<stdres.percentage;
gotoxy(80,50);

result.write((char*)&stdres,sizeof(stdres));
result.close();
getch();
menu();

char grade(long int x,int y)


{ // FUNCTION THAT CALCULATES GRADE OF THE STUDENT //
if((x*100/y)>74)
return('A');

else
{

if((x*100/y)>60)
return('B');
else
{
if((x*100/y)>33)
return('C');
else
return('E');
}

}
long int total(int a,int b,int c,int d,int e)
{ // FUNCTION TO CALCULATE TOTAL OF STUDENT //
int t;
t=(a+b+c+d+e);
return (t);
}
float percent(long int z)
{
float p=0;
p=(z*100)/410;
return(p);
}
int checking(int z, char y, int x)
{ // FUNCTION TO CALCULATE WETHER STUDENT EXISTS //
int suansh=0;
fstream result;
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.clas==z&&stdres.s==y&&stdres.rollno==x)
{
clrscr();
gotoxy(22,25);
cout<<"ENTRY ALREADY EXISTS";
getch();
suansh=1;
break;
}
result.read((char*)&stdres, sizeof(stdres));
}
return(suansh);
}
void menu()
// FUNCTION THAT PRINTS THE MAIN MENU //
{
clrscr();
int ans;
gotoxy(25,10);
cout<<"M A I N M E N U";
gotoxy(17,15);
cout<<"1. Create the Primary Result Register";
gotoxy(17,16);
cout<<"2. Add Result";
gotoxy(17,17);

cout<<"3. Modify Result";


gotoxy(17,18);
cout<<"4. Search Result";
gotoxy(17,19);
cout<<"5. Delete Result";
gotoxy(17,20);
cout<<"6. Generate Merit List Grade wise per subject";
gotoxy(17,21);
cout<<"7. Generate Merit List on basis of Total Marks";
gotoxy(17,22);
cout<<"8. Exit";
gotoxy(40,25);
cout<<"Enter Option(1-8):";
cin>>ans;
// SWITCH USED TO MAKE PROGRAM MENU BASED //
switch(ans)
{
case 1:create();
break;
case 2: add();
break;
case 3: modify();
break;
case 4: search();
break;
case 5: delet();
break;
case 6: merit();
break;
case 7: merit1();
break;
case 8: exit(0);
}
}
void search()
{ // FUNCTION TO SEARCH A STUDENT //
{
clrscr();
int max1=100,max2=70,roll,flag=0;
char ch;
gotoxy(23,15);
cout<<"Enter the Roll No. of the student: ";
cin>>roll;
fstream result;
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.rollno==roll)
{
flag=1;
clrscr();
gotoxy(23,25);
cout<<"RECORD FOUND";
gotoxy(23,27);
cout<<"PRESS ANY KEY TO CONTINUE ...";
getch();

clrscr();
cout<<"CLASS:";
gotoxy (16,1);
cout<<"SECTION:";
gotoxy(31,1);
cout<<"ROLL NO:";
gotoxy(47,1);
cout<<"STUDENT'S NAME:";
gotoxy(1,5);
cout<<"FATHER NAME:";
gotoxy(45,5);
cout<<"CLASS TEACHER:";
gotoxy(1,9);
cout<<"S.NO.";
gotoxy(15,9);
cout<<"SUBJECT";
gotoxy(30,9);
cout<<"MARKS";
gotoxy(40,9);
cout<<"MAXIMUM MARKS";
gotoxy(70,9);
cout<<"GRADE";
gotoxy(3,11);
cout<<"1";
gotoxy(15,11);
cout<<"ENGLISH";
gotoxy(3,13);
cout<<"2";
gotoxy(15,13);
cout<<"MATHS";
gotoxy(3,15);
cout<<"3";
gotoxy(15,15);
cout<<"PHYSICS";
gotoxy(3,17);
cout<<"4";
gotoxy(15,17);
cout<<"CHEMISTRY";
gotoxy(3,19);
cout<<"5";
gotoxy(15,19);
cout<<"COMPUTER";
gotoxy(3,25);
cout<<"6";
gotoxy(15,25);
cout<<"TOTAL";
gotoxy(3,27);
cout<<"7";
gotoxy(15,27);
cout<<"PERCENTAGE";
gotoxy(8,1);
cout<<stdres.clas; /*printing student information*/
gotoxy(25,1);
cout<<stdres.s;
gotoxy(40,1);
cout<<stdres.rollno;
gotoxy(63,1);
puts(stdres.name);
fflush(stdout);
gotoxy(14,5);
puts(stdres.fatername);
fflush(stdout);

gotoxy(60,5);
puts(stdres.teachname);
gotoxy(47,11); /*marks table*/
cout<<"100";
gotoxy(32,11);
cout<<stdres.engmark;
gotoxy(72,11);
cout<<grade(stdres.engmark,max1);
gotoxy(47,13);
cout<<"100";
gotoxy(32,13);
cout<<stdres.matmark;
gotoxy(72,13);
cout<<grade(stdres.matmark,max1);
gotoxy(47,15);
cout<<" 70";
gotoxy(32,15);
cout<<stdres.phymark;
gotoxy(72,15);
cout<<grade(stdres.phymark,max2);
gotoxy(47,17);
cout<<" 70";
gotoxy(32,17);
cout<<stdres.chemark;
gotoxy(72,17);
cout<<grade(stdres.chemark,max2);
gotoxy(47,19);
cout<<" 70";
gotoxy(32,19);
cout<<stdres.commark;
gotoxy(72,19);
cout<<grade(stdres.commark,max2);
gotoxy(47,25);
cout<<"410";
gotoxy(31,25);
cout<<total(stdres.engmark,stdres.matmark,stdres.phymark,stdres.ch

emark,st

dres.commark);
stdres.total=total(stdres.engmark,stdres.matmark,stdres.phymark,st

dres.che

mark,stdres.commark);
gotoxy(32,27);
stdres.percentage=percent(stdres.total);
cout<<stdres.percentage;
gotoxy(80,50);
result.close();
getch();
menu();
}
else
result.read((char*)&stdres,sizeof(stdres));

}
if(flag==0)
{

}
}

clrscr();
gotoxy(23,23);
cout<<"SORRY! The Roll No."<<roll<<" does not exist.";
gotoxy(23,25);
cout<<" PLEASE! TRY AGAIN.";
result.close();
getch();
menu();

void modify()
{ // FUNCTION TO MODIFY STUDENT'S DATA //
clrscr();
int max1=100,max2=70,roll,flag=0,flag2=0;
char ch,z,n;
gotoxy(23,15);
cout<<"Enter the Roll No. of the student: ";
cin>>roll;
fstream result,tfile;
result.open("result.dat",ios::in|ios::binary);
tfile.open("tfile.dat",ios::out|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.rollno==roll)
{
flag=1;
clrscr();
gotoxy(23,25);
cout<<"RECORD FOUND";
gotoxy(23,27);
cout<<"PRESS ANY KEY TO CONTINUE ...";
getch();
clrscr();
cout<<"CLASS:";
gotoxy (16,1);
cout<<"SECTION:";
gotoxy(31,1);
cout<<"ROLL NO:";
gotoxy(47,1);
cout<<"STUDENT'S NAME:";
gotoxy(1,5);
cout<<"FATHER NAME:";
gotoxy(45,5);
cout<<"CLASS TEACHER:";
gotoxy(1,9);
cout<<"S.NO.";
gotoxy(15,9);
cout<<"SUBJECT";
gotoxy(30,9);
cout<<"MARKS";

gotoxy(40,9);
cout<<"MAXIMUM MARKS";
gotoxy(70,9);
cout<<"GRADE";
gotoxy(3,11);
cout<<"1";
gotoxy(15,11);
cout<<"ENGLISH";
gotoxy(3,13);
cout<<"2";
gotoxy(15,13);
cout<<"MATHS";
gotoxy(3,15);
cout<<"3";
gotoxy(15,15);
cout<<"PHYSICS";
gotoxy(3,17);
cout<<"4";
gotoxy(15,17);
cout<<"CHEMISTRY";
gotoxy(3,19);
cout<<"5";
gotoxy(15,19);
cout<<"COMPUTER";
gotoxy(3,25);
cout<<"6";
gotoxy(15,25);
cout<<"TOTAL";
gotoxy(3,27);
cout<<"7";
gotoxy(15,27);
cout<<"PERCENTAGE";
gotoxy(8,1);
cout<<stdres.clas; /*printing student information*/
gotoxy(25,1);
cout<<stdres.s;
gotoxy(40,1);
cout<<stdres.rollno;
gotoxy(63,1);
puts(stdres.name);
fflush(stdout);
gotoxy(14,5);
puts(stdres.fatername);
fflush(stdout);
gotoxy(60,5);
puts(stdres.teachname);
gotoxy(47,11); /*marks table*/
cout<<"100";
gotoxy(32,11);
cout<<stdres.engmark;
gotoxy(72,11);
cout<<grade(stdres.engmark,max1);
gotoxy(47,13);
cout<<"100";
gotoxy(32,13);
cout<<stdres.matmark;
gotoxy(72,13);
cout<<grade(stdres.matmark,max1);
gotoxy(47,15);

cout<<" 70";
gotoxy(32,15);
cout<<stdres.phymark;
gotoxy(72,15);
cout<<grade(stdres.phymark,max2);
gotoxy(47,17);
cout<<" 70";
gotoxy(32,17);
cout<<stdres.chemark;
gotoxy(72,17);
cout<<grade(stdres.chemark,max2);
gotoxy(47,19);
cout<<" 70";
gotoxy(32,19);
cout<<stdres.commark;
gotoxy(72,19);
cout<<grade(stdres.commark,max2);
gotoxy(47,25);
cout<<"410";
gotoxy(31,25);
st

cout<<total(stdres.engmark,stdres.matmark,stdres.phymark,stdres.chemark,
dres.commark);

he

stdres.total=total(stdres.engmark,stdres.matmark,stdres.phymark,stdres.c
mark,stdres.commark);
gotoxy(32,27);
stdres.percentage=percent(stdres.total);
cout<<stdres.percentage;

getch();
gotoxy(32,40);
cout<<"WOULD YOU LIKE TO MAKE ANY CHANGES
";
cout<<"IF YES .. PRESS 'A' ";
z=getch();
if(z=='a'||z=='A')
{
flag2=1;
clrscr(); /*again printing format for modification*/
cout<<"CLASS:";
gotoxy (16,1);
cout<<"SECTION:";
gotoxy(31,1);
cout<<"ROLL NO:";
gotoxy(47,1);
cout<<"STUDENT'S NAME:";
gotoxy(1,5);
cout<<"FATHER NAME:";
gotoxy(45,5);
cout<<"CLASS TEACHER:";
gotoxy(1,9);
cout<<"S.NO.";
gotoxy(15,9);

cout<<"SUBJECT";
gotoxy(30,9);
cout<<"MARKS";
gotoxy(40,9);
cout<<"MAXIMUM MARKS";
gotoxy(70,9);
cout<<"GRADE";
gotoxy(3,11);
cout<<"1";
gotoxy(15,11);
cout<<"ENGLISH";
gotoxy(3,13);
cout<<"2";
gotoxy(15,13);
cout<<"MATHS";
gotoxy(3,15);
cout<<"3";
gotoxy(15,15);
cout<<"PHYSICS";
gotoxy(3,17);
cout<<"4";
gotoxy(15,17);
cout<<"CHEMISTRY";
gotoxy(3,19);
cout<<"5";
gotoxy(15,19);
cout<<"COMPUTER";
gotoxy(3,25);
cout<<"6";
gotoxy(15,25);
cout<<"TOTAL";
gotoxy(3,27);
cout<<"7";
gotoxy(15,27);
cout<<"PERCENTAGE";
gotoxy(8,1);
cout<<stdres.clas; /*printing student information*/
gotoxy(25,1);
cout<<stdres.s;
gotoxy(40,1);
cout<<stdres.rollno;
gotoxy(63,1);

gotoxy(63,1);
gets(stdres.name);
fflush(stdin);
gotoxy(14,5);
gets(stdres.fatername);
fflush(stdin);
gotoxy(60,5);
gets(stdres.teachname);
gotoxy(47,11); /*marks table*/
cout<<"100";
gotoxy(32,11);
cin>>stdres.engmark;
gotoxy(72,11);
cout<<grade(stdres.engmark,max1);

gotoxy(47,13);
cout<<"100";
gotoxy(32,13);
cin>>stdres.matmark;
gotoxy(72,13);
cout<<grade(stdres.matmark,max1);
gotoxy(47,15);
cout<<" 70";
gotoxy(32,15);
cin>>stdres.phymark;
gotoxy(72,15);
cout<<grade(stdres.phymark,max2);
gotoxy(47,17);
cout<<" 70";
gotoxy(32,17);
cin>>stdres.chemark;
gotoxy(72,17);
cout<<grade(stdres.chemark,max2);
gotoxy(47,19);
cout<<" 70";
gotoxy(32,19);
cin>>stdres.commark;
gotoxy(72,19);
cout<<grade(stdres.commark,max2);
gotoxy(47,25);
cout<<"410";
gotoxy(31,25);
cout<<total(stdres.engmark,stdres.matmark,stdres.phymark,stdres.ch
emark,st

dres.commark);
stdres.total=total(stdres.engmark,stdres.matmark,stdres.phymark,st

dres.che

mark,stdres.commark);
gotoxy(32,27);
stdres.percentage=percent(stdres.total);
cout<<stdres.percentage;
gotoxy(80,50);
tfile.write((char*)&stdres,sizeof(stdres));
result.read((char*)&stdres,sizeof(stdres));
}
else
{

getch();

tfile.write((char*)&stdres,sizeof(stdres));
result.read((char*)&stdres,sizeof(stdres));
}
}
else
{
tfile.write((char*)&stdres,sizeof(stdres));

result.read((char*)&stdres,sizeof(stdres));
}
}
result.close();
tfile.close();
if(flag2==1)
{
result.open("result.dat",ios::out|ios::binary);
tfile.open("tfile.dat",ios::in|ios::binary);
tfile.read((char*)&stdres,sizeof(stdres));
while(tfile)
{
result.write((char*)&stdres,sizeof(stdres));
tfile.read((char*)&stdres,sizeof(stdres));
}
result.close();
tfile.close();

}
if(flag==0)
{
clrscr();
gotoxy(23,23);
cout<<"SORRY! The Roll No."<<roll<<" does not exist.";
gotoxy(23,25);
cout<<" PLEASE! TRY AGAIN.";
result.close();
getch();
}
menu();
}

void merit1()
{ // FUNCTION TO GENERATE MERIT LIST //
clrscr();
int larg,l,flag=0;
gotoxy(15,17);
cout<<"Press any key for the merit list <BR>;
gotoxy(80,50);
getch();
clrscr();
gotoxy(15,17);
cout<<"PLEASE wait while the list is being generated...";
gotoxy(80,50);
delay(2000);
clrscr();
gotoxy(15,15);
cout<<"LIST generated.";
gotoxy(15,17);

cout<<"press any key to continue...";


gotoxy(80,50);
getch();
clrscr();
fstream result,tfile;
result.open("result.dat",ios::in|ios::binary);
tfile.open("tfile.dat",ios::out|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
l=total(stdres.engmark,stdres.matmark,stdres.phymark,stdres.chemark,stdres
commark);
if(l>368)
{
temp.roll=stdres.rollno;
strcpy(temp.name,stdres.name);
temp.clas=stdres.clas;
temp.sec=stdres.s;
temp.total=l;
tfile.write((char*)&temp,sizeof(temp));
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();
tfile.close();
gotoxy(12,1);
cout<<"ROLL NO.";
gotoxy(22,1);
cout<<"NAME";
gotoxy(38,1);
cout<<"TOTAL";
int k=3;
for(int i=410;i>368;i--)
{
tfile.open("tfile.dat",ios::in|ios::binary);
tfile.read((char*)&temp,sizeof(temp));
while(tfile)
{
if(temp.total==i)
{
flag=1;
gotoxy(15,k);
cout<<temp.roll;
gotoxy(22,k);
cout<<temp.name;
gotoxy(39,k++);
cout<<temp.total;
k++;
}
tfile.read((char*)&temp,sizeof(temp));
}
tfile.close();

}
if(flag==0)
{
clrscr();
gotoxy(15,20);
cout<<"SORRY! No record found.";
}
gotoxy(80,50);
getch();
menu();
}
void merit()
{
// FUNCTION TO GENERATE SECOND MERIT LIST //
reverse:
clrscr();
int n,c,k=3,flag=0,i;
gotoxy(15,2);
cout<<"MENU";
gotoxy(15,4);
cout<<"1. ENGLISH";
gotoxy(15,5);
cout<<"2. MATHEMATICS";
gotoxy(15,6);
cout<<"3. PHYSICS";
gotoxy(15,7);
cout<<"4. CHEMISTRY";
gotoxy(15,8);
cout<<"5. COMPUTER";
gotoxy(15,18);
cout<<"Enter your option: ";
cin>>n;
clrscr();
fstream result;
switch(n)
{
/*english*/
case 1: back1:
gotoxy(15,2);
cout<<"1. Students with Grade
gotoxy(15,3);
cout<<"2. Students with Grade
gotoxy(15,4);
cout<<"3. Students with Grade
gotoxy(15,5);
cout<<"4. Students with Grade
gotoxy(15,7);
cout<<"Enter your Option: ";
cin>>c;
clrscr();
gotoxy(14,1);
cout<<"Roll no.";
gotoxy(25,1);
cout<<"Name";
gotoxy(37,1);
cout<<"Class";

'A'";
'B'";
'C'";
'E' ";

gotoxy(45,1);
cout<<"Section";
gotoxy(60,1);
cout<<"<Marks";
switch(c)
{
case 1: for(i=100;i>74;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.engmark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.engmark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();
gotoxy(80,50);
}
if(flag==0)
{
gotoxy(20,22); gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
case 2: for(i=74;i>60;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.engmark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);

cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.engmark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();
}
gotoxy(80,50);
if(flag==0)
{
gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
case 3: for(i=60;i>33;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.engmark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.engmark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();
}
gotoxy(80,50);
if(flag==0)
{
gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
case 4: for( i=33;i>0;i--)

{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.engmark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.engmark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();
}
gotoxy(80,50);
if(flag==0)
{
gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
default:clrscr();
gotoxy(25,25);
cout<<"Invalid Option.";
getch();
goto back1;
}
menu();
/*mathematics*/
case 2: back2:
gotoxy(15,2);
cout<<"1. Students with Grade
gotoxy(15,3);
cout<<"2. Students with Grade
gotoxy(15,4);
cout<<"3. Students with Grade
gotoxy(15,5);
cout<<"4. Students with Grade
gotoxy(15,7);
cout<<"Enter your Option: ";
cin>>c;
clrscr();
gotoxy(14,1);
cout<<"Roll no.";
gotoxy(25,1);

'A'";
'B'";
'C'";
'E' ";

cout<<"Name";
gotoxy(37,1);
cout<<"Class";
gotoxy(45,1);
cout<<"Section";
gotoxy(60,1);
cout<<"Marks";
switch(c)
{
case 1: for(i=100;i>74;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.matmark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.matmark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();
gotoxy(80,50);
}
if(flag==0)
{
gotoxy(20,22); gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
case 2: for(i=74;i>60;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.matmark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;

gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.matmark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();

}
gotoxy(80,50);
if(flag==0)
{

gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
case 3: for(i=60;i>33;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.matmark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.matmark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();

}
gotoxy(80,50);
if(flag==0)
{
gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}

getch();
break;
case 4: for( i=33;i>0;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.matmark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.matmark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();

}
gotoxy(80,50);
if(flag==0)
{
gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
default:clrscr();
gotoxy(25,25);
cout<<"Invalid Option.";
getch();
goto back2;
}
menu();
/*physics*/
case 3: back3:
gotoxy(15,2);
cout<<"1. Students with Grade
gotoxy(15,3);
cout<<"2. Students with Grade
gotoxy(15,4);
cout<<"3. Students with Grade
gotoxy(15,5);
cout<<"4. Students with Grade
gotoxy(15,7);
cout<<"Enter your Option: ";
cin>>c;
clrscr();

'A'";
'B'";
'C'";
'E' ";

gotoxy(14,1);
cout<<"Roll no.";
gotoxy(25,1);
cout<<"Name";
gotoxy(37,1);
cout<<"Class";
gotoxy(45,1);
cout<<"Section";
gotoxy(60,1);
cout<<"Marks";
switch(c)
{
case 1: for(i=100;i>74;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.phymark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.phymark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();
gotoxy(80,50);
}
if(flag==0)
{
gotoxy(20,22); gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
case 2: for(i=74;i>60;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.phymark==i)
{

flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.phymark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();
}
gotoxy(80,50);
if(flag==0)
{
gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
case 3: for(i=60;i>33;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.phymark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.phymark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();
}
gotoxy(80,50);
if(flag==0)
{
gotoxy(20,22);

cout<<"NO RECORD FOUND.";


gotoxy(80,50);
}
getch();
break;
case 4: for( i=33;i>0;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.phymark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.phymark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();
}
gotoxy(80,50);
if(flag==0)
{
gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
default:clrscr();
gotoxy(25,25);
cout<<"Invalid Option.";
getch();
goto back3;
}
menu();
/*chemistry*/
case 4: back4:
gotoxy(15,2);
cout<<"1. Students
gotoxy(15,3);
cout<<"2. Students
gotoxy(15,4);
cout<<"3. Students
gotoxy(15,5);
cout<<"4. Students
gotoxy(15,7);

with Grade 'A'";


with Grade 'B'";
with Grade 'C'";
with Grade 'E' ";

cout<<"Enter your Option: ";


cin>>c;
clrscr();
gotoxy(14,1);
cout<<"Roll no.";
gotoxy(25,1);
cout<<"Name";
gotoxy(37,1);
cout<<"Class";
gotoxy(45,1);
cout<<"Section";
gotoxy(60,1);
cout<<"Marks";
switch(c)
{
case 1: for(i=100;i>74;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.chemark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.chemark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();
gotoxy(80,50);
}
if(flag==0)
{
gotoxy(20,22); gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
case 2: for(i=74;i>60;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)

if(stdres.chemark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.chemark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));

}
result.close();

}
gotoxy(80,50);
if(flag==0)
{

gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
case 3: for(i=60;i>33;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.chemark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.chemark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();

}
gotoxy(80,50);

if(flag==0)
{
gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
case 4: for( i=33;i>0;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.chemark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.chemark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();

}
gotoxy(80,50);
if(flag==0)
{
gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
default:clrscr();
gotoxy(25,25);
cout<<"Invalid Option.";
getch();
goto back4;
}
menu();
/*computer*/
case 5: back5:
gotoxy(15,2);
cout<<"1. Students with Grade 'A'";
gotoxy(15,3);
cout<<"2. Students with Grade 'B'";
gotoxy(15,4);
cout<<"3. Students with Grade 'C'";

gotoxy(15,5);
cout<<"4. Students with Grade 'E' ";
gotoxy(15,7);
cout<<"Enter your Option: ";
cin>>c;
clrscr();
gotoxy(14,1);
cout<<"Roll no.";
gotoxy(25,1);
cout<<"Name";
gotoxy(37,1);
cout<<"Class";
gotoxy(45,1);
cout<<"Section";
gotoxy(60,1);
cout<<"Marks";
switch(c)
{
case 1: for(i=100;i>74;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.commark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.commark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();
gotoxy(80,50);
}
if(flag==0)
{
gotoxy(20,22); gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
case 2: for(i=74;i>60;i--)
{

result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.commark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.commark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();
}
gotoxy(80,50);
if(flag==0)
{
gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
case 3: for(i=60;i>33;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.commark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.commark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));

}
result.close();
}
gotoxy(80,50);
if(flag==0)
{
gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
case 4: for( i=33;i>0;i--)
{
result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.commark==i)
{
flag=1;
gotoxy (17,k);
cout<<stdres.rollno;
gotoxy(25,k);
cout<<stdres.name;
gotoxy(39,k);
cout<<stdres.clas;
gotoxy(47,k);
cout<<stdres.s;
gotoxy(62,k++);
cout<<stdres.commark;
k++;
}
result.read((char*)&stdres,sizeof(stdres));
}
result.close();

}
gotoxy(80,50);
if(flag==0)
{
gotoxy(20,22);
cout<<"NO RECORD FOUND.";
gotoxy(80,50);
}
getch();
break;
default:clrscr();
gotoxy(25,25);
cout<<"Invalid Option.";
getch();
goto back5;
}
menu();
default: clrscr();
cout<<"INVALID OPTION";
getch();
menu();

gotoxy(80,50);
}
void delet()
{ // FUNCTION TO DELETE A RESULT ENTRY //
clrscr();
student stud;
int r,s,flag=0;
char t,z;
cout<<"Enter the class: ";
cin>>s;
cout<<"Enter the section: ";
cin>>t;
cout<<"Enter the roll no.";
cin>>r;
fstream result,tfile;
result.open("result.dat",ios::in|ios::binary);
tfile.open("tfile.dat",ios::out|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
if(stdres.clas==s&&stdres.s==t&&stdres.rollno==r)
{
clrscr();
flag=1;
gotoxy(20,20);
cout<<"RECORD FOUND AND DELETED.";
getch();
}
else
tfile.write((char*)&stdres,sizeof(stdres));
result.read((char*)&stdres,sizeof(stdres));
}
result.close();
tfile.close();
if(flag==0)
{
clrscr();
gotoxy(25,22);
cout<<"RECORD NOT FOUND.";
getch();
exit(0);
}
result.open("result.dat",ios::out|ios::binary);
tfile.open("tfile.dat",ios::in|ios::binary);
tfile.read((char*)&stdres,sizeof(stdres));
while(tfile)
{

result.write((char*)&stdres,sizeof(stdres));
tfile.read((char*)&stdres,sizeof(stdres));
}
result.close();
tfile.close();
/*DISPLAYING THE RECORDS AFTER DELETION*/
clrscr();
cout<<"New File after Deletion of Record";
getch();

result.open("result.dat",ios::in|ios::binary);
result.read((char*)&stdres,sizeof(stdres));
while(result)
{
clrscr();
int max1=100,max2=70;
char ch;
cout<<"CLASS:";
gotoxy (16,1);
cout<<"SECTION:";
gotoxy(31,1);
cout<<"ROLL NO:";
gotoxy(47,1);
cout<<"STUDENT'S NAME:";
gotoxy(1,5);
cout<<"FATHER NAME:";
gotoxy(45,5);
cout<<"CLASS TEACHER:";
gotoxy(1,9);
cout<<"S.NO.";
gotoxy(15,9);
cout<<"SUBJECT";
gotoxy(30,9);
cout<<"MARKS";
gotoxy(40,9);
cout<<"MAXIMUM MARKS";
gotoxy(70,9);
cout<<"GRADE";
gotoxy(3,11);
cout<<"1";
gotoxy(15,11);
cout<<"ENGLISH";
gotoxy(3,13);
cout<<"2";
gotoxy(15,13);
cout<<"MATHS";
gotoxy(3,15);
cout<<"3";
gotoxy(15,15);
cout<<"PHYSICS";
gotoxy(3,17);
cout<<"4";
gotoxy(15,17);
cout<<"CHEMISTRY";
gotoxy(3,19);
cout<<"5";

gotoxy(15,19);
cout<<"COMPUTER";
gotoxy(3,25);
cout<<"6";
gotoxy(15,25);
cout<<"TOTAL";
gotoxy(3,27);
cout<<"7";
gotoxy(15,27);
cout<<"PERCENTAGE";
gotoxy(8,1);
cout<<stdres.clas; /*printing student information*/
gotoxy(25,1);
cout<<stdres.s;
gotoxy(40,1);
cout<<stdres.rollno;
gotoxy(63,1);
puts(stdres.name);
fflush(stdout);
gotoxy(14,5);
puts(stdres.fatername);
fflush(stdout);
gotoxy(60,5);
puts(stdres.teachname);
gotoxy(47,11); /*marks table*/
cout<<"100";
gotoxy(32,11);
cout<<stdres.engmark;
gotoxy(72,11);
cout<<stud.grade(stdres.engmark,max1);
gotoxy(47,13);
cout<<"100";
gotoxy(32,13);
cout<<stdres.matmark;
gotoxy(72,13);
cout<<stud.grade(stdres.matmark,max1);
gotoxy(47,15);
cout<<" 70";
gotoxy(32,15);
cout<<stdres.phymark;
gotoxy(72,15);
cout<<stud.grade(stdres.phymark,max2);
gotoxy(47,17);
cout<<" 70";
gotoxy(32,17);
cout<<stdres.chemark;
gotoxy(72,17);
cout<<stud.grade(stdres.chemark,max2);
gotoxy(47,19);
cout<<" 70";
gotoxy(32,19);
cout<<stdres.commark;
gotoxy(72,19);
cout<<stud.grade(stdres.commark,max2);
gotoxy(47,25);
cout<<"410";

gotoxy(31,25);
cout<< stud.total(stdres.engmark, stdres.matmark, stdres.phymark,
stdres.chemark,stdres.commark);
stdres.total = stud.total(stdres.engmark,stdres.matmark, stdres.phymark,
stdres.chemark, stdres.commark);
gotoxy(32,27);
stdres.percentage=stud.percent(stdres.total);
cout<<stdres.percentage;
gotoxy(80,50);
result.read((char*)&stdres,sizeof(stdres));
getch();
}
result.close();
menu();
}

C++ Create a Class to change distance feet and inches to meter and
centimeter and vice-versa
#include<iostream.h>
#include<conio.h>
class DistDm;
class DistFt
{
float ft;
float in;
public:
void getft();
void dispft();
friend class DistDm;
};
void DistFt::getft()
{
cin>>ft>>in;
}
void DistFt::dispft()
{
cout<<"\n Distance="<<ft<<"\'"<<in<<"\" ";
}
class DistDm
{
int mtr;
int cm;
public:
void getdm();
void dispdm();
DistDm adddist(DistFt &ff1);
};
void DistDm::getdm()

cin>>mtr>>cm;
}
void DistDm::dispdm()
{
cout<<"\n Distance ="<<mtr<<"m"<<cm<<"cm";
}
DistDm DistDm::adddist(DistFt &ff1)
{
DistDm temp;
float k;
k=(ff1.ft*12+ff1.in)*2.54;
temp.cm=cm+k;
temp.mtr=mtr+int(temp.cm/100);
temp.cm=int(temp.cm)%100;
return temp;
}
void main()
{
DistFt ff;
DistDm mm,mm1;
clrscr();
cout<<"\nenter the first distance in feets and inches\n";
ff.getft();
cout<<"\nThe first distance in feets and inches\n";
ff.dispft();
cout<<"\nenter the second distance in meter and centimeter\n";
mm.getdm();
cout<<"\nThe second distance in meter and centimeter\n";
mm.dispdm();
mm1=mm.adddist(ff);
cout<<"\nResultant distance in meter and centimeter\n";
mm1.dispdm();
getch();
}

C++ - Create a class 'intarr' to overload [] operator such that it return the value of a
particular character in a string i.e str="ramansingh",str[3] return
#include<iostream.h>
#include<conio.h>
#include<string.h>
class intarr
{
int *arr;
int num;
public:
intarr()
{
arr= new int;
}
void getnum()
{
cin>>num;
}

int operator[](int index);


};
int intarr ::operator[](int ind)
{
return(arr[ind]);
}
void main()
{
int i,k;
intarr *a[10];
clrscr();
for(i=0;i<10;i++)
a[i].getnum();
cout<<"enter the index value";
cin>>k;
cout<<a[k];
getch();
}

C++ - Create a class complex and overload all the Arthimatic Operators
#include<iostream.h>
#include<conio.h>
class complex
{
int re,im;
public:
void getcomp();
void dispcomp();
complex operator+(complex &cc);
complex operator-(complex &cc);
complex operator/(complex &cc);
};
void complex:: getcomp()
{
cin>>re>>im;
}
void complex::dispcomp()
{
cout<<"\n";
cout<<re<<"+i "<<im;
cout<<"\n";
}
complex complex::operator + (complex &cc)
{
complex temp;
temp.re=re+cc.re;
temp.im=im+cc.im;
return temp;
}
complex complex::operator - (complex &cc)
{

complex temp;
temp.re=re-cc.re;
temp.im=im-cc.im;
return temp;
}
complex complex::operator / (complex &cc)
{
complex temp;
temp.re=((re*cc.re)+(im*cc.im))/((cc.re*cc.re)+(cc.im*cc.im));
temp.im=((cc.re*im)-(re*cc.im))/((cc.re*cc.re)+(cc.im*cc.im));
return temp;
}
void main()
{
complex c1,c2,c3;
cout<<"enter the first complex number";
c1.getcomp();
cout<<"enter the second complex number";
c2.getcomp();
cout<<"addition of two complex number is";
c3=c1+c2;
c3.dispcomp();
cout<<"substraction of two complex numbers is";
c3=c1-c2;
c3.dispcomp();
cout<<"division of two complex numbers is";
c3=c1/c2;
c3.dispcomp();
getch();
}

Create a class 'distance' with data-member(meter,centimeter) and memberfunctions to add and substract two distances
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class distance
{
int mtr;
int cm;
public:
void getdist();
void dispdist();
distance adddist(distance &dd);
distance subdist(distance &dd);
};
void main()
{
distance d1,d2,d3;
cout<<"enter the first distance";
d1.getdist();
cout<<"enter the second distance";

d2.getdist();
cout<<"addition of two distances is";
d3=d1.adddist(d2);
d3.dispdist();
cout<<"substraction of two distances is";
d3=d1.subdist(d2);
d3.dispdist();
getch();
}
void distance:: getdist()
{
cin>>mtr>>cm;
}
void distance::dispdist()
{
cout<<"\nDistance= "<<mtr<<"m "<<cm<<" cm";
}
distance distance :: subdist(distance &dd)
{
distance temp;
if(mtr>dd.mtr)
{
if(cm<dd.cm)
{
mtr=mtr-1;
cm=cm+100;
}
temp.mtr=mtr-dd.mtr;
temp.cm=cm-dd.cm;
return temp;
}
else
exit(0);
}
distance distance :: adddist(distance &dd)
{
int i;
distance temp;
temp.mtr=mtr+dd.mtr;
temp.cm=cm+dd.cm;
if(temp.cm>=100)
{ for(i=0;i<=(int)(temp.cm/100);i++)
{
temp.mtr=temp.mtr+1;
temp.cm=temp.cm-100;
}
}
return temp;
}

C++ - Create a class 'distance' with data-member(feets,inches) and friendfunctions to add and substract two distances
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class distance
{
float ft;
float in;
public:

friend
friend
friend
friend

void getdist(distance &dd);


void dispdist(distance &dd);
distance adddist(distance &dd1,distance &dd2);
distance subdist(distance &dd1,distance &dd2);

};
void main()
{
distance d1,d2,d3;
cout<<"enter the first distance";
getdist(d1);
cout<<"enter the second distance";
getdist(d2);
cout<<"addition of two distances is";
d3=adddist(d1,d2);
dispdist(d3);
cout<<"substraction of two distances is";
d3=subdist(d1,d2);
dispdist(d3);
getch();
}
void getdist(distance &dd)
{
cin>>dd.ft>>dd.in;
}
void dispdist(distance &dd)
{
cout<<"\nDistance= "<<dd.ft<<"\'-"<<dd.in<<"\" ";
}
distance subdist(distance &dd1,distance &dd2)
{
distance temp;
if(dd1.ft>dd2.ft)
{
if(dd1.in<dd2.in)
{
dd1.ft=dd1.ft-1;
dd1.in=dd1.in+12;
}
temp.ft=dd1.ft-dd2.ft;

temp.in=dd1.in-dd2.in;
return temp;
}
else
exit(0);
}
distance adddist(distance &dd1,distance &dd2)
{
int i;
distance temp;
temp.ft=dd1.ft+dd2.ft;
temp.in=dd1.in+dd2.in;
if(temp.in>=12)
{ for(i=0;i<=(int)(temp.in/12);i++)
{
temp.ft=temp.ft+1;
temp.in=temp.in-12;
}
}
return temp;
}

C++ - WAP to do all the posible operations on a String(s)

#include<iostream.h>
#include<conio.h>
#include<string.h>
class string1
{
char *name;
public:
string1();
string1(char s[]);
string1(string1 &ss);
void disp();
void concat(string1 &ss1);
int compare(string1 &ss1);
void copy(string1 &ss1);
int length1();
};
string1::string1()
{
name=new char[2];
strcpy(name,"\0");
}
string1 :: string1(char s[])
{
int l;
l=strlen(s);
name=new char[l+1];
strcpy(name,s);
}
string1 :: string1(string1 &ss)
{

int l;
l=strlen(ss.name);
name=new char[l+1];
strcpy(name,ss.name);
}
void string1:: disp()
{
cout<<"\nstring is="<<this->name;
}
int string1::length1()
{
int count=0;
for(;*(name+count)!='\0';)
count++;
return count;
}
int string1::compare(string1 &ss)
{
int l;
//l=strcmp(name,ss.name);
int l1,l2,m,i;
l1=this->length1();
l2=ss.length1();
if(l1<=l2)
m=l1;
else
m=l2;
for(i=0;i<=m;i++)
{
if(name[i]==ss.name[i])
continue;
else
{
l=name[i]-ss.name[i];
break;
}
}
return l;
}
void string1::concat(string1 &ss1)
{
int count=0;
for(;*(name+count)!='\0';count++);
for(;*ss1.name!='\0';ss1.name++,count++)
*(name+count)=*ss1.name;
*(name+count)='\0';
this->disp();
}
void string1:: copy(string1 &ss1)
{
int count=0;
for(;*ss1.name!='\0';count++,ss1.name++)
*(name+count)=*ss1.name;
//
*(name+count)='\0';
}

void main()
{
string1 s[3];
int i;
char nm[20],df[10];
clrscr();
// s[0]=string1();
cout<<"enter the string\n";
cin>>nm;
s[1]=string1(nm);
cout<<"enter the second string\n";
cin>>df;
s[2]=string1(df);
for(i=1;i<3;i++)
s[i].disp();
int l=s[1].length1();
cout<<"length ="<<l;
l=s[1].compare(s[2]);
cout<<"diff ="<<l;
s[1].concat(s[2]);
cout<<"enter the string\n";
cin>>df;
s[2]=string1(df);
s[1].copy(s[2]);
s[1].disp();
getch();
}

Write a C program.
The program should return the last non-zero digit of n! (i.e n factorial).
Sample input and output:
Input

: 7

Output : 2
Explanation : 7! 5040. Therefore the last non-zero digit in 5040 is 4.

Solution

#include<stdio.h>
long fact(int x)
{
int i;
long f=1;

if(x<0 || x>1000)
f=0;
else
for(i=1;i<=x;i++)
f=f*i;
return(f);
}
void main()
{
int a=12,n;
long b;
b=fact(a);
if(b>=1)
{
printf("\n\nNumber : ",a);
printf("\n\nFactorial : ",b);
while(1)
{
n=b%10;
if(n!=0)
break;
b=b/10;
}
printf("\n\nThe last non-zero : ",n);
}
else
printf("\nOverflow error..");
}

Write a program to print a triangle like the following: 1


1
1
1
1

1
11
111
1111

#include <stdio.h>

#include <conio.h>
void main()
{
int num,i,j;
clrscr();
printf("\nENTER THE NUMBER OF LINES: ");
23
scanf("%d",&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
{
printf("1 ");
}
printf("\n");
}
getch();
}

Write a program to print a triangle like the following: 1


12
123
1234
12345

#include <stdio.h>
#include <conio.h>
void main()
{
int num,i,j;
clrscr();
printf("\nENTER THE NUMBER OF LINES: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
getch();
}

Write a program to print a triangle like the following: 1


23
456
7 8 9 10

11 12 13 14

#include <stdio.h>
#include <conio.h>
void main()
{
int num,i,j,k=1;
clrscr();
printf("\nENTER THE NUMBER OF LINES: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",k);
k++;
}
printf("\n");
}
getch();
}

C++ Solved Example of Bank Class using INHERITANCE


(Exercise 8.1 of Balagurusamy's C++ Book)
Question:
Assume that a bank maintains two kinds of accounts for customers, one called as
savings account and the other as current account. The savings account provides
compound interest and withdrawal facilities but no cheque book facility. The current
account provides cheque book facility but no interest. Current account holders should
also maintain a minimum balance and if the balance falls below this level, a service
charge is imposed.
Create a class account that stores customer name, account number and type of account.
From this derive the classes cur_acct and sav_acct to make them more specific to their
requirements. Include necessary member functions in order to achieve the following
tasks:
a) Accept deposit from a customer and update the balance.
b) Display the balance.
c) Compute and deposit interest.
d) Permit withdrawal and update the balance.
e) Check for the minimum balance, impose penalty, necessary and update the balance.
f) Do not use any constructors. Use member functions to initialize the class members.
Solution:
#include<iostream.h>
#include<stdio.h>
#include<math.h>

#include<conio.h>
#include<string.h>
class account
{
protected:
char cname[20];
int accno;
char type;
int bal;
public:
account()
{ strcpy(cname," ");
accno=0;
type=' ';
bal=0;
}
void entacc()
{ cout<<"Enter cname";cin>>cname;
cout<<"Enter accno";cin>>accno;
fflush(stdin);
cout<<"Enter type"; cin>>type;
fflush(stdin);
cout<<"Enter bal";cin>>bal;
}
void dispacc()
{
cout<<"\n Customer Name "<<cname;
cout<<"\n Account Number "<<accno;
cout<<"\n Type "<<type;
cout<<"\n Balance "<<bal;
}
void deposit()
{
int amt;
cout<<"\n Enter the amount to deposit";
cin>>amt;
bal=bal+amt;
}
};
class sav_acct:public account
{
int inter;
public:
int comp_int()
{ int time1,rate1;
rate1=10;
cout<<"\n Enter time";cin>>time1;
inter=bal*pow(1+rate1/100.0,time1)-bal;
return inter;
}
void update_bal()
{
bal=bal+comp_int();
}
void withdrawal()
{
int amt;
cout<<"\n Enter amont to withdrawn";
cin>>amt;
if(bal>=amt)
bal=bal-amt;
else
cout<<"\n The amount cannot be withdrawn";

}
};
class cur_acct:public account
{ int chq_bk;
int penal;
public:
int min_bal()
{ int ret1=1;
if(bal<=500)
{
penal=50;
bal=bal-penal;
ret1=0;
}
else
{
cout<<"\n No penality imposed";
}
return ret1;
}
void withdrawal()
{
int amt;
cout<<"\n Enter the amount to withdrawn";
cin>>amt;
int k=min_bal();
if(k==1)
{ if(bal>=amt)
bal=bal-amt;
else
cout<<"\n The amount cannot be withdrawn";
}
}
};
void main()
{
cur_acct c1;
sav_acct s1;
c1.entacc();
c1.dispacc();
c1.deposit();
c1.dispacc();
c1.withdrawal();
c1.dispacc();
s1.entacc();
s1.dispacc();
s1.deposit();
s1.dispacc();
s1.withdrawal();
s1.dispacc();
}

C++ - Program to do all the operation on complex number using FRIEND


Function
Solution:
#include<iostream.h>
#include<conio.h>
class complex
{
float a;
float b;
public:
friend void getcomp(complex &cc);

};

friend
friend
friend
friend
friend

void dispcomp(complex &cc);


complex operator+(complex &cc1,complex &cc2);
complex subcomp(complex &cc1,complex &cc2);
complex mulcomp(complex &cc1,complex &cc2);
complex divcomp(complex &cc1,complex &cc2);

void main()
{
complex c1,c2,c3;
cout<<"enter the first complex number";
getcomp(c1);
cout<<"enter the second complex number";
getcomp(c2);
cout<<"addition of two complex number is";
c3=c1+2;
dispcomp(c3);
cout<<"substraction of two complex numbers is";
c3=subcomp(c1,c2);
dispcomp(c3);
cout<<"multiplication of two complex numbers is";
c3=mulcomp(c1,c2);
dispcomp(c3);
cout<<"division of two complex numbers is";
c3=divcomp(c1,c2);
dispcomp(c3);
getch();
}
void
{
}

getcomp(complex &cc)
cin>>cc.a>>cc.b;

void dispcomp(complex &cc)


{
cout<<"\n";
cout<<cc.a<<"+i "<<cc.b;
cout<<"\n";
}
complex operator+(complex &cc1,complex &cc2)
{
complex temp;
temp.a=cc1.a+cc2.a;
temp.b=cc1.b+cc2.b;
return temp;
}
complex subcomp(complex &cc1,complex &cc2)
{
complex temp;
temp.a=cc1.a-cc2.a;
temp.b=cc1.b-cc2.b;
return temp;
}
complex mulcomp(complex &cc1,complex &cc2)
{
complex temp;
temp.a=cc1.a*cc2.a-cc1.b*cc2.b;
temp.b=cc1.a*cc2.b+cc2.a*cc1.b;
return temp;
}

complex divcomp(complex &cc1,complex &cc2)


{
complex temp;
temp.a=(cc1.a*cc2.a+cc1.b*cc2.b)/(cc2.a*cc2.a+cc2.b*cc2.b);
temp.b=(cc2.a*cc1.b-cc1.a*cc2.b)/(cc2.a*cc2.a+cc2.b*cc2.b);
return temp;
}

C++ - Program for Books Details Entry


#include<iostream.h>
#include<conio.h>
#include<string.h>
class books
{
char auther[20];
char title[20];
float price;
char pub[10];
int stpos;
float c_price()
{
int p;
cout<<"enter the new price\n";
cin>>p;
return p;
}
public:
void putdetails();
void dispdetails();
int search_book(char a[],char t[]);
int find_stock();
void update_record()
{
price=c_price();
}
static int succtr;
static int unsucctr;
};
int books :: succtr;
int books :: unsucctr;
void books::putdetails()
{
cout<<"enter the auther's name\n";
cin>>auther;
cout<<"enter the book's name\n";
cin>>title;
cout<<"enter the price\n";
cin>>price;
cout<<"enter the publisher\n";
cin>>pub;
cout<<"enter the stock position\n";
cin>>stpos;
}

void books::dispdetails()
{
cout<<"\nauther's name";
cout<<auther;
cout<<"\nbook's name ";
cout<<title;
cout<<"\nprice ";
cout<<price;
cout<<"\npublisher ";
cout<<pub;
cout<<"\nstock position ";
cout<<stpos;
}
int books :: find_stock()
{
return stpos;
}
int books::search_book(char a[],char t[])
{
int k;
if((strcmp(auther,a)==0)&&(strcmp(title,t)==0))
{
cout<<"\n Book is Present";
int no_of_copies,avial;
cout<<"\n enter the no. of copies";
cin>>no_of_copies;
avial=find_stock();
if(avial>=no_of_copies)
{
float p=no_of_copies*price;
cout<<"total price of"<<"books"<<p;
stpos=stpos-no_of_copies;
k=1;
succtr++;
}
else
{
cout<<"not avialable copies";
k=1;
unsucctr++;
}
}
else
{
k=-1;
unsucctr++;
}
return k;
}
void main()
{
books b[3];
int i;
clrscr();
for(i=0;i<3;i++)
b[i].putdetails();
for(i=0;i<3;i++)
b[i].dispdetails();
char an[20],bt[20];
cout<<"\nenter auther name";

cin>>an;
cout<<"\nenter book title name";
cin>>bt;

int k=-1;
for(i=0;i<3;i++)
{
k=b[i].search_book(an,bt);
if(k==1)
{
break;
}
}
if(k==-1)
{
cout<<"books not present";
}
cout<<"\nenter the book number which has to be updated";
cin>>i;
b[i].update_record();
cout<<"successful trans"<<books::succtr;
cout<<"un succ "<<books::unsucctr;
getch();

// Write a C program to Create a FLOWER POT using C Graphics


#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
int gm,gd=DETECT;
int midx,midy;
char str[20];
int stangle=180,endangle=360,xradius=100,yradius=50;
initgraph(&gd,&gm,"c:\\borlandc\\bgi");
setfillstyle(HATCH_FILL,3);
midx = getmaxx()/2 ;
midy = getmaxy()/2+120 ;
ellipse(midx, midy, stangle, endangle, xradius, yradius+20);
stangle=120;endangle=180;
ellipse(midx,midy,stangle,endangle,xradius,yradius+150);
stangle=0;endangle=60;
ellipse(midx,midy,stangle,endangle,xradius,yradius+150);

arc(170,185,0,40,100);
arc(468,185,140,180,100);
line(248,120,391,120);
arc(200,120,0,50,100);
arc(440,120,130,180,100);

arc(241,75,50,90,40);
arc(292,75,90,130,40);
arc(108+241,75,50,90,40);
arc(108+292,75,90,130,40);
arc(267,65,53,127,40);
arc(267+108,65,53,127,40);
getch();
closegraph();
}
C Program-Link List-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 CONTINUE[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 LINK LIST ARE:\n");
while(disp!=NULL)
{
printf("%d ",disp->number);
disp=disp->next;
}
}
C Program-Link List-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 CONTINUE[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 count(node *first)
{
int i=0;
while(first!=NULL)
{
first=first->next;
i++;
}
printf("THERE ARE %d NODES IN THE LINK LIST",i);
}
C Program-Link List-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);
printf("\nWANT TO CONTINUE[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 LINK LIST 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 INSERT: ");
scanf("%d",&newnum);
newnode=(node *) malloc(sizeof(node));
newnode->num=newnum;
newnode->next=ins;
head=newnode;
}
C Program-Link List-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 *);
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 CONTINUE[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 LINK LIST 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 INSERT: ");
scanf("%d",&newnum);
newnode=(node *) malloc(sizeof(node));
newnode->num=newnum;
newnode->next=NULL;
ins->next=newnode;
}
C Program-Link List-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 CONTINUE[Y/N]: ");
conf=getche();
printf("\n");
if(conf=='n' || conf=='N')

else
{

list->next=NULL;
list->next=(node *) malloc(sizeof(node));
create(list->next);

}
}
void display(node *disp)
{
printf("\nTHE VALUES OF THE LINK LIST 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 INSERT: ");
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 IN THE LIST\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);
else
if(list->next->next==NULL)
return(NULL);
else
find(list->next,key);
}
C Program-Link List-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 CONTINUE[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 LINK LIST 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 INSERT: ");
scanf("%d",&newnum);
printf("\nENTER THE VALUE OF KEY NUMBER: ");
scanf("%d",&key);
if(ins->num==key)
{
newnode=(node *) malloc(sizeof(node));

}
else
{

newnode->num=newnum;
newnode->next=ins;
head=newnode;

n1=find(ins,key);
if(n1==NULL)
printf("\nKEY VALUE NOT FOUND IN THE LIST\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);
else
if(list->next->next==NULL)
return(NULL);
else
find(list->next,key);
}
C Program-Link List-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 CONTINUE[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 LINK LIST ARE:\n");
while(disp!=NULL)
{
printf("%d ",disp->num);
disp=disp->next;
}
}
void delet(node *del)
{
head=del->next;
free(del);
}
C Program-Link List- 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)
{
char conf='y';
int i;
printf("\nENTER A NUMBER: ");

scanf("%d",&list->num);
printf("\nWANT TO CONTINUE[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 LINK LIST 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;
}
}
C Program-Link List- 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;
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 CONTINUE[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 LINK LIST ARE:\n");
while(disp!=NULL)
{
printf("%d ",disp->num);
disp=disp->next;
}
}
void delet(node *del)
{
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 IN THE LIST\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);

}
C Program-Link List- 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 *);
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 CONTINUE[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-PREVIOUS ADDRESS-VALUE-NEXT ADDRESS OF THE LINK LIST ARE:\n");

while(disp!=NULL)
{
printf("%u-%u-%u-%u\n",disp,disp->prev,disp->num,disp->next);
disp=disp->next;
}

}
C Program-Link List- Write a program to create a circular 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';
int i;
printf("\nENTER A NUMBER: ");
scanf("%d",&list->number);
printf("\nWANT TO CONTINUE[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 LINK LIST 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);
}

C Program-File Handling- Write a program to read a text file print it on the screen.
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *fp;
char s;
clrscr();
fp=fopen("text.txt","r");
if(fp==NULL)
{
printf("\nCAN NOT OPEN FILE");
getch();
exit();
}
do
{
s=getc(fp);
printf("%c",s);
}
while(s!=EOF);
fclose(fp);
getch();
}
C Program-File Handling- 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()
{
FILE *fp;
char ch[50];
clrscr();
fp=fopen("output.txt","w");
printf("\nENTER YOUR LINES: (PRESS ^Z TO EXIT)\n");
while(strlen(gets(ch))!=0)
{
fputs(ch,fp);
fputs("\n",fp);
}
fclose(fp);
getch();
}
C Program-File Handling- 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[])
{
FILE *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("\nFILE COPIED\n");

C Program-File Handling- 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()
{
FILE *fp;
char str[100],s;
int word=0,vow=0;
clrscr();
fp=fopen("text.txt","r");
if(fp==NULL)
{
printf("\nCAN NOT OPEN FILE");
getch();
exit();
}
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=='I' || s=='O'|| s=='U')
vow++;
}
while(s!=EOF);
printf("\nNUMBER OF WORDS IN THE STRING ARE %d",word);
printf("\nNUMBER OS VOWELS IN THE STRING ARE %d",vow);
fclose(fp);
getch();
}
Write a program to print a triangle like the following: -

1
11
111
1111
#include <stdio.h>
#include <conio.h>
void main()
{

1
1
1
1
1

1
11
111
1111

int num,i,j,k,s=40;
clrscr();
printf("\nENTER THE NUMBER OF LINES: ");
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");
s--;
}
getch();
}

Write a program to print a triangle (Floyds Triangle) like the following: -

1
12
123
1234

#include <stdio.h>
#include <conio.h>
void main()
{
int num,i,j,k,s=40;
clrscr();
printf("\nENTER THE NUMBER OF LINES: ");
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();
}

1
2
3
4
5

1
21
321
4321

Ques. C Language - Structures -Write a program to create a book structure


having name, author, page and price
Solution:
#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 PRICE OF THE BOOK: ");
scanf("%f",&b.price);
printf("\nNAME OF THE BOOK: %s",b.name);
printf("\nNAME OF THE AUTHOR: %s",b.auth);
printf("\nNUMBER OF PAGES: %d",b.page);
printf("\nPRICE OF THE BOOK: %0.2f",b.price);
getch();
}

Ques. C Language - Pointers - Write a program to give examples & (Address


Of) and * (Value At Address) operator
Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=3,*j;
clrscr();
j=&i;
printf("\nVALUE OF i
printf("\nVALUE OF i
printf("\nADDRESS OF
printf("\nVALUE OF j
printf("\nADDRESS OF
printf("\nADDRESS OF
getch();
}

IS [i]: %d",i);
IS [*(&i)]: %d",*(&i));
i IS [&i]: %u",&i);
IS [*(&j)]: %u",*(&j));
j IS [&j]: %u",&j);
i IS [j]: %u",j);

Ques. C Language - Pointers - Write a program to swap the address of two


variables
Solution:
#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 SWAPING ARE %d %d",a,b);
swap(&a,&b);
printf("\nVALUES OF a AND b AFTER SWAPING ARE %d %d",a,b);
getch();
}
void swap(x,y)
int *x,*y;
{
int t;
t=*x;
*x=*y;
*y=t;
}

Ques. C Language - Pointers - Write a program to compute the average of n


given numbers using pointers
Solution:
#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 ALLOCATION 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 IS %0.2f",avg);
getch();
}

Ques. C Language - Pointers - Write a program to sort n given numbers using


pointers
Hint: - Use Bubble Sort Algorithm

Solution:
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
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 ALLOCATION 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();
}

Ques. C Language-Pointers-Write a program to concatenate two given string


without using the library function strcat()
Solution:
#include <stdio.h>
#include <conio.h>
void strconc(char *,char *);
char *s3;
void main()
{
char *str1,*str2;
clrscr();
printf("\nENTER THE FIRST STRING: ");
gets(str1);
printf("\nENTER THE SECOND STRING: ");

gets(str2);
strconc(str1,str2);
printf("\nTHE NEW STRING IS: %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;
}

Q. C Language - Pointers - Write a program to reverse the order of each word


of the string using pointers

Example: - INPUT: ALL Free Stuff


OUTPUT: Stuff Free ALL
#include <stdio.h>
#include <conio.h>
void main()
{
char *s;
int len,i,j,sp=0,start,end;
clrscr();
printf("\nENTER A STRING: ");
gets(s);
printf("\nTHE STRING AFTER CHANGING 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();
}

Q. C Language - Pointers - 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 CITY-%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("\nCITIES IN SORTED ORDER:\n");
for(i=0;i<items;i++)
printf("\n%s",str[i]);
getch();
}

Ques. C Language - Pointers - Write a program to find out whether a given


string is palindrome or not using pointers
Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
char *a;
int i,len,flag=0;
clrscr();
printf("\nENTER A STRING: ");
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 STRING IS PALINDROM");

else
}

printf("\nTHE STRING IS NOT PALINDROM");


getch();

Ques. C Language - Pointers - Write a program to reverse a string using


pointers
Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
char *s;
int len,i;
clrscr();
printf("\nENTER A STRING: ");
gets(s);
len=strlen(s);
printf("\nTHE REVERSE OF THE STRING IS:");
for(i=len;i>=0;i--)
printf("%c",*(s+i));
getch();
}

Ques. C Language - Pointers - Write a program to search a given key number


within n given numbers using pointers. If the key number exists print found
otherwise print not found
Solution:
#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 ALLOCATION 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 IS FOUND",num);
else
printf("\nTHE NUMBER %d DOES NOT EXIST",num);
getch();
}

Ques. C Language - Pointers - Write a program to find the maximum number


within n given numbers using pointers
Solution:
#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 ALLOCATION 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++)
{
if(*(p+i)>h)
h=*(p+i);
}
printf("\nTHE HIGHEST NUMBER IS %d",h);
getch();
}

DOEACC Examination Questions & Answers


July 2001 (Ques 2)

Question : 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,
Input: Base = 2, Number = 10 (in decimal)

Output: Output = 1010


Solution :
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long num;
int base,n[25],c=0,i;
clrscr();
printf("\nENTER A DECIMAL NUMBER: ");
scanf("%lu",&num);
printf("\nENTER THE BASE: ");
scanf("%d",&base);
printf("\n%lu TO THE BASE %d IS ",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();
}

DOEACC Examination Questions & Answers


July 2001 (Ques 1)

Question : 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
Input: THE PRICE OF THIS ITEM IS $100.
Output: uifBBqsjdfBBpgBBuijtBBjufnBBjtBB$100.
Solution :

#include <stdio.h>
#include <conio.h>
void main(int argc,char *argv[])
{
int i;
char ch;
FILE *sfp,*tfp;
clrscr();
if(argc<3)
printf("\nPARAMETER MISSING");
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);
}
}
}

DOEACC Examination Questions & Answers


January 2002 (Ques 1)

Question: 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
"?". Solution:

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

int count(FILE *);


void main()
{
int w;
FILE *fp;
clrscr();
fp=fopen("text.txt","r");
if(fp==NULL)
{
printf("\nTHE FILE DOES NOT EXIST");
getch();
exit();
}
w=count(fp);
printf("\nTHERE ARE %d WORDS IN THE FILE",w);
getch();
}
int count(FILE *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;
}

DOEACC Examination Questions & Answers


January 2002 (Ques 2)

Question: 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. Solution:

#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;
};

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;
}
if(x[i].s4>hm4)
{
hm4=x[i].s4;
h4=i;
}
if(x[i].s5>hm5)
{
hm5=x[i].s5;
h5=i;
}
}
clrscr();
printf("\nHIGHEST MARK OBTAINED BY STUDENTS IN 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);

Q. C Language - Searching - 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 EXISTS IN THE ARRAY",key);
else
printf("\nTHE NUMBER %d DOES NOT EXIST IN THE ARRAY",key);
getch();
}

Q. C Language - Searching - 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()
{
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 IS: ");
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 EXISTS",key);
else
printf("\nTHE NUMBER %d DOES NOT EXIST 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;
break;
}
else
{
if(k>x[mid])
low=mid+1;
else
high=mid-1;
}
}
return res;
}

Q. C Language - Sorting - 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 IS:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}

Q. C Language - Sorting - 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();
printf("\nENTER THE SIZE 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 SORTING: \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 SORTING: \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;

Q. C Language - Using Recursion - Write a recursive function to print the


factorial of a number
#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);
f=fact(n);
printf("\nFACTORIAL OF %d IS %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);
}

Q. C Language - Using Recursion - 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 FIBONNACI SERIES UPTO %d NUMBERS IS:\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);
else
if(x==1)
return(1);
else
{
res=fib(x-1)+fib(x-2);
return(res);
}
}
DOEACC Examination Questions & Answers
July 2002
Question: Write C function in each case that will work exactly as following

without using any built-in string manipulation function.


a.

strcpy()

b.

stccmp()

Solution:

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];
printf("\na=%s\nb=%s",x,y);
}
Solution : 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);
}

DOEACC Examination Questions & Answers


January 2002 (Ques 4)

Question: Write a C function to sort an array using bubble sort algorithm.


Solution:
#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 IN THE ARRAY BEFORE SORTING:\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
sort(a);
printf("\n\nTHE NUMBERS IN THE ARRAY AFTER SORTING:\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;
}
}
}
}

DOEACC Examination Questions & Answers


January 2002 (Ques 3)

Question: 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}. Solution:

#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 SIZE 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 UNION OF THESE TWO ARRAYS IS:\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++;
}
}
}

DOEACC Questions - July 2003

9)
a) Write a program in C to find the value of xn 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 IS %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;
clrscr();
result=lardif(a);
printf("\nTHE LARGEST DIFFERENCE IS %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;
}

DOEACC Questions - July 2003

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 threeelement 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;
*(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;
}

DOEACC Questions - July 2003

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 employees salary.
b) Print the contents of created list.
#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 CONTINUE[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 LINK LIST IN SORTED ORDER ARE:\n");
while(disp!=NULL)
{
printf("%d ",disp->salary);
disp=disp->next;
}
}

DOEACC Questions - July 2003


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.

Solution: Part 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 IS: \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();
}

Solution Part 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 IS %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;
}

DOEACC Questions - July 2003


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.

Solution: Part 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 IS: \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();
}

Solution Part 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 IS %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;
}

Q. C Language - Using Recursion - 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 IS ",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);
}
}

Q. C Language - Using Recursion - Write a recursive function to evaluate xy


#include <stdio.h>
#include <conio.h>
pow(int,int);
void main()
{
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 IS: %d",pow);
getch();
}

power(int a,int b)
{
int prod;
if(b==0)
return(1);
else
{
prod=a*power(a,b-1);
return(prod);
}
}

Q. C Language - Recursion - 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 DISKS: ");
scanf("%d",&num);
printf("\nTOWER OF HANOI FOR %d NUMBER OF DISKS:\n", num);
hanoi('A','B','C',num);
getch();
}
void hanoi(char from,char to,char other,int n)
{
if(n<=0)
printf("\nILLEGAL NUMBER OF DISKS");
if(n==1)
printf("\nMOVE DISK FROM %c TO %c",from,other);
if(n>1)
{
hanoi(from,other,to,n-1);
hanoi(from,to,other,1);
hanoi(to,from,other,n-1);
}
}
Q. 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.
Solution:

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

int i,n;
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 BASIC: ");
scanf("%d",&basic);
if(basic<9000)
tax=basic*20/100;
else
tax=basic*25/100;
printf("\nTHE AMOUNT OF TAX IS %0.2f\n",tax);
}
Q. Write a program to add three given numbers using function.
Solution:

#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 IS %d",a,b,c,d);
getch();
}
sum(int x,int y,int z)
{
int temp;
temp=x+y+z;
return(temp);
}
Q. Write a program to print Hello World in the main function and Welcome To C in another
function.
Solution:

#include <stdio.h>

#include <conio.h>
void message(void);
main()
{
clrscr();
printf("\nHELLO WORLD");
message();
getch();
}
void message()
{
printf("\nWELCOME TO C");
}
Q. Write a program to compute the root of a number using function.
Solution:

#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 IS %lu",x,res);
getch();
}
unsigned long root(int a)
{
unsigned long b;
b=a*a;
return(b);
}

Q. Write a program to evaluate ab using function.


Solution:

#include <stdio.h>
#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 IS %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);
}
Q. Write a program to print the following series using function.
9 25 57 121 249 505 1017 2041
Solution:

#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++)
prod=prod*a;
return(prod);
}
Q. Write a program to check a number is prime or not using function.
Solution:

#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 IS A PRIME NUMBER",num);
else
printf("\n%d IS NOT A PRIME 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;
}
Q. Write a program to find out the maximum number in an array using function.
Solution:
#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("\nMAXIMUM NUMBER IS %d",m);
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);
}
Q. Write a program to evaluate 1/Factorial of 1 + 2/Factorial of 2 + n/Factorial of n using function.

Solution:
#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 SUMMATION IS: %0.2f",sum);
getch();
}
fact(float x)
{
if(x==1)
return(1);
else
return(x*fact(x-1));
}
Q. Write a program to convert a binary number to its equivalent decimal number
Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long num;
int digit,i,pos=0,pow=1,dec=0;
clrscr();
printf("\nENTER A BINARY NUMBER: ");
scanf("%lu",&num);
printf("\nDECIMAL EQUIVALANT OF %lu IS ",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();

}
Q. Write a program to convert a decimal number to its equivalent hexadecimal number
Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long dec;
int a[25],c=0,i;
clrscr();
printf("\nENTER A DECIMAL NUMBER: ");
scanf("%lu",&dec);
printf("\n%lu IN HEXADECIMAL FORMAT: ",dec);
while(dec>0)
{
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();
}
Q. Write a program to convert a decimal number to its equivalent octal number.
Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long dec;
int a[25],c=0,i;
clrscr();
printf("\nENTER A DECIMAL NUMBER: ");
scanf("%lu",&dec);
printf("\n%lu IN BINARY FORMAT: ",dec);
while(dec>0)
{
a[c]=dec%2;
dec=dec/2;
c++;

}
for(i=c-1;i>=0;i--)
printf("%d",a[i]);
getch();
}
Q. Write a program to convert a decimal number to its equivalent binary
number.
Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long dec;
int a[25],c=0,i;
clrscr();
printf("\nENTER A DECIMAL NUMBER: ");
scanf("%lu",&dec);
printf("\n%lu IN BINARY FORMAT: ",dec);
while(dec>0)
{
a[c]=dec%2;
dec=dec/2;
c++;
}
for(i=c-1;i>=0;i--)
printf("%d",a[i]);
getch();
}

Q. Write a program to convert a decimal number to its equivalent binary


number.
Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long dec;
int a[25],c=0,i;
clrscr();
printf("\nENTER A DECIMAL NUMBER: ");
scanf("%lu",&dec);
printf("\n%lu IN BINARY FORMAT: ",dec);
while(dec>0)
{

a[c]=dec%2;
dec=dec/2;
c++;
}
for(i=c-1;i>=0;i--)
printf("%d",a[i]);
getch();
}
String or Array of Characters
Q. Write a program which will accept Name, Age, Sex and Qualification and display the same.

Solution:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int i,j;
char label[5][20]={"NAME","AGE","SEX","QUALIFICATION"},a[5][10];
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 IS YOUR %s\n",a[i],label[i]);
getch();
}

String or Array of Characters


Q. Write a program to find out the length of a given string without using the library function strlen()

Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
char str[50];
int len;
clrscr();
printf("\nENTER A STRING: ");
gets(str);
for(len=0;str[len]!=NULL;len++);
printf("\nTHE LENGTH OF THE STRING IS %d",len);
getch();
}

String or Array of Characters


Q. Write a program to print the reverse of a given string

Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
char ch[100];
int i,len;
clrscr();
printf("\nENTER A STRING: ");
gets(ch);
len=strlen(ch);
printf("\nTHE STRING IN THE REVERSE ORDER: ");
for(i=len-1;i>=0;i--)
printf("%c",ch[i]);
getch();
}

String or Array of Characters


Q. 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.


Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
char a[100];
int i,len,flag=0;
clrscr();
printf("\nENTER A STRING: ");
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 STRING IS PALINDROM");
else
printf("\nTHE STRING IS NOT PALINDROM");
getch();
}

String or Array of Characters

Q. 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.


Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
char a[100];
int len,i,sp=0;
clrscr();
printf("\nENTER A STRING: ");
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 INCLUDING SPACES: %d",len);
printf("\nNUMBER OF WORDS EXCLUDING SPACES: %d",len-sp);
getch();
}

String or Array of Characters


Q. Write a program to count the number of vowels in a given string.

Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
char a[100];
int len,i,vow=0;
clrscr();
printf("\nENTER A STRING: ");
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]=='I' || a[i]=='o' || a[i]=='O' || a[i]=='u' || a[i]=='U')
vow=vow+1;
}
printf("\nTHERE ARE %d VOWELS IN THE STRING",vow);
getch();
}

String or Array of Characters


Q. Write a program to count the number of words in a given string. Two words are separated by one or more

blank spaces.

Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
char a[100];
int len,i,word=1;
clrscr();
printf("\nENTER A STRING: ");
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 IN THE STRING",word);
getch();
}

String or Array of Characters


Q. Write a program to print a given string in lower case.

Solution:
#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();
}

String or Array of Characters

Q. Write a program to print a given string in upper case.

Solution:

#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();
}
Q. Write a program to print the string in first case

Solution:

#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();
}
Example 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 MATRIX:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nTHE VALUES OF THE MATRIX ARE:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%5d",a[i][j]);
printf("\n");
}
getch();
}

Example 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 MATRIX A:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nENTER VALUES FOR MATRIX 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 MATRIX C ARE:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%5d",c[i][j]);
printf("\n");
}

getch();

Example 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 MATRIX-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 MATRIX-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 MATRIX-A\n");
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\nVALUES OF MATRIX-B\n");
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
printf("%d ",b[i][j]);

}
printf("\n");

}
printf("\nVALUES OF MATRIX-C\n");
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
getch();

Example 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 MATRIX:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nTHE VALUES OF THE MATRIX 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 SUMMATION OF THE DIAGONAL ELEMENTS OF THE MATRIX IS %d",trace);
getch();
}

Example 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;
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();

Example 2: Write a program to create an array of students 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("\nINPUT AGES ARE: ");
for(i=0;i<10;i++)
printf("%d ",age[i]);
avg=sum/10;
printf("\nTHE AVERAGE AGE IS: %0.2f",avg);
getch();
}
Example 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++)
{
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("\nHIGHEST VALUE IS %d",h);
printf("\nLOWEST VALUE IS %d",l);

getch();

Example 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 INSERT: ");
scanf("%d",&val);
printf("\nENTER POSITION TO INSERT: ");
scanf("%d",&pos);
j++;
for(k=j;k>=pos;k--)
{
a[k]=a[k-1];
}
a[--pos]=val;
printf("\nTHE ARRAY IS: ");
for(i=0;i<j;i++)
{
printf("\n%d",a[i]);
}
getch();
}
Example 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("\nVARIANCE = %0.2f",var);
printf("\nSTANDARD DAVIATION = %0.2f",sd);
getch();
}
Note: - To use the sqrt() function <math.h> header file must be included.

Example 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()
{
int g[n],i,j;
clrscr();
for(i=0;i<n;i++)
{
printf("\nENTER HOW MANY EMPLOYEES ARE IN 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();
}

Write a program to print the prime numbers within a given number.

#include <stdio.h>
#include <conio.h>
main()
{
int n,i,j,c;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
printf("\nPRIME NUMBERS WITHIN %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();
}
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 IS A PRIME NUMBER",n);
else
printf("\n%d IS NOT A PRIME NUMBER",n);
getch();
}
Alternative Method
#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 IS A PRIME NUMBER",n);
else
printf("\n%d IS NOT A PRIME NUMBER",n);
getch();
}
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 IS A PALINDROM NUMBER",temp);
else
printf("\n%lu IS NOT A PALINDROM NUMBER",temp);
getch();
}
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 IS AN AMSTORNG NUMBER",num1);
else
printf("\n%d IS NOT AN AMSTRONG NUMBER",num1);
getch();
}

For Loop Example 1: Write a program to print the odd numbers within a given number
Solution:

#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();
}
For Loop Example 2: Write a program to print the even numbers within a given number.
Solution:
#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);
}
getch();
}
For Loop Example 3: Write a program to add the odd numbers within a given number.
Solution:
#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 IS %lu",n,sum);
getch();
}
For Loop Example 4 : Write a program to add the even numbers within a given number
Solution:
#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 IS %lu",n,sum);
getch();
}

For Loop Example 5 : Write a program to print the multiplication table of a given number
Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
int n,i;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
printf("\nMULTIPLICATION TABLE OF %d\n",n);

for(i=1;i<=10;i++)
{
printf("\n%2d x %2d = %3d",n,i,n*i);
}
getch();
}
For Loop Example 6 : Write a program to print the ASCII chart within a given number
Solution:
#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();
}

For Loop Example 7 : Write a program to evaluate xn


Solution:

#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++)
{
res=res*num;
}
printf("\nTHE %d POWER OF %d IS %lu",power,temp,res);

getch();
}
For Loop Example 8 : Write a program to evaluate 1/3 + 2/5 + 3/7 . + n/((n*2) + 1).
Solution:
#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 SUMMATION IS: %0.2f",sum);
getch();
}
For Loop Example 9 : Write a program to compute the factorial of a given number.
Solution:
#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("\nFACTORIAL OF %d IS %lu",n,fact);
getch();
}
For Loop Example 10 : Write program to print the fibonacci series up to a given
number
Solution:
#include <stdio.h>

#include <conio.h>
void main()
{
int num,i;
unsigned long n1=0,n2=1,s;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
printf("\nFIBONACCI SERIES UPTO %d NUMBERS IS: \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();
}

For Loop Example 11 : Write a program to evaluate 1/1 Factorial + 2/2


Factorial + n/n Factorial
Solution:

#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 SUMMATION IS: %0.2f",sum);
getch();
}

Question: Write a program to print the reverse of a given number.


Example: - Reverse of 2565 is 5652.

Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long num,rev;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%lu",&num);
printf("\nTHE REVERSE OF THE NUMBER %lu IS ",num);
do
{
rev=num%10;
num=num/10;
printf("%lu",rev);
}
while(num!=0);
getch();
}

Question: Write a program to print the sum of digit of a given number.


Solution:
#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 DIGITS IS %lu",sum);
getch();
}

Control Statements (Loop Control Statements)


While Loop
Example 2: Write a program to ask the user to enter a series of marks of a
student. If 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();
}

Control Statements (Loop Control Statements)


While Loop
Example 1: 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')
{
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
printf("\nITS SQUARE IS: %d\n",num*num);
printf("\nDO YOU WANT TO CONTINUE [Y/N]: ");
ch=getche();
}
getch();
}

Control Statements (Conditional

Control Statements)

Switch Case

Example 1: Write a program to ask the user to enter a character. If 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 Is a Or A");
break;
case 'b':
case 'B':
printf("\nCharacter Is b Or B");
break;
case 'c':
case 'C':
printf("\nCharacter Is c or C");
break;
default:
printf("\nCharacter Is Other Than a,b,c,A,B,C");
}
getch();
}
Control Statements (Conditional

Control Statements)

Switch Case

Example 2:

Write a program to ask the user to enter a number. If 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 IS ONE");
break;
case 2:

printf("\nNUMBER IS TWO");
break;
case 3:
printf("\nNUMBER IS THREE");
break;
default:
printf("\nNUMBER IS OTHER THAN ONE, TWO OR THREE");
}
getch();
}

Control Statements (Conditional

Control Statements)

If Conditions

Example 8: Write a program to check whether a given year is leap year or not.
#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 IS A LEAP YEAR",year);
else
printf("\n%d IS NOT A LEAP YEAR",year);
getch();
}

Control Statements (Loop Control Statements)


While Loop
Example 3: Write a program to print natural numbers up to a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1,n;
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();

}
Control Statements (Conditional

Control Statements)

If Conditions

Example 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

Fai

#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("\nFIRST DIVISION");
else
{
if(per>=50)
printf("\nSECOND DIVISION");
else
{
if(per>=40)
printf("\nTHIRD DIVISION");
else
printf("\nFAIL");
}
}
getch();
}
Control Statements (Conditional

Control Statements)

If Conditions

Example 6: Write a program to find out the gross amount from the given basic
pay.
#include <stdio.h>
#include <conio.h>
void main()
{
float basic,da,hra,gross;
clrscr();
printf("\nENTER BASIC PAY: ");
scanf("%f",&basic);
if(basic>=8000)
{
da=basic*20/100;
hra=basic*25/100;
}
else
{
da=basic*15/100;
hra=basic*20/100;
}
gross=basic+da+hra;
printf("\nGROSS AMOUNT: %0.2f",gross);
getch();
}

Gross = Basic + DA + HRA

DA & HRA can be calculated as follows: -

If 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.
Control Statements (Conditional

Control Statements)

If Conditions

Example 5: Write a program to find out a person is insured or not. The person
is insured if following conditions are satisfied: In 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 IS INSURED");
else

printf("\nTHE EMPLOYEE IS NOT INSURED");


getch();
}

i) If the person is married.


ii) If the person is unmarried, male & above 30 years of age.
iii) If the person is unmarried, female & above 25 years of age.
Control Statements (Conditional

Control Statements)

If Conditions

Example 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 IS AN EVEN NUMBER");
else
printf("\nTHE NUMBER IS AN ODD NUMBER");
getch();
}

Control Statements (Conditional

Control Statements)

If Conditions

Example 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 IS %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 IS %d",a);
if(b<c && b<a)
printf("\nLOWEST NUMBER IS %d",b);
if(c<a && c<b)
printf("\nLOWEST NUMBER IS %d",c);
getch();
Control Statements (Conditional

Control Statements)

If Conditions

Example 2: Find out the highest number from three given numbers.
#include <stdio.h>
#include <conio.h>
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("\nHIGHEST NUMBER IS %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("\nHIGHEST NUMBER IS %d",a);
if(b>c && b>a)
printf("\nHIGHEST NUMBER IS %d",b);
if(c>a && c>b)
printf("\nHIGHEST NUMBER IS %d",c);
getch();
}

Control Statements (Conditional

Control Statements)

If Conditions

Example 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 QUANTITY: ");
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();
}

Question: Write a program to swap the values of two variables without using a
third variable.

Solution :

#include <stdio.h>
#include <conio.h>
void main()
{
int a,b;
clrscr();
printf("\nENTER TWO NUMBERS FOR a AND b:\n");
scanf("%d %d",&a,&b);
printf("\nBEFORE SWAPING THE VALUE OF a=%d AND b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAFTER SWAPING THE VALUE OF a=%d AND b=%d",a,b);
getch();
}

Question: Write a program to swap the values of two variables.

Solution :

#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 SWAPING THE VALUE OF a=%d AND b=%d",a,b);
c=a;
a=b;
b=c;
printf("\nAFTER SWAPING THE VALUE OF a=%d AND b=%d",a,b);
getch();
}

Question: Write a program to compute the area of a circle from the given
radius.Solution :
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 RADIUS OF THE CIRCLE: ");
scanf("%f",&rad);
area=pi*rad*rad;
printf("\nTHE AREA OF THE CIRCLE IS: %0.2f",area);
getch();
}

Question: Write a program to compute simple interest and compound interest


from the given principal, time period and interest rate.
Solution :

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

int p,t;
float si,ci,r,i;
clrscr( );
printf("\nENTER PRINCIPAL: ");
scanf("%d",&p);
printf("\nENTER TIME PERIOD: ");
scanf("%d",&t);
printf("\nENTER RATE OF INTEREST: ");
scanf("%f",&r);
si=(p*t*r)/100;
i=r/100;
ci=p*pow((1+i),t);
printf("\nSIMPLE INTEREST: %0.2f",si);
printf("\n\nCOMPOUND INTEREST: %0.2f",ci);
getch();

Note: - To use the pow() function <math.h> header file must be included.

Question: Write a program to compute the seconds from a given age.


Solution :
#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 IN SECONDS: %lu",sec);
getch();
}

Question: Write a program to compute the average of three given numbers.


Solution :
#include <stdio.h>
#include <conio.h>
void main()
{
int n1,n2,n3;
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();

Question: Write a program to compute the addition, subtraction, product, quotient and remainder of two given
numbers.

Solution
#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("\nADDITION OF THE NUMBERS: %d",add);
printf("\nSUBSTRACTION OF THE NUMBERS: %d",sub);
printf("\nPRODUCTION OF THE NUMBERS: %d",prod);
printf("\nQUOTIENT OF THE NUMBERS: %d",quot);
printf("\nREMAINDER OF THE NUMBERS: %d",remain);
getch();
}

Write a program to print "HELLO WORLD".


#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf("\nHELLO WORLD");
getch();
}

Das könnte Ihnen auch gefallen