ALL PRAISE "ROBUSTA"
===========================================================================
===========================================================================
1.
even digits. For example the number 21345 has two even
numbers 2 and 4
and three odd numbers 1,3 and 5. Also
print the digits and their positions.
#include<stdio.h>
int oddeven(int n)
{
if(n%2==0)
return 0;
else
return 1;
}
void main()
{
int earray[10],oarray[10];
int x;
printf("Enter number");
scanf("%d",&x);
int i;
int countere=0;
int countero=0;
while(x!=0)
{
i=x%10;
if(oddeven(i)==0)
{
earray[countere]=i;
countere++;
}
else
{
oarray[countero]=i;
countero++;
}
x=(int)x/10;
}
printf("%d number of even digits\n%d
number of odd digits\n",countere,countero);
printf("EVEN DIGITS \n");
for(i=0;i<countere;i++)
printf("%d \n",earray[i]);
printf("ODD DIGITS \n");
for(i=0;i<countero;i++)
printf("%d \n",oarray[i]);
}
2.
Write a program to generate twin prime numbers between
1 and 100.(If the difference between successive prime numbers is 2 they are
known as twin prime numbers. For example 3 and 5 are twin prime numbers).
#include<stdio.h>
#include<stdlib.h>
int
main()
{
printf("Required twin prime numbers
are: \n");
int
i,j,temp;
for(i=1;i<=100;i++)
{
int c=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
c++;
}
if(c==0)
{
if((i-temp)==2)
printf("%d %d\n",temp,i);
temp=i;
}
}
}
OUTPUT:
3.
Find all the odd numbers between m & n (take m and
n from user) and find the sum of their cubes. Also find all the even numbers
between m & n and find the sum of their squares. (Check whether m and n are
3 digit numbers and the difference between them should be >100).
#include<stdio.h>
#include<math.h>
int oddeven(int n)
{
if(n%2==0)
return 0;
else
return 1;
}
void main()
{
int m,n,i;
printf("Enter m and n \n");
scanf("%d %d",&m,&n);
int m1=m;
int n1=n;
int counter1=0;
int counter2=0;
while(m1!=0)
{
counter1++;
m1=(int)m1/10;
}
while(n1!=0)
{
counter2++;
n1=(int)n1/10;
}
if(counter1==3&&counter2==3&&abs(m-n)>=100)
printf("VALID INPUTS \n");
else
printf("INVALID INPUTS \n");
int sumo=0,sume=0;
for(i=m;i<=n;i++)
{
if(oddeven(i)==0)
sume=sume+pow(i,2);
else
sumo=sumo+pow(i,3);
}
printf("Sum of cubes of odd numbers is
%d \n",sumo);
printf("Sum of squares of even numbers
is %d \n ",sume);
}
OUTPUT:
4.
Check whether a given 5 digit number is a palindrome by
reversing it using while loop. Also display the difference between them. (Check
whether the given number is of 5 digits before proceeding)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int a,b,i=5,reverse=0,c=0,copy,x;
printf("enter a 5 digit
number");
scanf("%d",&a);
x=a;
copy=a;
while(copy!=0)
{
c++;
copy=copy/10;
}
if(c!=5)
printf("invalid
input");
else
{
while(i>0)
{
b=a%10;
a=a/10;
reverse=reverse+b*pow(10,(i-1));
i--;
}
printf("Reverse is
%d",reverse+1);
}
if(reverse+1==x)
{
printf("\nNumber is a
pallindrome");
}
else
printf("\nNumber is not a
pallindrome");
printf("\nThe difference is
%d",(reverse+1)-x);
}
OUTPUT:
5.
Convert a given 3 digit decimal number ( ex: 345) to a
reverse binary number. Ensure that the user has not given input more than 3
digits. ( Note: 345%2= 1, 345/2=172, 172%2=0, 172/2=86, 86%2=0, 86/2=43,
43%2=1, 43/2=21, 21%2=1, 21/2=10, 10%2=0, 10/2=5,5%2=1, 5/2=2, 2%2=0. Print all
remainders then and there to get the binary number in the reverse).
#include<stdio.h>
void main()
{
int n;
printf("Enter number");
scanf("%d",&n);
int n1=n;
int stack[50];
int counter=0;
while(n1!=0)
{
counter++;
n1=(int)n1/10;
}
if(counter==3)
{
printf("VALID INPUT \n");
}
else
{
printf("INVALID INPUT");
return;
}
int rem,c=-1;
printf("LIST OF REMAINDERS");
while(n!=0)
{
rem=n%2;
c++;
stack[c]=rem;
printf("\n %d",rem);
n=(int)n/2;
}
printf("\n");
printf("Binary is: ");
int i;
for(i=c;i>=0;i--)
{
printf("%d", stack[i]);
}
}
OUTPUT:
6.
Write a program to read a three digit number and print
the number in words (For example the number 153 is to be printed as one five
three). Use Switch case control structure.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,a,b,c;
printf("enter a three digit
number");
scanf("%d",&n);
c=n%10;
n=n/10;
b=n%10;
a=n/10;
switch(a)
{
case 1:
{
printf(" one ");
break;
}
case 2:
{
printf(" two ");
break;
}
case 3:
{
printf(" three ");
break;
}
case 4:
{
printf(" four ");
break;
}
case 5:
{
printf(" five ");
break;
}
case 6:
{
printf(" six ");
break;
}
case 7:
{
printf(" seven ");
break;
}
case 8:
{
printf(" eight ");
break;
}
case 9:
{
printf(" nine ");
break;
}
case 10:
{
printf(" ten ");
break;
}
}
switch(b)
{
case 1:
{
printf(" one ");
break;
}
case 2:
{
printf(" two ");
break;
}
case 3:
{
printf(" three ");
break;
}
case 4:
{
printf(" four ");
break;
}
case 5:
{
printf(" five ");
break;
}
case 6:
{
printf(" six ");
break;
}
case 7:
{
printf(" seven ");
break;
}
case 8:
{
printf(" eight ");
break;
}
case 9:
{
printf(" nine ");
break;
}
case 10:
{
printf(" ten ");
break;
}
}
switch(c)
{
case 1:
{
printf(" one ");
break;
}
case 2:
{
printf(" two ");
break;
}
case 3:
{
printf(" three ");
break;
}
case 4:
{
printf(" four ");
break;
}
case 5:
{
printf(" five ");
break;
}
case 6:
{
printf(" six ");
break;
}
case 7:
{
printf(" seven ");
break;
}
case 8:
{
printf(" eight ");
break;
}
case 9:
{
printf(" nine ");
break;
}
case 10:
{
printf(" ten ");
break;
}
}
}
OUTPUT:
7.
Check whether the sum of digits of a 5 digit number is
a prime number or not. Ensure that the input no. is a 5 digit number
#include<iostream>
using namespace std;
int prime(int x)
{
int i;
for(i=2;i<x/2+2;i++)
{
if(x%i==0)
return 0;
}
return 1;
}
int main()
{
int n,n1,n2;
cout<<"Enter
number";cin>>n;
n1=n2=n;
int counter=0;
while(n2!=0)
{
counter++;
n2=(int)n2/10;
}
if(counter!=5)
{
return 5;
cout<<"INVALID INPUT";
}
int rem,sum=0;
while(n1!=0)
{
rem=n1%10;
sum=sum+rem;
n1=(int)n1/10;
}
if(prime(sum)==1)
{
cout<<"Sum is
"<<sum<<" and sum is prime";
}
else
cout<<"Sum is
"<<sum<<" and sum is not prime";
}
OUTPUT:
8.
Calculate the pay scale of an employee by obtaining the
following, Basic salary, Dearness allowance (45% of basic sal), House Rental
Allowance (12% of basic), City Compensatory Allowance (10% of basic, provided
if the employee resides in a city or 0 otherwise), Deductions (if any for
loans), Also have additional information on overtime worked and calculate the
amount for that based on hours worked and rate per hour (102 for days/ 240Rs.
Per night). Finally calculate the tax amount with the following slabs,
1-1, 05,000 - Nil
1,
05,001-remaining – 10%.
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
float bs, da,hra,cca, loan, nh, dh, net;
printf("Enter basic salary");
cin>>bs;
da=0.45*bs;
hra=0.12*bs;
char ch1,ch2,ch3;
printf(" \nDo you reside in a city?
(y/n)");
cin>>ch1;
if(ch1=='y')
cca=0.1*bs;
else
cca=0;
printf(" \nAre loans pending against
you? (y/n)");
cin>>ch2;
if(ch2=='y')
{
printf("\nEnter loan
amount");
cin>>loan;
}
printf("Enter overtime hours
\n");
printf("Day hours: ");
cin>>dh;
printf("\nNight hours");
cin>>nh;
net=bs+da+hra+cca-loan+(102*dh)+(240*nh);
cout<<net;
if(net>105000)
{
cout<<"Your tax amount
is"<<0.1*net;
}
else
{
cout<<"Your tax amount is
zero";
}
}
OUTPUT:
9.
Take an input binary number like 10110 and convert it
into its decimal equivalent. (10110Ã (1*2 power 4)+(0*2 power 3)+(1*2 power
2)+(1*2 power 1)+(0*2 power 0)
#include<stdio.h>
#include<math.h>
void main()
{
int i,len=0, sum=0;
char x[10];
printf("Enter binary number");
scanf("%s",x);
len=strlen(x);
for(i=0;i<len;i++)
{
sum+= (int)(x[i]-48)*pow(2,len-1-i);
}
printf("%d", sum);
}
OUTPUT:
10. Apply
the mathematical logic and generate the following series,
1/5! + 2/4! +
3/3! + 4/2! + 5/1!
(!- represents
Factorial)
Also compute the
value of the series.
#include<stdio.h>
int main()
{
float
i,j,n,ans=0;
for(i=1;i<=5;i++)
{
n=1;
for(j=6-i;j>=1;j--)
{
n=n*j;
}
ans=ans+i/n;
}
printf("1/5!+2/4!+3/3!+4/2!+5/1!
= %.2f",ans);
}
OUTPUT:
11. Write
a program to solve the following problem:-
Create an array
with 6 student names (name length should be 7 characters long), two arrays to
store their age and grade point and create two independent structures to store
the followings from the original arrays:-
i) First
structure should store the student names with prefix and suffix gets
interchanged vice versa. (Note: prefix length=4).
ii) Second
structure should store the age and grade point of all students from the
original arrays
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
struct name
{
char name[8];
}arra[6];
struct agegpa
{
int ag;
float g;
}arrav[6];
void swapi(char arr[], char
name[])
{
char tarr[7];
int i, count=0;
for(i=3;i<7;i++)
{
tarr[count++]=arr[i];
}
for(i=0;i<3;i++)
{
tarr[count++]=arr[i];
}
strcpy(name, tarr);
name[7]=NULL;
}
int main()
{
int age[6];
float gp[6];
char array[6][8];
char names[6][8];
int i;
for(i=0;i<6;i++)
{
printf("Enter name(seven
lettered)");
scanf("%s", array[i]);
if(strlen(array[i])!=7)
{
printf("Invalid Input");
return 0;
}
printf("Enter age");
scanf("%d",&age[i]);
printf("Enter GPA");
scanf("%f",&gp[i]);
}
for(i=0;i<6;i++)
{
swapi(array[i], names[i]);
}
for(i=0;i<6;i++)
{
strcpy(arra[i].name,names[i]);
}
for(i=0;i<6;i++)
{
arrav[i].ag=age[i];
arrav[i].g=gp[i];
}
for(i=0;i<6;i++)
{
printf("%s \n",arra[i].name);
printf("%d \n",arrav[i].ag);
printf("%f \n",arrav[i].g);
}
}
OUTPUT:
12. Write
a C program to solve the following problem:-
Get a 6x6 matrix
and print the same. Create 4 symmetrical sub-matrices from the above matrix and
print all the sub-matrices. Find out the sum of all the outermost (boundary)
elements and the sum of the inner most elements and print the result.
#include<stdio.h>
void main()
{
int
x=6,i,j,n,A[x][x],outer=0,inner=0;
printf("enter
the matrix\n");
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
printf("A%d%d\t",i+1,j+1);
scanf("%d",&n);
A[i][j]=n;
}
}
printf("the
matrix is:\n");
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("4
square matrices created from this matrix are:\n");
printf("Matrix
1\n");
for(i=0;i<x/2;i++)
{
for(j=0;j<x/2;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("Matrix
2\n");
for(i=0;i<x/2;i++)
{
for(j=x/2;j<x;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("Matrix
3\n");
for(i=x/2;i<x;i++)
{
for(j=0;j<x/2;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("Matrix
4\n");
for(i=x/2;i<x;i++)
{
for(j=x/2;j<x;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
if((i==0||i==x-1)||(j==0||j==x-1))
outer=outer+A[i][j];
else
inner=inner+A[i][j];
}
}
printf("sum
of outer elements = %d\nsum of inner elements = %d",outer,inner);
}
OUTPUT:
13. Write
a program in C to merge two different strings with even number of characters in
such a way that the resultant string should display the characters of the first
string interleaved with the characters of the second string and then create a
new string by reversing both halves of the merged string.
#include<string.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
char str1[20],str2[20];
cout<<"Enter string 1";
cin>>str1;
cout<<"Enter string 2";
cin>>str2;
string strf;
int i,len1=0,len2=0,len=0;
len1=strlen(str1);
len2=strlen(str2);
if(len1%2!=0||len2%2!=0)
exit(0);
if(len1>len2)
{
for(i=0;i<len1;i++)
{
if(str2[i]!='\0')
strf=strf+str1[i]+str2[i];
else
strf=strf+str1[i];
}
}
else if(len2>len1)
{
for(i=0;i<len2;i++)
{
if(str1[i]!='\0')
strf=strf+str1[i]+str2[i];
else
strf=strf+str2[i];
}
}
else
{
for(i=0;i<len1;i++)
{
strf=strf+str1[i]+str2[i];
}
}
cout<<strf<<endl;;
int ctr=0;
while(1)
{
if(strf[ctr]=='\0')
break;
else
ctr++;
}
len=ctr-1;
strf[ctr]=NULL;
cout<<len;
char tarr[50];
int count=0;
for(i=(len/2);i<len;i++)
{
tarr[count++]=strf[i];
}
for(i=0;i<len/2;i++)
{
tarr[count++]=strf[i];
}
cout<<tarr;
}
OUTPUT:
14. Write
a C program to solve the following problem:-
Get a 4x4 matrix
and print the same. Create 2 sub-matrices by folding the original matrix
horizontally and vertically and print all the sub matrices. (Note: Vertical
folding should be done with addition of corresponding elements and horizontal
folding should be done with multiplication of corresponding elements)
#include<stdio.h>
void main()
{
int
x=4,i,j,n,A[x][x],B[2][4],C[4][2];
printf("enter
the matrix\n");
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
printf("A%d%d\t",i+1,j+1);
scanf("%d",&n);
A[i][j]=n;
}
}
printf("\nthe
matrix is:\n");
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("\nmatrix
formed by horizontal folding(multiplication of corresponding
elements):\n");
for(i=0;i<x/2;i++)
{
for(j=0;j<x;j++)
{
printf("%d\t",A[i][j]*A[x-i-1][j]);
}
printf("\n");
}
printf("\nmatrix
formed by vertical folding(addition of corresponding elements):\n");
for(i=0;i<x;i++)
{
for(j=0;j<x/2;j++)
{
printf("%d\t",A[i][j]+A[i][x-j-1]);
}
printf("\n");
}
}
OUTPUT:
15. Write
a program in C to get 4 – four digits number and then generate 4 sub-matrices
of size 2 X 2 based on unique place values like 1’s place, 10’s place, 100’s
place and 1000’s place, then add all the resultant sub-matrices to display the
final result
#include<stdio.h>
int main()
{
char a[5],b[5],c[5],d[5];
printf("Enter 4-four digit
numbers");
scanf("%s", a);
scanf("%s", b);
scanf("%s", c);
scanf("%s", d);
int w[2][2], x[2][2], y[2][2], z[2][2];
int i,j;
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
if(i==0 && j==0)
{
w[i][j]=a[3]-48;
x[i][j]=a[2]-48;
y[i][j]=a[1]-48;
z[i][j]=a[0]-48;
}
else if(i==0 && j==1)
{
w[i][j]=b[3]-48;
x[i][j]=b[2]-48;
y[i][j]=b[1]-48;
z[i][j]=b[0]-48;
}
else if(i==1 && j==0)
{
w[i][j]=c[3]-48;
x[i][j]=c[2]-48;
y[i][j]=c[1]-48;
z[i][j]=c[0]-48;
}
else if(i==1 && j==1)
{
w[i][j]=d[3]-48;
x[i][j]=d[2]-48;
y[i][j]=d[1]-48;
z[i][j]=d[0]-48;
}
}
}
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d ",
w[i][j]);
}
printf("\n");
}
printf("\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d ",
x[i][j]);
}
printf("\n");
}
printf("\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d ", y[i][j]);
}
printf("\n");
}
printf("\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d ",
z[i][j]);
}
printf("\n");
}
printf("\n");
int sum[2][2];
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
sum[i][j]=w[i][j]+x[i][j]+y[i][j]+z[i][j];
}
}
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d ",
sum[i][j]);
}
printf("\n");
}
}
OUTPUT:
16. Write
a C program to solve the following problem:-
Get a 4x4
matrix and print the same. Create 2 new matrices by taking the squares of all
outermost elements of the original matrix and by taking cubes of diagonal
elements of the original matrix and display the same. Find out the sum of these
two resultant matrices and display the same.
#include<stdio.h>
void main()
{
int
;
int
x=4,i,j,n,A[x][x],B[x][x],C[x][x];
printf("enter
the matrix\n");
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
printf("A%d%d\t",i+1,j+1);
scanf("%d",&n);
A[i][j]=n;
}
}
printf("\nthe
matrix is:\n");
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("\n\nmatrix
1(with outer elements squared):\n");
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
if((i==0||i==x-1)||(j==0||j==x-1))
{
B[i][j]=A[i][j]*A[i][j];
printf("%d\t",B[i][j]);
}
else
{
B[i][j]=A[i][j];
printf("%d\t",B[i][j]);
}
}
printf("\n");
}
printf("\n\nmatrix
2(with diagonal elements cubed):\n");
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
if(i==j)
{
C[i][j]=A[i][j]*A[i][j]*A[i][j];
printf("%d\t",C[i][j]);
}
else
{
C[i][j]=A[i][j];
printf("%d\t",C[i][j]);
}
}
printf("\n");
}
printf("\n\nsum
of matrices 1 & 2:\n");
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
printf("%d\t",B[i][j]+C[i][j]);
}
printf("\n");
}
}
OUTPUT:
17. Write a program to
find length of read string and position of first occurrence of a particular
character in a string using pointer
to a string
#include<stdio.h>
#include<stdlib.h>
int main()
{
char str[50];
char
*a, *b;
char x;
printf("Enter string and character
\n");
scanf("%s %c", str,&x);
a=&str[0];
b=&str[0];
int counter1=0;
while(*a!='\0')
{
counter1++;
a++;
}
printf("String length is: %d \n",counter1);
int counter2=0;
while(1)
{
if(*b==x)
{
break;
}
else if(counter2==counter1)
{
printf("Character was not
found");
exit(0);
}
counter2++;
b++;
}
printf("The character is found at:
%d ",counter2+1);
return 0;
}
OUTPUT:
18. Create a
registration form application by taking the details like username, address,
phone number, email
along with password and confirm password (should be same as password).Ensure
that the password is of 8 characters length.Take such details for 3 users and
display the details. In place of password display “****”.(Use Structures).
#include
<iostream>
#include
<string>
#include
<conio.h>
#include<string.h>
#include<stdlib.h>
using namespace
std;
struct info
{
string name;
string add;
string email;
int phone;
string pass;
string cpass;
}obj;
int main()
{
cout<<"Enter username";
cin>>obj.name;
cout<<"Enter address";
cin>>obj.add;
cout<<"Enter telephone
number";
cin>>obj.phone;
cout<<"Enter password(8
characters) \n";
char ch;
char c;
const char ENTER = 13;
while((ch = getch()) != ENTER)
{
obj.pass
+= ch;
cout
<< '*';
}
char *x;
x=&obj.pass[0];
int counter=0;
while(*x !='\0')
{
counter++;
x++;
}
if((counter)<8)
{
cout<<"Password length must
be greater than equal to 8 characters \n Exiting";
exit(0);
}
cout<<"Enter-Confirm password
\n";
while((c = getch()) != ENTER)
{
obj.cpass
+= c;
cout
<< '*';
}
if(obj.cpass==obj.pass)
{
cout<<"FORM SUCCESSFULLY
FILLED";
}
else
{
cout<<"Passwords do not
match";
cout<<"\n exiting";
}
}
OUTPUT:
19. Write a
program to create a structure book with fields, title, author, edition,
publications, price and quantity. Then
i) Make function
booksort( ) to display all books details sorted title wise
ii) Make
function booksearch( ) to find out for a particular book. If available
display all its details. Otherwise prompt message “Book withthat title is not
available”.
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace
std;
struct book{
char title[50];
char author[50];
int edition;
char publications[50];
int price;
int quantity;
void getinput()
{
cout<<"\nenter
the title:";
cin>>title;
cout<<"\nenter
the author:";
cin>>author;
cout<<"\nenter
edition:";
cin>>edition;
cout<<"\nenter
publication:";
cin>>publications;
cout<<"\nenter
price:";
cin>>price;
cout<<"\nenter
the quantity:";
cin>>quantity;
}
};
void
booksort(struct book c[],int n)
{ int i,j;
char tname[50];
for(i=0;i<n-1;i++)
{
for(j=0;j<n;j++)
{
if(strcmp(c[i].title,c[j].title)>0)
{
strcpy(tname,c[i].title);
strcpy(c[i].title,c[j].title);
strcpy(c[j].title,tname);
}
}
}
cout<<"\nsorted books:";
for(i=0;i<n;i++)
{
cout<<"\n"<<c[i].title;
}
}
void
booksearch(struct book b[],char c[50],int n)
{int i=0;
for(i=0;i<n;i++)
{
if(strcmp(b[i].title,c)==0)
{
cout<<b[i].title<<"\n"<<b[i].author<<"\n"<<b[i].edition<<"\n"<<b[i].publications<<"\n"<<b[i].price<<"\n"<<b[i].quantity;}
}
}
int main()
{
struct book b[100];
int n,i;
cout<<"enter the
number books:";
cin>>n;
char k[1];
for(i=0;i<n;i++)
{
b[i].getinput();
}
booksort(b,n);
cout<<"\nenter the
title of book you want details:";
cin>>k;
booksearch(b,k,n);
return 0;
}
OUTPUT:
20. Take the
details of 5 Employees such as Employee name, Employee ID, Department,
Designation, Blood group, address and phone number. Ensure that the Employee
IDs are unique. Sort the Employee records based on length of Employee names. (Use
Structures)
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace
std;
struct employee{
char name[50];
int emp_id;
char department[50];
char designation[50];
char b_grp[50];
char add[50];
double no;
void getemp()
{
cout<<"\nenter
the name:";
cin>>name;
cout<<"\nenter
employee id:";
cin>>emp_id;
cout<<"\nenter
the department:";
cin>>department;
cout<<"\nenter
the designation:";
cin>>designation;
cout<<"\nenter
the blood group:";
cin>>b_grp;
cout<<"\nenter
the address:";
cin>>add;
cout<<"enter
the phone number:";
cin>>no;
}
};
void sort(struct
employee c[5])
{ int i,j;
char tname[50];
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
if(strcmp(c[i].name,c[j].name)>0)
{
strcpy(tname,c[i].name);
strcpy(c[i].name,c[j].name);
strcpy(c[j].name,tname);
}
}
}
cout<<"\nsorted
names:";
for(i=0;i<5;i++)
{
cout<<"\n"<<c[i].name;
}
}
int main()
{
struct employee e[5];
int i;
for(i=0;i<5;i++)
{
e[i].getemp();
}
sort(e);
}
OUTPUT:
21. Create a structure for books (book
title, author, pages, year) and stack the books one over the other such that
the resulting stack has the books arranged in the order of year of publication.
(Don’t do sorting of the stack)
#include
<stdio.h>
#include
<stdlib.h>
// Stack is represented using linked list
Struct
book
{char
title;
Char
author;
Int year;
Int pages;};
struct
stack
{
Int data;
struct stack *next;
};
//
Utility function to initialize stack
void initStack(struct
stack **s)
{
*s = NULL;
}
//
Utility function to chcek if stack is empty
int
isEmpty(struct stack *s)
{
if (s == NULL)
return 1;
return 0;
}
//
Utility function to push an item to stack
void
push(struct stack **s, int x)
{
struct stack *p = (struct stack
*)malloc(sizeof(*p));
if (p == NULL)
{
fprintf(stderr, "Memory allocation
failed.\n");
return;
}
p->data = x;
p->next = *s;
*s = p;
}
//
Utility function to remove an item from stack
int
pop(struct stack **s)
{
int x;
struct stack *temp;
x = (*s)->data;
temp = *s;
(*s) = (*s)->next;
free(temp);
return x;
}
//
Function to find top item
int
top(struct stack *s)
{
return (s->data);
}
// Recursive
function to insert an item x in sorted way
void
sortedInsert(struct stack **s, int x)
{
// Base case: Either stack is empty or
newly inserted
// item is greater than top (more than all
existing)
if (isEmpty(*s) || x > top(*s))
{
push(s, x);
return;
}
// If top is greater, remove the top item
and recur
int temp = pop(s);
sortedInsert(s, x);
// Put back the top item removed earlier
push(s, temp);
}
//
Function to sort stack
void
sortStack(struct stack **s)
{
// If stack is not empty
if (!isEmpty(*s))
{
// Remove the top item
int x = pop(s);
// Sort remaining stack
sortStack(s);
// Push the top item back in sorted
stack
sortedInsert(s, x);
}
}
//
Utility function to print contents of stack
void
printStack(struct stack *s)
{
while (s)
{
printf("%d ", s->data);
s = s->next;
}
printf("\n");
}
// Driver
Program
int
main(void)
{
struct stack *top;
initStack(&top);
printf("Stack elements before
sorting:\n");
printStack(top);
sortStack(&top);
printf("\n\n");
printf("Stack elements after
sorting:\n");
printStack(top);
return 0;
}
OUTPUT:
22. Create a structure Job (job title,
file type, size, author). Write a C program to implement the scheduling of jobs
to a printer on the basis of first come first serve. Provide options to add a
job, cancel a job and display the status of jobs
#include<iostream>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
using namespace
std;
struct Job
{
string jobt,filet,size,author;
Job *next;
}*newptr,*ptr,*front,*rear,*save;
Job
*create(string j,string f,string s,string a)
{
ptr=new Job;
ptr->jobt=j;
ptr->filet=f;
ptr->size=s;
ptr->author=a;
ptr->next=NULL;
return ptr;
}
void push(Job*
np)
{
if(front==NULL)
front=rear=np;
else
{
rear->next=np;
rear=rear->next;
}
}
void pop(Job*
np)
{
if(front==NULL)
{
cout<<"underflow\n";
exit(0);
}
else
{
save=front;
front=front->next;
delete(save);
}
}
void
display(Job* np)
{
cout<<"NOW THE JOB STATUS
IS\n";
while(np!=NULL)
{
cout<<"\nJOB
TITLE: "<<np->jobt<<"\n";
cout<<"FILE
TYPE: "<<np->filet<<"\n";
cout<<"SIZE:
"<<np->size<<"\n";
cout<<"AUTHOR:
"<<np->author<<"\n";
np=np->next;
}
cout<<"\n";
}
int main()
{
char ch='y';
front=rear=NULL;
string jobt,filet,size,author;
while(ch=='y' || ch=='Y')
{
cout<<"enter
job title\n";
cin>>jobt;
cout<<"enter
file title\n";
cin>>filet;
cout<<"enter
size\n";
cin>>size;
cout<<"enter
author\n";
cin>>author;
newptr=create(jobt,filet,size,author);
push(newptr);
cout<<"do you
want to insert more jobs?\n";
cin>>ch;
}
display(front);
cout<<"give away
job?\n";
cin>>ch;
while(ch=='y' || ch=='Y')
{
pop(front);
display(front);
cout<<"are
more jobs given away?\n";
cin>>ch;
}
}
OUTPUT:
23. Design a C program for consulting a
doctor in a clinic on priority basis. (Get patient Name and assign priority for
them. Those who are having higher priority they will avail service first.)
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct doc{
char name[100];
int prior;
}a[100];
int n,front=-1,rear=-1;
int isempty()
{
if(front==-1||front>rear)
return 1;
else
return 0;
}
int isfull()
{
if(rear==n-1)
return 1;
else
return 0;
}
insert()
{
int j;
struct doc t;
if(!isfull())
{
printf("\nEnter the patient
name:");
scanf("%s",t.name);
printf("\nNow assign a
priority number(1>2):");
scanf("%d",&t.prior);
if(front==-1)
front++;
j=rear;
while(j>=0&&(t.prior))
{
a[j+1]=a[j];
j--;
}
a[j+1]=t;
rear++;
}
else
printf("\nThe list is
already full!!!");
}
void print()
{
if(!isempty())
{
printf("\nPatient
name:%s",a[front].name);
front++;
}
else
printf("\nThe list is
already over!!!");
}
int main()
{
printf("\nEnter the maximum
limit:");
scanf("%d",&n);
int o;
while(o!=0)
{
printf("\nMENU\n");
printf("\nEnter 1 for
inserting doctor job");
printf("\nEnter 2 for
getting the patient job");
printf("\nEnter 3 for
checking the job list is full or not??");
printf("\nEnter 4 for
checking the job list is empty or not??");
printf("\nEnter 0 for
exit!");
printf("\nEnter the
choice:");
scanf("%d",&o);
switch(o)
{
case 1:insert();break;
case 2:print();break;
case 4:{if(isempty()==1)
printf("\nThe job lict is
empty!!!");
else
printf("\nThe job list is
not empty!!");
break;
}
case 3:{if(isfull()==1)
printf("\nThe job list is
full!!!");
else
printf("\nThe job list is
not full!!");
break;
}
case 0: exit(0); break;
default:printf("\nYou have
entered the wrong choice!!!");
}
}
}
OUTPUT:
24. Write a C program to evaluate the
given postfix expression using Stack
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 50
int stack[MAX];
char post[MAX];
int top=-1;
void
pushstack(int tmp);
void
calculator(char c);
int main()
{
int i;
printf("Insert a postfix notation ::
");
gets(post);
for(i=0;i<strlen(post);i++)
{
if(post[i]>='0' &&
post[i]<='9')
{
pushstack(i);
}
if(post[i]=='+' || post[i]=='-' ||
post[i]=='*' ||
post[i]=='/' || post[i]=='^')
{
calculator(post[i]);
}
}
printf("\n\nResult ::
%d",stack[top]);
getch();
}
void
pushstack(int tmp)
{
top++;
stack[top]=(int)(post[tmp]-48);
}
void calculator(char
c)
{
int a,b,ans;
a=stack[top];
stack[top]='\0';
top--;
b=stack[top];
stack[top]='\0';
top--;
switch(c)
{
case '+':
ans=b+a;
break;
case '-':
ans=b-a;
break;
case '*':
ans=b*a;
break;
case '/':
ans=b/a;
break;
case '^':
ans=b^a;
break;
default:
ans=0;
}
top++;
stack[top]=ans;
}
OUTPUT:
25. Design a C program for Student
Database using Single linked list and perform search, Insert and delete
operation for a particular register number.
#include<iostream>
#include<stdio.h>
#include<process.h>
using namespace
std;
struct node
{
int info;
struct node *next;
}*start;
class
single_llist
{
public:
node*
create_node(int);
void
insert_begin();
void insert_pos();
void insert_last();
void delete_pos();
void sort();
void search();
void update();
void reverse();
void display();
single_llist()
{
start = NULL;
}
};
main()
{
int choice, nodes, element, position, i;
single_llist sl;
start = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Operations
on singly linked list"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert Node at
beginning"<<endl;
cout<<"2.Insert node at
last"<<endl;
cout<<"3.Insert node at
position"<<endl;
cout<<"4.Sort Link List"<<endl;
cout<<"5.Delete a Particular
Node"<<endl;
cout<<"6.Update Node
Value"<<endl;
cout<<"7.Search
Element"<<endl;
cout<<"8.Display Linked
List"<<endl;
cout<<"9.Reverse Linked List
"<<endl;
cout<<"10.Exit
"<<endl;
cout<<"Enter your choice :
";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at
Beginning: "<<endl;
sl.insert_begin();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at
Last: "<<endl;
sl.insert_last();
cout<<endl;
break;
case 3:
cout<<"Inserting Node at
a given position:"<<endl;
sl.insert_pos();
cout<<endl;
break;
case 4:
cout<<"Sort Link List:
"<<endl;
sl.sort();
cout<<endl;
break;
case 5:
cout<<"Delete a
particular node: "<<endl;
sl.delete_pos();
break;
case 6:
cout<<"Update Node
Value:"<<endl;
sl.update();
cout<<endl;
break;
case 7:
cout<<"Search element in
Link List: "<<endl;
sl.search();
cout<<endl;
break;
case 8:
cout<<"Display elements
of link list"<<endl;
sl.display();
cout<<endl;
break;
case 9:
cout<<"Reverse elements
of Link List"<<endl;
sl.reverse();
cout<<endl;
break;
case 10:
cout<<"Exiting..."<<endl;
break;
default:
cout<<"Wrong
choice"<<endl;
}
}
}
/*
* Creating Node
*/
node
*single_llist::create_node(int value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated
"<<endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;
return temp;
}
}
/*
* Inserting element in beginning
*/
void
single_llist::insert_begin()
{
int value;
cout<<"Enter the value to be
inserted: ";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at
beginning"<<endl;
}
/*
* Inserting Node at last
*/
void
single_llist::insert_last()
{
int value;
cout<<"Enter the value to be
inserted: ";
cin>>value;
struct node *temp, *s;
temp = create_node(value);
s = start;
while (s->next != NULL)
{
s
= s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at
last"<<endl;
}
/*
* Insertion of node at a given position
*/
void
single_llist::insert_pos()
{
int value, pos, counter = 0;
cout<<"Enter the value to be
inserted: ";
cin>>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the postion at
which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Positon out of
range"<<endl;
}
}
/*
* Sorting Link List
*/
void
single_llist::sort()
{
struct node *ptr, *s;
int value;
if (start == NULL)
{
cout<<"The List is
empty"<<endl;
return;
}
ptr = start;
while (ptr != NULL)
{
for (s = ptr->next;s !=NULL;s =
s->next)
{
if (ptr->info > s->info)
{
value = ptr->info;
ptr->info = s->info;
s->info = value;
}
}
ptr = ptr->next;
}
}
/*
* Delete element at a given position
*/
void
single_llist::delete_pos()
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<"List is
empty"<<endl;
return;
}
cout<<"Enter the position of
value to be deleted: ";
cin>>pos;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <=
counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of
range"<<endl;
}
cout<<"Element
Deleted"<<endl;
}
}
/*
* Update a given Node
*/
void
single_llist::update()
{
int value, pos, i;
if (start == NULL)
{
cout<<"List is
empty"<<endl;
return;
}
cout<<"Enter the node postion to
be updated: ";
cin>>pos;
cout<<"Enter the new value:
";
cin>>value;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start->info = value;
}
else
{
for (i = 0;i < pos - 1;i++)
{
if (s == NULL)
{
cout<<"There are
less than "<<pos<<" elements";
return;
}
s = s->next;
}
s->info = value;
}
cout<<"Node
Updated"<<endl;
}
/*
* Searching an element
*/
void
single_llist::search()
{
int value, pos = 0;
bool flag = false;
if (start == NULL)
{
cout<<"List is
empty"<<endl;
return;
}
cout<<"Enter the value to be
searched: ";
cin>>value;
struct node *s;
s = start;
while (s != NULL)
{
pos++;
if (s->info == value)
{
flag = true;
cout<<"Element
"<<value<<" is found at position
"<<pos<<endl;
}
s = s->next;
}
if (!flag)
cout<<"Element
"<<value<<" not found in the list"<<endl;
}
/*
* Reverse Link List
*/
void
single_llist::reverse()
{
struct node *ptr1, *ptr2, *ptr3;
if (start == NULL)
{
cout<<"List is
empty"<<endl;
return;
}
if (start->next == NULL)
{
return;
}
ptr1 = start;
ptr2 = ptr1->next;
ptr3 = ptr2->next;
ptr1->next = NULL;
ptr2->next = ptr1;
while (ptr3 != NULL)
{
ptr1 = ptr2;
ptr2 = ptr3;
ptr3 = ptr3->next;
ptr2->next = ptr1;
}
start = ptr2;
}
/*
* Display Elements of a link list
*/
void single_llist::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is
Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are:
"<<endl;
while (temp != NULL)
{
cout<<temp->info<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
OUTPUT: