0% found this document useful (0 votes)
4 views17 pages

Quadratic Equation and Factorial in C

The document contains multiple C programming examples demonstrating various concepts such as solving quadratic equations, calculating factorials using recursion, and illustrating call by value and call by reference. It also includes implementations of stack and queue data structures using linked lists, as well as a method to create a file containing student details. Each section includes code snippets and sample outputs for clarity.

Uploaded by

Lalith Aditya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views17 pages

Quadratic Equation and Factorial in C

The document contains multiple C programming examples demonstrating various concepts such as solving quadratic equations, calculating factorials using recursion, and illustrating call by value and call by reference. It also includes implementations of stack and queue data structures using linked lists, as well as a method to create a file containing student details. Each section includes code snippets and sample outputs for clarity.

Uploaded by

Lalith Aditya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

6.

To solve quadratic equation using pointer

#include<stdio.h>
#include<conio.h>
#include<math.h>
void qua(int a,int b,int c);
void main()
{
int x,y,z;
clrscr();
printf("\n\t\t Quadratic equation");
printf("\n\n Enter value for x");
scanf("%d",&x);
printf("\n Enter value for y");
scanf("%d",&y);
printf("\n Enter value for z");
scanf("%d",&z);
qua(x,y,z);
getch();
}
void qua(int a,int b,int c)
{
float d,r1,r2,rp,ip;
d=((b*b)-(4*a*c));
if(d>0)
{
printf("\n\n Roots are unequal");
r1=((-b)+sqrt(d))/(2*a);
r2=((-b)-sqrt(d))/(2*a);
printf("\n\n Root R1 is:%f",r1);
printf("\n\n Root R2 is:%f",r2);
}
else if(d<0)
{
rp=0,ip=0;
d=abs(d);
rp=(-b)/(2*a);
ip=sqrt(d)/(2*a);
printf("\n\n Real part:%f",rp);
printf("\n\n Imaginary part:%f",ip);
printf("\n\n Root r1=%f+i%f",rp,ip);
printf("\n\n Root r2=%f-i%f",rp,ip);
}
else
{
printf("\n\n Roots are equal");
r1=(-b)/(2*a);
r2=r1;
printf("\n\n Root R1:%f",r1);
printf("\n\n Root R2:%f",r2);
}
}
Output:

Quadratic equation

Enter the value for x1


Enter the value for y2
Enter value for z3

Roots are imaginary


Real part: -1.000000
Imaginary part: 1.414214
Root r1 i -1.000000+ i1414214
Root r2=1.000000- i1.414214
[Link] find factorial of a number using recursion

#include<stdio.h>
#include<conio.h>
int fact(int n);
void main()
{
int n;
clrscr();
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, fact(n));
getch();
}
int fact(int n)
{
if (n>=1)return n*fact(n-1);
else return 1;
}

Output:

Enter the positive number 5


Factorial value : 120
[Link] demonstrate call by value and call by reference

#include<stdio.h>
#include<conio.h>
void swapCbyR(int *a, int *b);
void swapCbyV(int x, int y);
void main()
{
int m,n;
clrscr();
printf("Enter Two Values");
scanf("%d %d",&m,&n);
printf("values before swap m = %d \n and n = %d",m,n);
printf("\nCall by Value \n");
swapCbyV(m,n);
printf("\nCall by Reference \n");
swapCbyR(&m, &n);
getch();
}
void swapCbyV(int a, int b)
{
int tmp;
tmp = a;
b = tmp;
printf("\nvalues after swap m = %d and n = %d\n", a, b);
}
void swapCbyR(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf("\n values after swap a = %d and b = %d\n", *a, *b);
}
Output:

Enter two values 18


12
Values before swap m=18
And n=12
Call by value

Values after swap m=12 and n=18


Call by reference
Values after swap a=12 and b=18
[Link] create a file containing student details

#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
int rollno,m1,m2,m3;
FILE *fp;
fp=fopen("[Link]","w");
printf("\n\n Student Details");
printf("\n\n Enter student name");
scanf("%s",name);
printf("\n\n Enter rollno");
scanf("%d",&rollno);
printf("\n\n Enter m1");
scanf("%d",&m1);
printf("\n\n Enter m2");
scanf("%d",&m2);
printf("\n\n Enter m3");
scanf("%d",&m3);
fprintf(fp,"%s\n%d\n%d\n%d\n%d\n",name,rollno,m1,m2,m3);
printf("%s\n%d\n%d\n%d\n%d\n",name,rollno,m1,m2,m3);
fclose(fp);
getch();
}
Output:

Student details

Enter student name Janani

Enter roll no 10
Enter m1 88
Enter m2 86
Enter m3 84

Janani
10
88
86
84
10(a)stack using linked list

#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
void main ()
{
int choice=0;
printf("\nStack operations using linked list\n");
while(choice != 4)
{
printf("\n\nChose one from the below options...\n");
printf("\[Link]\[Link]\[Link]\[Link]");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1: push(); break;
case 2: pop(); break;
case 3: display(); break;
case 4: printf("Exiting...."); break;
default:printf("Please Enter valid choice ");
};
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("Enter the value");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Item pushed");
}
}
void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
}
void display()
{
Int I;
struct node *ptr; ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n"); while(ptr!=NULL)
{
printf("%d\n",ptr->val); ptr = ptr->next;
}
}
}
Output:

Enter your choice


2
Item poped
Choose one from the below options……
[Link]
[Link]
[Link]
[Link]
Enter your choice
3
Printing stack elements
23
10(b)queue using linked list

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n");
printf("\n==============================================================
===\n");
printf("\[Link] an element\[Link] an element\[Link] the queue\[Link]\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1:insert(); break;
case 2:delete(); break;
case 3:display(); break;
case 4:exit(0); break;
default:printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{
printf("\nprinting values \n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}
Output:

****************in menu****************
[Link] an element
[Link] an element
[Link] the queue
[Link]
Enter your choice 1
Enter value 10

***************in menu**************
[Link] an element
[Link] an element
[Link] the queue
[Link]

Enter your choice 2


Enter value 20

************in menu************
[Link] an element
[Link] an element
[Link] the queue
[Link]

Enter your choice 3


Printing values……..
10
20

You might also like