IF ANYONE WANT TO TAKE TUITION FROM CLASS 5 TO CLASS 12 FOR SUBJECTS LIKE:
1)PHYSICS
2)COMPUTER (C,C++,JAVA)
3)CHEMISTRY
4)MATH
PLEASE CONTACT AT : 8981869131
NOTE : PEOPLE HAVING COACHING CENTER THEY CAN ALSO CONTACT ME , IF THERE IS ANY VACANCY OF THE TEACHER FOR THE ABOVE SUBJECT , THEY CAN ALSO CONTACT AT THE GIVEN NUMBER.
Question: Write a C program to implement the following Linked List function:
Insert at any position
Delete at any position
Sorting the value in the list
Displaying the total nodes
Counting the total number of nodes
SOLUTION :
#include<stdio.h>
#include<conio.h>
typedef struct node
{
int d;
struct node*link;
}Node;
Node*head=NULL; // Global variable Declaration
int append() //Add at the end function
{
Node*p,*q;
p=(struct node*)malloc(sizeof(struct node));
printf(“\n Enter the value to add at the end:”);
scanf(“%d”,&p->d);
if(head==NULL)
{
head=p;
return;
}
q=head;
while(q->link!=NULL)
{
q=q->link;
}
q->link=p;
return 1;
}
void add_beg() //Add at the beginning function
{
Node*p;
p=(struct node*)malloc(sizeof(struct node));
printf(“\n Enter the value to add at the beginning :”);
scanf(“%d”,&p->d);
if(head==NULL)
{
p->link=NULL;
head=p;
return;
}
p->link=head;
head=p;
}
void del_beg() //Delete at the beginning function
{
Node*q;
if(head==NULL)
{
printf(“\nThere is no node to be deleted:”);
return;
}
printf(“\nDeleted Node is: %d”,head->d);
head=head->link;
free(q);
}
void del_end() Delete at the end function
{
Node*q,*r;
if(head==NULL)
{
printf(“\n Cannot delete.Linked list is empty.”);
return;
}
if(head->link==NULL)
{
printf(“\nDeletd value:%d”,head->d);
q=head;
head=NULL;
free(q);
return;
}
r=head;
q=head->link;
while(q->link!=NULL)
{
r=q;
q=q->link;
}
printf(“\n Deleted value:%d”,q->d);
r->link=NULL;
free(q);
}
void display() // Display function
{
Node*q;
q=head;
printf(“\n The values in the Linked list are:\n”);
while(q!=NULL)
{
printf(“%d->”,q->d);
q=q->link;
}
printf(“NULL\n”);
}
int count() Count function
{
Node*q=head;
int c=0;
while(q!=NULL)
{
c=c+1;
q=q->link;
}
return c;
}
void add_any_pos() // Add at any position
{
Node*p,*q;
int i,pos;
printf(“\n Enter the position where you want to insert the node:”);
scanf(“%d”,&pos);
if(pos<1||pos<count()+1)
{
printf(“\n Invalid position”);
return;
}
if(pos==1)
{
add_beg();
return;
}
if(pos==count()+1)
{
append();
return;
}
p=(struct node*)malloc(sizeof(struct node));
printf(“\n Enter the value”);
scanf(“%d”,&p->d);
q=head;
for(i=2;i<pos;i++)
{
q=q->link;
}
p->link=q->link;
q->link=p;
}
void del_any_pos() //Display at any position
{
Node *q,*r;
int pos,i;
if(head==NULL)
{
printf(“\n There is no node to delete:”);
return;
}
printf(“\n Enter the position of the node you want to delete:”);
scanf(“%d”,&pos);
if(pos<1||pos>count()+1)
{
printf(“\n Invalid position”);
return;
}
if(pos==1)
{
del_beg();
return;
}
if(pos==count()+1)
{
del_end();
return;
}
r=head;
q=head->link;
for(i=2;i<pos;i++)
{
r=q;
q=q->link;
}
printf(“\n Deleted Node:%d”,q->d);
r->link=q->link;
free(q);
}
void bubble() // Sorting function
{
Node*q=head,*r,*s;
int tmp;
while(q!=NULL)
{
r=head;
while(r->link!=NULL)
{
s=r->link;
if(r->d>s->d)
{
tmp=r->d;
r->d=s->d;
s->d=tmp;
}
r=r->link;
}
q=q->link;
}
}
void main() // Main function
{
int ch;
do //Do – while loop
{
printf(“\nMENU”);
printf(“\n1.Add a node at the end of the Linked List.”);
printf(“\n2.Add a node at the beginning of the Linked List.”);
printf(“\n3.Delete a node at the end of the Linked List.”);
printf(“\n4.Delete a node at the beginning of the Linked List.”);
printf(“\n5.Display the values in the linked list.”);
printf(“\n6.Enter the node at any position of linked list.”);
printf(“\n7.Delete a node from any position.”);
printf(“\n8.Sort the value of linked list.”);
printf(“\n9.Terminate the program.”);
printf(“\n*****************************/n”);
printf(“\n*****************************/n”);
printf(“\nEnter your choice:”);
ch = getch()-48;
switch(ch)
{
case 1:
append();
break;
case 2:
add_beg();
break;
case 3:
del_end();
break;
case 4:
del_beg();
break;
case 5:
display();
break;
case 6:
add_any_pos();
break;
case 7:
del_any_pos();
break;
case 8:
bubble();
break;
default:
printf(“\nInvalid Input Enter Again.”);
} //End of Switch-Case
printf(“\nPress any key to continue”);
}while(ch!=0);
getch();
return 0;
}
Question: Write a C program to implement the function of linked list like INSERTION,DELETION,REVERSE,CREATE,DISPLAY AND MAIN. You can add many more function like CONCATE,PRIME NUMBER AND MANY MORE.Also Create a Menu driven pattern.
Solution:
#include <stdio.h> #include<conio.h> #include<stdlib.h> typedef struct list
{ int info; struct list *next;
}node; node *create(int n)//Creating the node all at once
{ int i; node *p,*q,*r; p=(node*)malloc(sizeof(node)); if(n<=0) return; printf(“Enter the element of the list = “); scanf(“%d”,&p->info); q=p; for(i=1;i<n;i++)
{ r=(node*)malloc(sizeof(node)); printf(“Enter the element of the list = “); scanf(“%d”,&r->info); q->next=r; q=r;
} q->next=NULL; return p;
} void insert(node **p,node *t, int pos) // Inserting the node at a particular position
{ node *q; int j; if(pos==0)
{ t->next=(*p); (*p)=t; return;
} q=(*p); for(j=1;j<pos&&q!=NULL;j++) q=q->next; if(q ==NULL) return; t->next=q->next; q->next=t; return;
} void delete(node **p,int pos) // Deleting the node at a particular position
{ node *q,*x; int i; if(pos==0)
{ (*p)=(*p)->next; return;
} q=(*p); x=q->next; for(i=1;i<pos&&q!=NULL;i++)
{ q=x; x=x->next;
} if(q==NULL) return; q->next=x->next; x->next=NULL; return;
} void display(node *p) // Displaying the node { while (p!=NULL)
{ printf(“The element of the list = %d\n”,p->info); p=p->next;
} printf(“\n”);
} node *reverse(node *p) // Reversing the node
{ int j; node *i,*q,*r; i=NULL; q=p; r=q->next; while(q->next!=NULL)
{ q->next=i; i=q; q=r; r=r->next;
} q->next=i; return(q);
}
int main() //Main method
{ int ch,n,pos; node *p,*t,*r; do
{ printf(“\n\tMENU\n1.Create\n2.Insert\n3.Display\n4.Delete any node\n5.Reverse the list\n6.Exit\n\n”); printf(“Enter Your choice = “); scanf(“%d”,&ch); switch(ch) // Creating the menu driven pattern
{ case 1: printf(“Enter the number of data you want to enter = “); scanf(“%d”,&n); p=create(n); break; case 2: t=create(1); printf(“Enter the position where you want to insert the new node = “); scanf(“%d”,&pos); insert(&p,t,pos-1); break; case 3: display(p); break; case 4: printf(“Enter the position of the node which you want to delete = “); scanf(“%d”,&pos); delete(&p,pos-1); break; case 5: p=reverse(p); break; case 6: break; default:printf(“You have entered the wrong choice\n”); break;
} }while(ch!=6); return 0;
}
Question: Write a C program to delete an element from the array and print the array after deletion.
INPUT: 12345
OUTPUT: 1234 (5 ELEMENT IS DELETED)
Solution:
#include<stdio.h> #include<malloc.h> int search(int *p,int s,int n)//Checking if the delete value is present or not
{ int i; for(i=0;i<n;i++)
{
if(s==*(p+i)) return i;
} return -1;
} int delete(int*p,int s,int n,int pos) //function for deletion
{ int i,j;
printf(“\n The deleted element is = %d”,*(p+pos)); for(i=pos;i<n;i++) *(p+i)=*(p+i+1); n–; //decreasing the value from right as deletion is not possible in array because the memory allocate by the array is fixed. return n;
} int main()
{ int n,*p,x,s,i,pos; printf(“\n Enter the number of inputs :”); scanf(“%d”,&n); p=(int*)malloc(n*sizeof(int)); for(i=0;i<n;i++)
{ printf(“\n Enter the value :”); scanf(“%d”,(p+i));
} printf(“\n Enter the value to delete :”); scanf(“%d”,&s); pos=search(p,s,n); if(pos==-1) //if the deleted value is not present.
{ printf(“\n Value not found “); return;
} else
{ x=delete(p,s,n,pos); printf(“\n The value after deletion is : “); for(i=0;i<x;i++)
{ printf(“%d\t”,*(p+i));
}
printf(“\n The deleted value position is %d\n”,pos); //Indicating position of the deleted value.
} getch(); return 0;
}
Write a Program in Java to input a number and check whether it is a Pronic Number or Heteromecic Number or not.
Pronic Number : A pronic number, oblong number, rectangular number or heteromecic number, is a number which is the product of two consecutive integers, that is, n (n + 1).
The first few pronic numbers are:
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 … etc.
Solution :
#include<stdlib.h> int main(void) { int n,i,flag=0; printf(“\nEnter any number :”);//Inputtinhg the value scanf(“%d”,&n); for(i=1;i<=n;i++) { if(i*(i+1)==n)//Condition for pronic number { flag=1; break; } } if(flag==1) printf(“\n The number is a pronic number “); else printf(“\n The number is not pronic number “); getch(); return 0; }
Question: Write a Program in Java to input a number and check whether it is a Harshad Number or Niven Number or not.
NOTE: Harshad Number : In recreational mathematics, a Harshad number (or Niven number), is an integer (in base 10) that is divisible by the sum of its digits.
Let’s understand the concept of Harshad Number through the following example:
The number 18 is a Harshad number in base 10, because the sum of the digits 1 and 8 is 9 (1 + 8 = 9), and 18 is divisible by 9 (since 18 % 9 = 0)
The number 1729 is a Harshad number in base 10, because the sum of the digits 1 ,7, 2 and 9 is 19 (1 + 7 + 2 + 9 = 19), and 1729 is divisible by 19 (1729 = 19 * 91)
The number 19 is not a Harshad number in base 10, because the sum of the digits 1 and 9 is 10 (1 + 9 = 10), and 19 is not divisible by 10 (since 19 % 10 = 9)
Solution: #include<stdio.h>
int main(void)
{
int n,d,a,sum=0;// Initializing the variable
printf(“\n Enter the number :”);
scanf(“%d”,&n);
a=n; //Making copy of the original number because of while loop
while(a>0) // Finding the sum of digit
{
d=a%10;
sum=sum+d;
a=a/10;
}
if(n%sum==0) //Checking if the remainder is zero or not
printf(“\n The number is Harshard Number “);
else
printf(“\n The number is not a Harshard Number “);
getch();
return 0;
}
Question: Write a C program to search the value in an array and give the appropriate message.(Using pointer)
Solution: #include <stdio.h>
int main(void)
{
int a[10],i,search,*p,flag=0; //Intializing all the variable
p=&a[0]; //pointer to point the address of the first array
printf(“\n Inputting the first array “);
printf(“\n**********************\n”);
for(i=0;i<10;i++)
{
printf(“\n Enter a number :”);
scanf(“%d”,(p+i));
}
printf(“\n**********************\n”);
printf(“\n Enter the value to search : “);
scanf(“%d”,&search); //What value you want to search
for(i=0;i<10;i++)
{
if(search==*(p+i)) //if search value found
{
flag=1; //we rise the flag
break; //Then we came out of the loop
}
}
if(flag==0)
printf(“\n Value not found \n”);
else
printf(“\n Value found \n”);
getch();
return 1;
}
Question:Write a program to input two number and check whether it is twin prime or not using function.
NOTE:Twin Prime numbers are a pair of numbers which are both prime and their difference is 2.
We haven’t use function prototype in the below code.
Example: Twin Prime numbers are :
(3,5) (5,7) (11,13) (17,19) (29,31) (41,43) (59,61) (71,73)
#include<stdio.h> int isPrime(int n) //funton for checking prime { int i; if(n==1) return 0; for(i=2;i<=n/2;i++) { if(n%i == 0) return 0; else return 1; } } int main() { int a,b; printf(“\n Enter the first number \n”); scanf(“%d”,&a); printf(“\n Enter the second number \n”); scanf(“%d”,&b); if((a-b==2||b-a==2)&&(isPrime(a)==1&&isPrime(b)==1)) //Condition for twin prime { printf(“\n The number is twin prime “); } else { printf(“\n The number is not Twin Prime “); } getch(); return 0; }
Question : Write a C program to find the Prime Number with the help of Array.
The size of the array is 10.
Solution :
#include<stdio.h>
int main()
{
int A[10],i,j,n,c; // Intializing the variable
printf(“\n Inputting the first array :”);
printf(“\n *************************\n”);
for(i=0;i<10;i++) // Running forloop upto 10 times
{
printf(“\n Enter a number : \n”); //Taking input
scanf(“%d”,&A[i]);
}
printf(“\n The prime numbers are : \n”);
for(j=0;j<10;j++) // forloop to check that the number inputted in Array is prime or not
{
n=A[j];
c=0;
for(i=1;i<=n;i++) // Calculating the prime number
{
if(n%i==0)
c++;
}
if(c==2)
printf(“\n The numbers are %d”,n);
}
getch();
return 1;
}