0% found this document useful (0 votes)
66 views

Submitted To Submitted by

The document contains 18 C programming code examples with descriptions. Some of the examples include: 1. Finding the largest of three numbers using conditional operators. 2. Checking if a number is a palindrome. 3. Calculating the square and cube of a given number using functions. 4. Checking if a given number is prime. 5. Finding prime numbers up to 100. 6. Calculating the LCM and HCF of two numbers using functions. 7. Printing the multiplication table of a number using recursion.

Uploaded by

prashanth
Copyright
© © All Rights Reserved
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)
66 views

Submitted To Submitted by

The document contains 18 C programming code examples with descriptions. Some of the examples include: 1. Finding the largest of three numbers using conditional operators. 2. Checking if a number is a palindrome. 3. Calculating the square and cube of a given number using functions. 4. Checking if a given number is prime. 5. Finding prime numbers up to 100. 6. Calculating the LCM and HCF of two numbers using functions. 7. Printing the multiplication table of a number using recursion.

Uploaded by

prashanth
Copyright
© © All Rights Reserved
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
You are on page 1/ 34

Submitted To

Submitted By

/********************************************************************
1.LARGEST NUMBER IN THREE NUMBERS
USING CONDITIONAL OPERATOR
********************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,l;
clrscr();
printf("ENTER THREE VALUES\n");
scanf("%d%d%d",&a,&b,&c);
l=a>b&&a>c?a:(b>c)?b:c;
printf("LARGEST VALUE=%d",l);
getch();
}
OUTPUT

/***********************************************************************
2.CHECK A NUMBER IS PELIDROM OR NOT
***********************************************************************/
#include <stdio. h>
#include < conic. h>
void main()
{
int a, no, b, temp =0;
clrscr ();
printf("ENTER ANY NUMBER = ");
scanf("%d", &no);
b=no;
while (no>0)
// if num is grater than 0 (zero)
{
a=no%10;
no=no/10;
temp=temp*10+a;
}
if(temp==b)
{
printf("\n\n%d IS PELIDROM NUMBER",b);
}
else
{
printf("\n\n%d IS NOT PELIDROM NUMBER",b);
}
getch();
}

OUTPUT

/********************************************************************
3.FIND SQURE AND CUBE OF A GIVEN NUMBER
********************************************************************/
#include<stdio.h>
#include<conio.h>
int squre(int);
int qube(int);
void main()
{
int num;
clrscr();
printf("ENTER ANY NUMBER ");
scanf("%d",&num);
printf("\nSQURE OF %d IS = %d",num,squre(num));
printf("\n\nCUBE OF %d IS = %d",num,qube(num));
getch();
}
int squre(int num)
{
int ans;
ans=num*num;
return(ans);
}
int qube(int num)
{
int ans;
ans=num*num*num;
return(ans);
getch();
}

OUTPUT

/***********************************************************************
4.CHECK A GIVEN NUMBER IS PRIME OR NOT
***********************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,no,prime=0;
clrscr();
textcolor(4);
gotoxy(20,10);
cprintf("ENTER ANY NUMBER ");
scanf("%d",&no);
if(no==1)
{
gotoxy(20,12);
printf("%d IS NOT A PRIME NUMBER ",no);
prime=1;
}
for(i=2;i<no;i++)
{
if(no%i==0)
{
gotoxy(20,12);
printf("%d IS NOT A PRIME NUMBER",no);
prime=1;
}
}
if(prime==0)
{
gotoxy(20,12);
printf("%d IS A PRIME NUMBER",no);
}
getch();
}

OUTPUT

/********************************************************************
5.PRIME NUMBER UP TO 100
********************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,hitesh=1;
clrscr();
textcolor(2);
cprintf("PRIME NUMBER UP TO 100\n");
for(i=2;i<=100;i++)
{
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
hitesh=0;
break;
}
else
hitesh=1;
}
if(hitesh==1)
printf("%d\n",i);
}
getch();
}

OUTPUT

/********************************************************************
6.FIND THE LCM OF ANY TWO NUMBER
********************************************************************/
#include<stdio.h>
#include<conio.h>
void lcm(int,int);
void main()
{
int a,b;
clrscr();
printf("EANY TWO NUMBER: \t");
scanf("%d %d",&a,&b);
lcm(a,b);
getch();
}
//function to calculate l.c.m
void lcm(int a,int b)
{
int m,n;
m=a;
n=b;
while(m!=n)
{
if(m<n)
{
m=m+a;
}
else
{
n=n+b;
}
}
printf("\nL.C.M OF %d & %d IS %d",a,b,m);
}

OUTPUT

