0% found this document useful (0 votes)
336 views36 pages

#Include #Include Void Main (CLRSCR

The document contains code snippets for various C programs including: 1) A program to find the greatest of three numbers. 2) Programs to calculate summation, quadratic equations, series summation, prime numbers, Armstrong numbers, Fibonacci series, factorials, palindrome numbers, array sums, and palindrome strings. 3) A program to multiply two 2x2 matrices. 4) A program to maintain student records including registration number, name, percentage, and class.

Uploaded by

meerashekar
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 DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
336 views36 pages

#Include #Include Void Main (CLRSCR

The document contains code snippets for various C programs including: 1) A program to find the greatest of three numbers. 2) Programs to calculate summation, quadratic equations, series summation, prime numbers, Armstrong numbers, Fibonacci series, factorials, palindrome numbers, array sums, and palindrome strings. 3) A program to multiply two 2x2 matrices. 4) A program to maintain student records including registration number, name, percentage, and class.

Uploaded by

meerashekar
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 DOC, PDF, TXT or read online on Scribd

PROGRAM FOR GREATEST AMONG THREE NUMBERS

#include<stdio.h>
#include<conio.h>

void main ()
{
int a, b, c;
clrscr();
printf("Enter the value of a,b,c");
scanf("%d%d%d",&a,&b,&c);

if((a>b)&&(a>c))
{
printf("A is greater");
}
else if((b>c)&&(b>a))
{
printf("B is greater");
}
else
printf("C is greater");
getch();
}
OUT PUT:

Enter the value of a,b,c


5
6
7
C is greater

Enter the value of a,b,c


25
96
67
B is greater

Enter the value of a,b,c


7
6
55
C is greater
PROGRAM FOR SUMMATION

#include<stdio.h>
#include<conio.h>

void main()
{
int i=1,sum=0;
clrscr();

while(i<=10)
{
sum=sum+i;
i++;
}
printf("The sum of numbers upto 10 is %d",sum);
getch();
}
OUTPUT:

The sum of numbers upto 10 is 55


PROGRAM FOR QUADRATIC EQUATION

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
float a,b,c,d,real,imag,r1,r2,n;
int k;
clrscr();
printf("Enter the numbers");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
k=1;
if(d==0)
k=2;
if(d>0)
k=3;

switch(k)
{
case 1:
{
printf("Roots are imaginary\n");
real=-b/(2*a);
d=-d;
n=pow((double)d,(double)0.5);
imag=n/(2*a);
printf("r1=%7.2f+j%7.2f\n",real,imag);
printf("r2=%7.2f-j%7.2f\n",real,imag);
break;
}

case 2:
{
printf ("roots are real and equal\n");
r1=-b/(2*a);
printf("r1=r2%7.2f\n",r1);
break;
}
case 3:
{
printf("real and unequal roots\n");
r1=(-b+sqrt((double)d)/(2*a));
r2=(-b-sqrt((double)d)/(2*a));
printf("r1=%7.2f\n",r1);
printf("r2=%7.2f\n",r2);
break;
}
getch();
}
OUTPUT:

Enter the numbers:


2
3
5
Roots are imaginary
R1=-0.75 +j 1.39
R2= -0.75 –j 1.39
PROGRAM FOR FINDING THE SUM OF SERIES

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int n,i,w,s=0,p=2;
clrscr();
printf("ENTER A VALUE FOR N: " ,n);
scanf("%d",&n);

for(i=1;i<=n;i++)
{
w=(pow(i,p));
s=s+w;
}
printf("SUM OF SERIES: %d",s);
getch();
}
OUTPUT:

ENTER A VALUE FOR N: 2


SUM OF SERIES: 5
PROGRAM FOR FINDING THE PRIME NUMBER

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("\n\nENTER THE NUMBER ");
scanf("%d",&n);

for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
printf("\n THE GIVEN NUMBER IS NOT A PRIME NUMBER");
goto exit;
}
}
printf("\nTHE GIVEN NUMBER IS A PRIME NUMBER");
exit:getch();
}
OUTPUT:

ENTER THE NUMBER: 3


THE GIVEN NUMBER IS A PRIME NUMBER

ENTER THE NUMBER: 80


THE GIVEN NUMBER IS NOT A PRIME NUMBER
PROGRAM FOR ARMSTRONG NUMBER

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int m,n,sum=0,l;
clrscr();
printf("ENTER THE NUMBER: ");
scanf("%d",&m);
l=m;

