0% found this document useful (0 votes)
51 views9 pages

C Programs

The document contains code snippets for several C programs including: 1) A program to check if a string is a palindrome by comparing the string to its reverse. 2) A program that calculates the sum and average of numbers stored in an array. 3) A program that appends additional content to the end of a text file.

Uploaded by

manirathina
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views9 pages

C Programs

The document contains code snippets for several C programs including: 1) A program to check if a string is a palindrome by comparing the string to its reverse. 2) A program that calculates the sum and average of numbers stored in an array. 3) A program that appends additional content to the end of a text file.

Uploaded by

manirathina
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

6 Program for 0uadrat|c equat|on

#include
void main()
{
int a,b,c,d,e,f,g;
clrscr();
printf("Enter the three values);
scanf("%d%d%d,&a&b&c);
d=((b*b)-(4*a*c));
if(d==0)
{
printf("Roots are real and equal);
f=-b/(2*a);
printf("x1=%d\nx2=%d,f,f);
}
else if(d>0)
{
printf("Roots are real and distinct);
e=sqrt(d);
f=(-b+e)/(2*a);
g=(-b-e)/(2*a);
printf("x1=%d\nx2=%d,f,g);
}
else
{
printf("Roots are imaginary);
d=-d;
e=sqrt(d);
f=-b/(2*a);
g=e/(2*a);
printf("x1=%d+%d\nx2%d-%d,f,g,f,g);
}
getch();
}


Re: program to Iind the roots oI a quadratic equation
3807
# 1
#include<stdio.h
#include<conio.h
#include<math.h
#include<process.h
void main()
,
float a,b,c,x1,x2,disc;
clrscr();
printf("Enter the co-

efficients\n");
scanf("%f%f%f",&a,&b,&c);
disc=bb-4ac;/to find
discriminant/
if(disc0)/distinct roots/
,
x1=(-b+sqrt(disc))/(2a);
x2=(-b-sqrt(disc))/(2a);
printf("The roots are
distinct\n");
exit(0);
,
if(disc==0)/Equal roots/
,
x1=x2=-b/(2a);
printf("The roots are
equal\n");
printf("x1=%f\nx2=%f\n",x1,x2);
exit(0);
,
x1=-b/(2a);/complex roots/
x2=sqrt(fabs(disc))/(2a);
printf("The roots are
complex\n");
printf("The first
root=%f+i%f\n",x1,x2);
printf("The second root=%f-
i%f\n",x1,x2);
getch();
,
Complex numbers
public class Test
public int add(int a, int b)
return a b;
}
public int sub(int a, int b)
return a - b;
}
public int mult(int a, int b)
return a * b;
}
public div(int a, int b)
return a/b;
}
}

complex num bers for add and subracL
#include<stdio.h
int 2,in(),
int a,b,c,d,x,y;
printf("\nEnter the first complex number:");
scanf("%d%d",&a,&b);
printf("\nEnter the second complex number:");
scanf("%d%d",&c,&d);
if(b<0)
printf("%d-i\n",a-b);
else
printf("d+i\n",a+b);
if(d<0)
printf("d-i\n",c-d);
else
printf("%d+i\n",c+d);
printf("\nADDITIJN ");
x=a+c;
y=b+d;
if(y0)
printf("%d-i%d",x,-y);
else
printf("%d+i%d",x,+y);
printf("\n\nSUBTRACTIJN ");
x=a-c;
y=b-d;
if(y<0)
printf("%d-i%d",x,-y);
else
printf("%d+i%d",x,+y);
7etu7n 0;
,
Area of c|rc|e

CalculaLes Lhe area of a clrcle

#lnclude sLdloh
#deflne l 314139

lnL maln()

floaL sum radlus
prlnLf(1hls program works ouL Lhe area of a clrclen)
prlnLf(LnLer Lhe radlus of Lhe clrclen)
scanf(fradlus)
/*area of a clrcle ls pl * radlus * radlus*/
sum l * radlus * radlus
/*dlsplay Lhe area*/
prlnLf(1he area of a clrcle ls fnsum)
reLurn 0


#include <stdio.h
#define PI 3.141
int 2,in()
,
flo,t r, a;
printf("Radius: ");
scanf("%f", &r);
a = PI r r;
printf("%f\n", a);
7etu7n 0;
,
the sum and average of given numbers using Arrays

Ok here it is:
Code: cpp
#include <iostream
#include <conio.h

using namespace std;

int main (int argc, char argv,)
,
int num100,;
int n;
register int i;
int sum = 0;
float avg;
cout << "Enter number of items in array: ";
cin n;
for (i = 0; i < n; i++)
,
cout << "Enter the " << i+1 << "th number: ";
cin numi,;
sum += numi,;
,
avg = (float) sum/n;
cout << "Sum is " << sum << "nAnd Average = " << avg;
getch();
,

Average of marks
/* Use of array */
/* Program to calculate average marks of 30 students */
main( )
{
int avg, sum = 0 ;
int i ;
int marks[30] ; /* array declaration */

for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ; /* store data in array */
}

for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i] ; /* read data from an array*/

avg = sum / 30 ;
printf ( "\nAverage marks = %d", avg ) ;
}

C program to check whether a StrIng Is PaIIndrome or not!

C program to check whether a StrIng Is PaIIndrome or not!
ContrIbuted by : Kaustubh 8agal

#IncludestdIo.h
#IncludestrIng.h
#defIne sIze 26
voId maIn()
[
char strsrc[sIze];
char strtmp[sIze];
clrscr();
prIntf(\n Enter StrIng:= ); gets(strsrc);
strcpy(strtmp,strsrc);
strrev(strtmp);
If(strcmp(strsrc,strtmp)==0)
prIntf(\n Entered strIng \s\ IspalIndrome,strsrc);
else
prIntf(\n Entered strIng \s\ Is not
palIndrome,strsrc);
getch();
]

#lncludesLdloh
#lncludeconloh
lnL sLpal(char sLr30)
vold maln()

char sLr30
lnL pal
clrscr()
prlnLf(nnL Ln1L8 A S18lnC )
geLs(sLr)
pal sLpal(sLr)
lf(pal)
prlnLf(nL 1PL Ln1L8Lu S18lnC lS A ALlnu8CML")
else
prlnLf(nL 1PL Ln1L8Lu S18lnC lS nC1 A ALlnu8CML")
geLch()

lnL sLpal(char sLr30)

lnL l 0 len 0 pal 1
whlle(sLrlen!'r)
len++
len
for(l0 llen/2 l++)

lf(sLrl sLrlenl)
pal 1
else

pal 0
break


reLurn pal

ort|ng two d|mens|ona| array
#include
#include

int main(void)
,
int item100,;
int a, b, t;
int count;

/ read in numbers /
printf("How many numbers. ");
scanf("%d", &count);

fo7(a = 0; a < count; a++)
scanf("%d", &itema,);

/ now, sort them using a bubble sort /

fo7(a = 1; a < count; ++a)
fo7(b = count-1; b = a; --b) ,
/ compare adjacent elements /
if(item b - 1, item b ,) ,
/ exchange elements /
t = item b - 1,;
item b - 1, = item b ,;
item b , = t;
,
,

/ display sorted list /
fo7(t=0; t

7etu7n 0;
,

C Program To Append Contents Into A File


O SATURDAY, JULY 17, 2010
O SATISH
O SHARE
O TWEET

#lnclude conloh
#lnclude sLdloh
#lnclude processh
vold maln()

llLL *fp
char c
clrscr()
prlnLf(conLenLs of Leh flle before appendlngn)
fpfopen(daLaLxLr)
whlle(!feof(fp))

cfgeLc(fp)
prlnLf(cc)


fpfopen(daLaLxLa)
lf(fpnuLL)

prlnLf(flle canL appendn)
exlL(1)

prlnLf(nLnLer Lhe sLrlng Lo appendn)
whlle(c!)

cgeLche()
fpuLc(cfp)

fclose(fp)
prlnLf(flle conLenLs afLer appendlngn)
fpfopen(daLaLxLr)
whlle(!feof(fp))

cfgeLc(fp)
prlnLf(cc)

You might also like