/********************************************************************
7.FIND THE HCF OF TWO NUMBER
********************************************************************/
#include<stdio.h>
#include<conio.h>
void gcd(int,int);
void main()
{
int a,b;
clrscr();
printf("ENTER ANY TWO NUMBER : \t");
scanf("%d %d",&a,&b);
gcd(a,b);
getch();
}
//function to calculate g.c.d
void gcd(int a,int b)
{
int m,n;
m=a;
n=b;
while(m!=n)
{
if(m>n)
{
m=m-n;
}
else
{
n=n-m;
}
}
printf("\nG.C.D OF %d & %d IS %d",a,b,m);
}

OUTPUT

/***********************************************************************
8.FIND TABLE FO A GIVEN NUM. USING RECURSION
***********************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int table(int,int);
int n,i;
clrscr();
printf("Enter any num = ");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
printf(" %d*%d= %d\n",n,i,table(n,i));
}
getch();
}
int table(n,i)
{
int t;
if(i==1)
{
return(n);
}
else
{
return(table(n,i-1)+n);
}
}

OUTPUT

/********************************************************************
9.PRINT 100 FIBONOCII SERIES
********************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
double i,twoaway,next,current;
clrscr();
current=0;
next=1;
for(i=1;i<=100;i++)
{
printf(" %lf",current);
twoaway=next+current;
current=next;
next=twoaway;
}
getch();
}
OUTPUT

/********************************************************************
10.ENTER TWO ARRAY AND MERGE IN 3RD ARRAY
********************************************************************/
#include<stdio.h>
#include<conio.h>
void merge_array(int a[],int b[]);
void main()
{
int a[10],b[10],i,n;
clrscr();
printf("ENTER ELEMENT IN 1ST ARRAY ");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("ENTER ELEMENT IN 1ST ARRAY ");
for(i=0;i<10;i++)
{
scanf("%d",&b[i]);
}
merge_array(a,b);
getch();
}
void merge_array(int a[],int b[])
{
int c[20],i;
printf("\n\nELEMENT OF 1ST ARRAY");
for(i=0;i<10;i++)
{
printf(" %d",a[i]);
}
printf("\n\nELEMENT of 2ND ARRAY");
for(i=0;i<10;i++)
{
printf(" %d",b[i]);
}
printf("\n\nELEMENT OF ARRAY AFTER MERGE ");
for(i=0;i<10;i++)
{
c[i]=a[i];
}
for(i=0;i<10;i++)

{
c[i+10]=b[i];
}
for(i=0;i<20;i++)
{
printf(" %d",c[i]);
}
}
OUTPUT

/***********************************************************************
11.PRINT REVERSE OF A GIVEN NUMBER
***********************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int no,rev=0,r,a;
clrscr();
textcolor(10);
cprintf("\n enter the number = ");
scanf("%d",&no);
a=no;
while(no>0)
{
r=no%10;
rev=rev*10+r;
no=no/10;
}
cprintf("\n\n reverse of %d is = %d", a, rev);
getch();
}
OUTPUT

/***********************************************************************
12. PASS ARRAY FROM ONE FUNCTION TO ANOTHER
***********************************************************************/
#include<stdio.h>
#include<conio.h>
void pass(int[],int);
void main()
{
int a[]={1,2,3,4,5};
clrscr();
pass(a,5);
getch();
}
void pass(int b[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf(" %d\n",b[i]);
}
}

OUTPUT