while(m!=0)
{
n=m%10;
sum=sum+pow(n,3);
m=m/10;
}
if(l==sum)
printf("\n %d IS AN AMSTRONG NUMBER",l);
else
printf("\n%d IS NOT AN AMSTRONG NUMBER",l);
getch();
}
OUTPUT:

ENTER THE NUMBER: 153


153 IS AN AMSTRONG NUMBER

ENTER THE NUMBER: 13


13 IS NOT AN AMSTRONG NUMBER
PROGRAM FOR FIBONACCI SERIES

#include<stdio.h>
#include<conio.h>

void main()
{
void fibo(void);
clrscr();
fibo();
getch();
}

void fibo()
{
int n,i,l,j,s;
printf("Enter the value n=");
scanf("%d",&n);
i=0;
j=1;
s=0;
l=0;
printf("%d\n",i);
printf("%d\n",j);
while(l<=n-3)
{
s=i+j;
printf("\n%d",s);
i=j;
j=s;
l++;
}
getch();
}
OUTPUT:

Enter the value n=5


0
1
1
2
3
PROGRAM FOR FACTORIAL OF NUMBERS

#include<stdio.h>
#include<conio.h>

int fact=1;
int facto(int n)
{
fact*=n;
if(n>1)
return(facto(n-1));
else
return(fact);
}

void main()
{
int m,facti;
clrscr();
printf("\nENTER VALUE TO FIND THE FACTORIAL: ");
scanf("%d",&m);
facti=facto(m);
printf("\nTHE FACTORIAL VALUE: %d",facti);
getch();
}
OUTPUT:

ENTER VALUE TO FIND THE FACTORIAL: 5


THE FACTORIAL VALUE: 120
PROGRAM FOR FINDING THE PALINDROME NUMBER

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int m,n,rev=0,l;
clrscr();
printf("ENTER THE NUMBER: ");
scanf("%d",&m);
l=m;
while(m!=0)
{
n=m%10;
rev=(rev*10)+n;
m=m/10;
}
if(l==rev)
printf("\n%d IS A PALINDROME",l);
else
printf("\n%d IS NOT A PALINDROME",l);
getch();
}
OUTPUT

ENTER THE NUMBER: 1111


1111 IS A PALINDROME

ENTER THE NUMBER: 321


321 IS NOT A PALINDROME
PROGRAM FOR FINDING THE SUM OF GIVEN ARRAY

#include<stdio.h>
#include<conio.h>

void main()
{
int a[50],b=0,i,n;
clrscr();
printf("Enter the limit: ");
scanf("%d",&n);
printf("Enter the numbers: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
b=b+a[i];
}
printf("The sum of the array is %d",b);
getch();
}
OUTPUT:

Enter the limit: 3


Enter the numbers: 7
7
5
The sum of the array is 19
PROGRAM FOR FINDING THE PALINDROME STRING

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
char str[20];
int len,pal=1;
int i=0;
clrscr();
printf("Enter the string: ");
scanf("%s",str);
len=strlen(str);
while(i<len)
{
if(str[i]!=str[(len-1)-i])
{
pal=0;
break;
}
else
{
i++;
}
}
if(pal==1)
{
printf("\nThe given string is a palindrome");
}
else
{
printf("\nThe given string is not a palindrome");
}
getch();
}
OUTPUT:

Enter the string: mam


The given string is a palindrome

Enter the string: chinna


The given string is not a palindrome
PROGRAM FOR MULTIPLICATION OF 2*2 MATRIX (TWO DIMENSIONAL)

#include<stdio.h>
#include<conio.h>

void main()
{
int a[2][2],b[2][2],c[2][2],k,i,j;
clrscr();

printf("Enter the value of the first 2*2 matrix:\n");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}

printf("Enter the value of the second 2*2 matrix:\n");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
}

printf("Values of first matrix:\n");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}

printf("Values of second matrix:\n");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=0;
for(k=0;k<2;k++)
{
c[i][j]+=(a[i][k]*b[k][j]);
}
}
}

printf("Product of two matri:\n");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
OUTPUT:

Enter the value of the first 2*2 matrix:


