C, C++,data Structure in C & C Code
C, C++,data Structure in C & C 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 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;
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;
}
{ 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');
}
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);
}
#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>
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();
}
# 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();
}
{
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();
}
# 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();
}
{
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:
};
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();
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;
}
#include<conio.h>
#include<process.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<dos.h>
class student
{
private:
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');
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();
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);
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);
}
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);
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;
}
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 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;
}
#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..");
}
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();
}
#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();
}
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();
}
#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();
}
};
friend
friend
friend
friend
friend
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 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();
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);
}
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);
}
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);
}
}
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();
}
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
#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();
}
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);
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();
}
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;
}
#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();
}
else
}
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();
}
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);
}
}
}
#include <stdio.h>
#include <conio.h>
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;
};
#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();
}
#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;
}
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();
}
++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;
#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
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);
}
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;
}
}
}
}
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++;
}
}
}
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();
}
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();
}
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;
}
}
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;
}
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;
}
power(int a,int b)
{
int prod;
if(b==0)
return(1);
else
{
prod=a*power(a,b-1);
return(prod);
}
}
#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);
}
#include <stdio.h>
#include <conio.h>
unsigned long power(int,int);
void main()
{
int num,pow;
unsigned long res;
clrscr();
#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();
}
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();
}
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();
}
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();
}
Q. Write a program to count the number of words including spaces and excluding spaces in a given string. Two
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();
}
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();
}
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();
}
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
getch();
Example 3: Write a program to create two matrixes. Multiply the values of the two matrixes and
}
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
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
#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();
}
#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();
}
#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();
}
#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();
}
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();
}
#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)
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)
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 (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();
}
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
Control Statements)
If Conditions
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)
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.
#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();
}
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: -
#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.
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();
}