/***********************************************************************
13.SORT THE ELEMENT OF ARRAY IN ASSENDING ORDER
***********************************************************************/
#include<stdio. h>
#include<conio. h>
#include<dos. h>
void main()
{
int i,a[10],temp,j;
clrscr();
printf("Enter any 10 num in array = \n");
for(i=0;i<=10;i++)
{
scanf("%d",&a[i]);
}
printf("\n\nData before sorting = ");
for(j=0;j<10;j++)
{
delay(200);
printf(" %d",a[j]);
}
for(i=0;i<=10;i++)
{
for(j=0;j<=10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n\n\nData after sorting = ");
for(j=0;j<10;j++)
{
delay(200);
printf(" %d", a[j]);
}
getch();
}

OUTPUT

/
********************************************************************14.C
OMB THE TWO STRING AND PRINT IN UPP CASE
********************************************************************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *ch1="hitesh ";
char *ch2="kumar";
char *ptr;
clrscr();
printf("\n\n1 ST STRING = %s",ch1);
printf("\n\n2 ND STRING = %s",ch2);
strcat(ch1,ch2);
printf("\n\nSTRING IS = %s",ch1);
ptr=strupr(ch1);
printf("\n\nIN UPPER CASE = ");
printf("%s",ptr);
getch();
}
OUTPUT

/********************************************************************
15. ADDITION OF TWO MATRIX
********************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int x[3][3],y[3][3],z[3][3],i,j;
clrscr();
printf("ENTER ELEMENTS OF 1st MATRIX\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&x[i][j]);
}
printf("ENTER ELEMENTS OF 2nd MATRIX\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&y[i][j]);
}
printf("MATRIX [X]");
for(i=0;i<3;i++)
{
printf("\n\n");
for(j=0;j<3;j++)
printf(" %d",x[i][j]);
}
printf("\nMATRIX [Y]");
for(i=0;i<3;i++)
{
printf("\n\n");
for(j=0;j<3;j++)
printf(" %d",y[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
z[i][j]=x[i][j]+y[i][j];
}
printf("\nMATRIX [Z]");
for(i=0;i<3;i++)
{

printf("\n\n");
for(j=0;j<3;j++)
printf(" %d",z[i][j]);
}
getch();
}

OUTPUT

/********************************************************************
16.CALCULATE THE BONUS OF EMPLOYEE A/C
TO BASIC SALARY
********************************************************************/
#include<stdio.h>
#include<conio.h>
struct employee
{
char emp_name[20],emp_grad[2];
int emp_bs,emp_code;
float emp_bonus;
};
void main()
{
struct employee emp;
char g;
int hitesh=0;
clrscr();
printf("Enter employee name = ");
gets(emp.emp_name);
printf("Enter employee code = ");
scanf("%d",&emp.emp_code);
printf("Enter Basic salary = ");
scanf("%d",&emp.emp_bs);
printf("Enter Grad of employee = ");
g=getch();
printf("%c",g);
if(g=='a'|| g=='A')
{
emp.emp_bonus=emp.emp_bs*0.5;
printf("\n\nBONUS %f = ",emp.emp_bonus);
hitesh=1;
}
if(g=='b'|| g=='B')
{
emp.emp_bonus=emp.emp_bs*0.1;
printf("\n\nBONUS %f = ",emp.emp_bonus);
hitesh=1;
}
if(g=='c'|| g=='C')
{

emp.emp_bonus=emp.emp_bs*0.02;
printf("\n\nBONUS %f = ",emp.emp_bonus);
hitesh=1;
}
if(hitesh==0)
{
printf("\n\nNo bonus");
}
getch();
}

OUTPUT

/********************************************************************
17.FIND FACTORIAL OF A GIVEN NUM USING RECURS
********************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int fact(int);
int f,n;
clrscr();
printf("ENTER ANY NUMBER = ");
scanf("%d",&n);
f=fact(n);
printf("\n\nFACTORIAL OF %d IS = %d",n,f);
getch();
}
int fact(int a)
{
if(a==1)
return(1);
else
{
return(a*fact(a-1));
}
}
OUTPUT

/********************************************************************
18.COPY THE FIRST STRING INTO SECOND STRING
********************************************************************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void string(char *ch1 ,char *ch2);
void main()
{
char *ch1[5];
char *ch2="kumar";
clrscr();
printf("\n\nSTRING = %s",ch2);
string(ch1,ch2);
getch();
}
void string(char *ch1 ,char *ch2)
{
strcpy(ch1,ch2);
strupr(ch1);
printf("\n\nSTRING IS = ");
printf("%s",ch1);
}

OUTPUT

/***********************************************************************
19.FIND THE ASCII VALUE OF A GIVEN NUMBER
***********************************************************************/
#include <stdio.h>
#include <ctype.h>
#include<conio.h>
int main(void)
{
int number, result;
clrscr();
textcolor(2);
cprintf("ENTER ANY NUMBER = ");
number = getch();
printf("%c",number);
result = toascii(number);
printf("\n\nASCII VALUE OF %c IS %d\n", number, result);
getch();
return 0;
}
OUTPUT

/*-------20 APPENDING DATA TO AN EXISTING FILE -----*/

#include <stdio.h>
#include <conio.h>
struct invent_record
{
char name[10];
int number;
float price;
int quantity;
};
void main()
{
struct invent_record item;
char filename[10];
int response;
FILE *fp;
long n;
void append (struct invent_record *x, FILE *y);
clrscr();
printf("Type filename:");
scanf("%s", filename);
fp = fopen(filename, "a+");
do
{
append(&item, fp);
printf("\nItem %s appended.\n",item.name);
printf("\nDo you want to add another item\
(1 for YES /0 for NO)?");
scanf("%d", &response);
} while (response == 1);
fseek(fp,0,SEEK_END); /*Set the file pointer at the end of file*/
n=ftell(fp);
/* Position of last character */
fclose(fp);
fp = fopen(filename, "r");
while(ftell(fp) < n)
{
fscanf(fp,"%s %d %f %d",
item.name, &item.number, &item.price, &item.quantity);

fprintf(stdout,"%-8s %7d %8.2f %8d\n",


item.name, item.number, item.price, item.quantity);
}
fclose(fp);
getch();
}
void append(struct invent_record *product, FILE *ptr)
{
printf("Item name:");
scanf("%s", product->name);
printf("Item number:");
scanf("%d", &product->number);
printf("Item price:");
scanf("%f", &product->price);
printf("Quantity:");
scanf("%d", &product->quantity);
fprintf(ptr, "%s %d %.2f %d",
product->name,
product->number,
product->price,
product->quantity);
}
OUTPUT

You might also like