2 3
3 4
Enter the value of the second 2*2 matrix:
3 4
4 5
Values of first matrix:
2 3
3 4
Values of second matrix:
3 4
4 5
Product of two matrix:
18 23
25 32
PROGRAM FOR STUDENT RECORD

#include<stdio.h>
#include<conio.h>
#include<string.h>

struct student
{
int regno;
char name[20];
int per;
char cla[10];
}s[10];

void main()
{
int i,n;
clrscr();
printf("Enter the number of the students: \n");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("Enter the Reg. No, Name, Percentage: \n");
scanf("%d%s%d",&s[i].regno,&s[i].name,&s[i].per);
}

for(i=0;i<n;i++)
{
if(s[i].per>60)
strcpy(s[i].cla,"FIRST CLASS");
else if(s[i].per>45&&s[i].per<60)
strcpy(s[i].cla,"SECOND CLASS");
else
strcpy(s[i].cla,"FAIL");
}

printf("\nREG.NO NAME PERCENTAGE CLASS \n");


printf("-----------------------------------------------------------------------\n");

for(i=0;i<n;i++)
{
printf("%d\t%s\t%d\t%s\n",s[i].regno,s[i].name,s[i].per,s[i].cla);
printf("-----------------------------------------------------------------------\n");
}
getch();
}
OUTPUT:

Enter the number of the students: 3


Enter the Reg. No, Name, Percentage:
001
ANAND
98

002
ASHOK
52

003
SUREN
40

REG.NO NAME PERCENTAGE CLASS


--------------------------------------------------------------------------
001 ANAND 98 FIRST CLASS
--------------------------------------------------------------------------
002 ASHOK 52 SECOND CLASS
--------------------------------------------------------------------------
003 YUVARAJ 40 FAIL
--------------------------------------------------------------------------
PROGRAM FOR SIZE OF UNION

#include<stdio.h>
#include<conio.h>

void main()
{
union result
{
int marks;
char grade;
};
struct res
{
char name[15];
int age;
union result perf;
}data;
clrscr();
printf("Size of union:%d\n",sizeof(data.perf));
printf("Size of structure:%d\n",sizeof(data));
getch();
}
OUTPUT:

Size of union: 2
Size of structure: 19
PROGRAM FOR MEMORY ALLOCATION

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<alloc.h>
#include<fstream.h>
#define NULL0

void main()
{
char*buffer;
clrscr();
//if(buffer==(char*)malloc(10)==NULL)
{
//printf("\nmemory allocation failed");
//exit(0);
}
printf("\nBuffer of size %d is created\n",sizeof(buffer));
strcpy(buffer,"bombay");
printf("\nBuffer contains:%s",buffer);
if(buffer==(char*)realloc(buffer,15)==NULL)
{
printf("\nreallocation failed");
exit(0);
}
printf("\nBuffer size is modified");
printf("\nBuffer still contains:%s",buffer);
strcpy(buffer,"mumbai");
printf("\nBuffer now contains:%s",buffer);
getch();
}
OUTPUT:

Buffer of size 2 is created


Buffer contains BOMBAY
Buffer size is modified
Buffer still contains: BOMBAY
Buffer now contains: MUMBAI
PROGRAM FOR CALL BY VALUE

#include<stdio.h>
#include<conio.h>
void swapping(int,int);
void main()
{
int a,b,c;
clrscr();
printf("\nProgram to swap two numbers\n");
printf("\nEnter the values of A and B: ");
scanf("%d%d",&a,&b);
printf("\nBefore Swapping A=%d,B=%d",a,b);
swapping(a,b);
getch();
}

void swapping(int x,int y)


{
int t;
t=x;
x=y;
y=t;
printf("\nAfter Swapping A=%d,B=%d",x,y);
}
OUTPUT:

Program to swap two numbers


Enter the values of A and B: 65
54
Before Swapping
A=65
B=54
After Swapping
A=54
B=65
PROGRAM FOR CALL BY REFERENCE

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nProgram to swap two numbers\n");
printf("\nEnter the values of A and B: ");
scanf("%d%d",&a,&b);
printf("\nBefore Swapping A=%d,B=%d",a,b);
swapping(&a,&b);
getch();
}
void swapping(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
printf("\nAfter Swapping A=%d,B=%d",*x,*y);

}
OUTPUT:

Program to swap two numbers


Enter the values of A and B: 65
54
Before Swapping
A=65
B=54
After Swapping
A=54
B=65

You might also like