0% found this document useful (0 votes)
6 views78 pages

C Programming Basic Algorithms and Functions

The document contains a collection of C programming code snippets for various tasks, including checking for perfect numbers, calculating factorials, matrix addition, sorting numbers, and more. Each section provides a brief description of the functionality, followed by the corresponding code. The document serves as a reference for basic programming concepts and implementations in C.
Copyright
© All Rights Reserved
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)
6 views78 pages

C Programming Basic Algorithms and Functions

The document contains a collection of C programming code snippets for various tasks, including checking for perfect numbers, calculating factorials, matrix addition, sorting numbers, and more. Each section provides a brief description of the functionality, followed by the corresponding code. The document serves as a reference for basic programming concepts and implementations in C.
Copyright
© All Rights Reserved
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

1.

PERFECT OR NOT

#include<stdio.h>
#include<conio.h>
void main()
{
int a,n,i,s=0;
clrscr();
printf("\n\nperfact or not\n");
printf("\nenter the number:\n");
scanf("%d",&n);
for(i=1;i<n;i++)
{a=n%i;
if(a==0)
s=s+i;
}
printf("\n\noutput\n");
if(s==n)
printf("the gfiven number is perfact\n");
else
printf("the given number is not perfact\n");
printf("\nent\n");
getch();
}
OUTPUT:
[Link]

#include<stdio.h>
#include<conio.h>
void main()
{
int i,s=1,n;
clrscr();
printf("\n \t\tfactorial \n\t\t*********\n");
printf("\n enter the number of finding factorial\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
s=s*i;
}
printf("\n\n\t\toutput\n\t\t**********");
printf("\nthe factorial of given number is,%d",s);
printf("\n\t\tEND\n\t\t*****");
getch();
}
OUTPUT:
[Link] ADDITION

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int a[10][10],b[10][10],c[10][10],n,m,j,p,q,i;
clrscr();
printf("\n\n\t\t..... MATRIXADDAITION......");
printf("\t\t********************************\n");
printf("enter the number of rows and colams for 1st matrix");
scanf("%d%d",&n,&m);
printf("enter the number of rows and coloums for 2nt matrix");
scanf("%d%d",&p,&q);
if(n==q)
{
printf("\nenter the 1st matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter the 2nd matrix:n\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
c[i][j]=0;
c[i][j]=a[i][j]+b[i][j];
}
printf("\n");
}
printf("\t out put");
printf("\t********");
printf("the answer is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
{
printf("your input is invalid\n");
}
printf("\tend\n");
printf("\t***\n");
getch();
}
OUTPUT:
4. ASCENDING ORDER USING POINTER

#include<stdio.h>
#include<conio.h>
void main()
{
int *pt[20],*t;
int i,j,n;
clrscr();
printf("\n ASCENDING ORDER \n");
printf("Enter the number\n");
scanf("%d",&n);
printf("Enter the number one by one:\n");
for(i=0;i<n;i++)
{
scanf("%d",&pt[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(pt[i]>pt[j])
{
t=pt[i];
pt[i]=pt[j];
pt[j]=t;
}
}
}
printf("\t\tOUTPUT:\n");
printf("ASCENDING ORDER IS \n");
for(i=0;i<n;i++)
{
printf("%d\n",pt[i]);
}
getch();
}
OUTPUT:
5. SUM OF DIGIT& REVERSE USING SWITCH CASE
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,ch,r,s=0;
printf("\n\t\t The Sum of Digits&Reverse using switch");
printf("\nEnter the number");
scanf("%d",&n);
printf("choose the program number");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
while(n>0)
{
r=n%10;
s=s+r;
n=n/10;
}
printf("\nThe Andwer is:%d",s);
break;
}
case 2:
{
while(n>0)
{
r=n%10;
s=(s*10)+r;
n=n/10;
}
printf("\n The Answer is: %d",s);
break;
}
default:
printf("The Number is Invalid");
break;
}
getch();
}
OUTPUT:
[Link] THE FILE

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
void main()
{
FILE*fp;
char fi[15];
clrscr();
system("[Link]");
printf("\n\tDELEDING THE FILE\n");
printf("\t*******************\n");
printf("\n\t enter the file name");
scanf("%s",fi);
fp=fopen(fi,"r");
if(fp==0)
{
printf("\nfile dose not exist");
}
else
{
remove(fi);
printf("\nfile has been deleted");
}
getch();
}
OUTPUT:
[Link] BILL USING STRUCTURE

#include<stdio.h>
#include<conio.h>
struct eb
{
int customerid;
char name[20];
char address[20];
int pu,cu,uc;
float amount;
char toc;
};
void main()
{
struct eb e;
clrscr();
printf("\nEnter the customer details\n");
scanf("%d",&[Link]);
printf("\n Enter the Name");
scanf("%s",&[Link]);
printf("\nEnter the Adress");
scanf("%sf",&[Link]);
printf("previous unit");
scanf("%d",&[Link]);
printf("Enter the current unit");
scanf("%d",&[Link]);
printf("Enter the Amount");
scanf("%f",&[Link]);
[Link]=[Link];
switch([Link])
{
case 'h':
[Link]=[Link]*3.50;
break;
case 's':
[Link]=[Link]*4.00;
break;
case 'c':
[Link]=[Link]*6.00;
break;
}
printf("\n\n");
printf("********eletric bill***\n\n");
printf("customerID :%d\n",[Link]);
printf("name :%s\n",[Link]);
printf("address :%s\n",[Link]);
printf("unit consumed :%d\n",[Link]);
printf("type of customer :%c\n",[Link]);
printf("amount :%2f\n",[Link]);
getch();
}
OUTPUT:
[Link] EQUATION

#include<stdio.h>
#include<conio.h>
void main()
{
int x,a,b,c;
clrscr();
printf("enter the a,b,c value\n");
scanf("%d\n%d\n%d",&a,&b,&c);
x=b*b-4*a*c/2*a;
if(x>0)
printf("the roots are real");
else
printf("the roots are imaginary");
getch();
}
OUTPUT:
[Link] SERIES

#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,i,n;
clrscr();
printf("\nFibonocci Seqince\n");
printf("\n""""""""""""""""""""\n");
printf("\nEnter the value of find fibanacci\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
c=a+b;
printf("The Fibonacci:");
printf("%d\n",c);
a=b;
b=c;
}
getch();
}
OUTPUT:
10. BINARY TO DECIMAL CONVERSION

#include<stdio.h>
void main()
{
int num,binary,decimal=0,basic=1,rem;
printf("Enter the binary number(1s and 0s)\n");
scanf("%d",num);
binary=num;
while(num>0)
{
rem=num%10;
decimal=decimal+(rem*basic);
num=num/10;
basic=basic*2;
}
printf("The Decimal Equalent is=%d \n",decimal);
getch();
}
OUTPUT:
FACTORIAL USING RECURSION.

#include<stdio.h>
long int factorial(int n)
{
if(n==1)
return 1;
return n*factorial(n-1);
}
int main()
{
int num;
long int fact=0;
clrscr();
printf("\n\nEnter the integer number:");
scanf("%d",&num);
fact=factorial(num);
printf("Factorial Of %d is=%d",num,fact);
printf("\n");
return 0;
getch();
}
OUTPUT:
12. ALPHEBETICAL ORDER

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
char a[10][10],t[10];
clrscr();
printf("enter the number of element \n");
scanf("%d",&n);
printf("enter the name one by one\n");
for(i=0;i<n;i++)
{
scanf("%s",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(a[i],a[j])>0)
{
strcpy(t,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],t);
}
}
}
printf("the out put is\n");
for(i=0;i<n;i++)
printf("%s\n",a[i]);
getch();
}
OUTPUT:
14. ARMSTRONG NUMBER

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,n1,s=0;
clrscr();
printf("\n\*******armstrong or not*******");
printf("\n enter the value for n");
scanf("%d",&n);
n1=n;
while(n>0)
{
r=n%10;
s=s+(r*r*r);
n=n/10;
}
printf("\n\n\t\t****out put*******\n\n");
printf("the anwer is %d",s);
if(n1==s)
{
printf("\n the given number is armstrong\n");
}
else
{
printf("\nthe given number is not armstrong\n");
}
printf("\n\n*******END********\n");
getch();
}
OUTPUT:
15. STRING PALINDROME

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j=0;
char st1[10],st2[10];
clrscr();
printf("Enter any string\n");
scanf("%s",st1);
i=strlen(st1);
for(i=i-1;i>=0;i--)
{
st2[j++]=st1[i];
st2[j]=0;
}
if(strcmp(st2,st1)==0)
printf("%s is palindrome\n",st1);
else
printf("%s is not palindrome\n",st2);
getch();
}
OUTPUT:
[Link] OR NOT ADAM

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int m,n,s=0,r,a,b,y;
clrscr();
printf("\n\n\t adam or not\n");
printf("enter the number\n");
scanf("%d",&n);
m=n;
a=n*n;
while(a>0)
{
r=a%10;
s=(s*10)+r;
a=a/10;
}
b=sqrt(s);
s=0;
while(b>0)
{
y=b%10;
s=(s*10)+y;
b=b/10;
}
printf("\n\n\t output");
printf("\n the answer is %d \n",s);
if(m==s)
printf("the given number is adam\n\n");
else
printf("\nthegiven number is not adam");
getch();
}

OUTPUT:
[Link] DETAILS USING FUNCTION

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct employee
{
int empid;
char name[32];
int basic,hra,da,ma;
int bf,insurence;
float gross,net;
};
void printsalary(struct employee e1)
{
printf("Salary flip of %s:",[Link]);
printf("employee ID of %d:",[Link]);
printf("Basic Salary:%d \n",[Link]);
printf("House rent allowance: %d\n",[Link]);
printf("Dearness allowance: %d \n",[Link]);
printf("Medical Allowance:%d \n",[Link]);
printf("gross Salary: %2f rupees \n",[Link]);
printf("\n Deduction: \n");
printf("Provident Fund:%d",[Link]);
printf("Insurence: % \n",[Link]);
printf("\n Net Salary: %2f rupees \n\n",[Link]);
return;
}
int main()
{
int i,ch,num,flag,empid;
struct employee*e1;
printf("Enter the Number of Employees:");
scanf("%d",&num);
e1=(struct employee *)malloc(sizeof(struct employee)*num);
printf("Enter your input value for every Employee:\n");
for(i=0;i<num;i++)
{
printf("Employee Id:");
scanf("%d",&(e1[i].empid));
getchar();
printf("Employee Name:");
fgets(e1[i].name,32,stdin);
e1[i].name[strlen(e1[i].name)-1]='\0';
printf("Basic Salary,hra:");
scanf("%d %d",&(e1[i].basic),&(e1[i].hra));
printf("da,Medical Allowance:");
scanf("%d %d",&(e1[i].da),&(e1[i].ma));
printf("pf and Insurence:");
scanf("%d %d",&(e1[i].bf),&(e1[i].insurence));
printf("\n");
}
for(i=0;i<num;i++)
{ e1[i].gross=e1[i].basic+(e1[i].hra*e1[i].basic)/100+(e1[i].da*e1[i].basic)/100+
(e1[i].ma*e1[i].basic)/100;
e1[i].net=e1[i].gross-(e1[i].bf+e1[i].insueance);
}
while(10
printf("enter EMPLOYEE ID to gett payslip:");
scanf("%d",&empid);
flag=0;
for(i=0;i<num;i++)
{
if(empid==e1[i].empid)
{
printsalary(e1[i]);
flag=1;
}
}
if(!flag)
{
printf("norecord found!!\n");
}
printf("do you want to continue(1/0)");
scanf("%d",&ch);
if(!ch)
{
break;
}
}
return 0;
}

OUTPUT:
[Link] POSITIVE,NEGATIVE & ZERO

#include<stdio.h>
void main()
{
int x[20],p,n,z,i;
p=0;n=0;z=0;
clrscr();
printf("\nEnter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("Enter the number");
{
scanf("%d",&x[i]);
}
for(i=1;i<=n;i++)
{
if(x[i]>0)
p++;
else if(x[i]<0)
n++;
else
z++;
}
printf("\nPositve number:%d\n",p);
printf("\nNegative number %d\n",n);
printf("\nZeros%d",z);
getch();
}
OUTPUT:
19. CONVERSION OF LETTERCASE

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char up[10],low[10];
clrscr();
printf("\n\n\nEnter the lowercase letter\n");
gets(low);
printf("The UPPERCASE LETTER is %s",strupr(low));
printf("\nEnter the uppercase letter\n");
gets(up);
printf("The LOWERCASE is %s",strlwr(up));
getch();
}
OUTPUT:
[Link] LETTERS,DIGITS&ALPHA NUMERICS CHARECTERS

#include<stdio.h>
int main()
{
char str[100];
int digit,alphabet,special,space;
int count;
clrscr();
digit=alphabet=special=space=0;
printf("\n\nEnter a Any Number & String:");
gets(str);
for(count=0;str[count]!=NULL;count++)
{
if(str[count]>='0'&&str[count]<='9')
digit++;
else if((str[count]>='A'&&str[count]<='Z')||(str[count]<='a'&&str[count]<='z'))
alphabet++;
else if(str[count]==' ')
space++;
else
special++;
}
printf("\n Digits:%d \n Alphabet: %d \nSpace:%d \n Special Charecter:
%d",digit,alphabet,space,special);
return 0;
getch();
}
OUTPUT:
[Link] TABLE

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n,a,c;
clrscr();
printf("Multiplication table");
printf("Enter the n value");
scanf("%d",&n);
printf("\nEnter the multiplier");
scanf("%d",&a);
for(i=1;i<=n;i++)
{
c=i*a;
printf("\n%d*%d=%d",i,a,c);
}
getch();
}
OUTPUT:
[Link] OR NOT

#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,n,i;
clrscr();
printf("\n\t\t prime or not\n");
printf("\n\t\t""""""""""""""\n");
printf("enter the number");
scanf("%d",&n);
printf("\n\n output\n");
for(i=2;i<+n;i++)
{if(n%i==0)
a=a++;
i++;
}
if(a==0)
{
printf("\nthe given number is prime");
}
else
{
printf("the given number is not prime");
}
printf("\n\n\t\t end ");
getch();
}
OUTPUT:
23. DECENDING ORDER

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int i,j,n,t;
clrscr();
printf("\n DECENDING ORDER \n");
printf("Enter the number\n");
scanf("%d",&n);
printf("Enter the number one by one:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i-1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("\t\tOUTPUT:\n");
printf("DECENDING ORDER IS \n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
OUTPUT:
[Link] MULTIPLICATION

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int a[10][10],b[10][10],c[10][10],n,m,j,p,q,i;
clrscr();
printf("\n\n\t\t..... MATRIXmultiplication......");
printf("\n\t\t********************************\n");
printf("enter the number of rows and colams for 1st matrix\n");
scanf("%d%d",&n,&m);
printf("enter the number of rows and coloums for 2nt matrix\n");
scanf("%d%d",&p,&q);
if(n==q)
{
printf("\nenter the 1st matrix:\n^^^^^^^^^^^^^^^^^^^^^^\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter the 2nd matrix:\n^^^^^^^^^^^^^^^^^^^^^^^^^\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
c[i][j]=0;
c[i][j]=a[i][j]*b[i][j];
}
printf("\n");
}
printf("\t out put");
printf("\n\t********");
printf("\nthe answer is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
{
printf("your input is invalid\n");
}
printf("\tend\n");
printf("\t***\n");
getch();
}
OUTPUT:
25. AVERAGE OF TWO NUMBER USING POINTERS

#include<stdio.h>
#include<conio.h>
void main()
{
int n,*p,sum=0,i;
float avg;
clrscr();
printf("\n\n How many numbers:");
scanf("%d",&n);
p=(int*)malloc(n*2);
if(p==NULL)
{
printf("\n Memory Allocation unsuccessful");
exit();
}
for(i=0;i<n;i++)
{
printf("\n Enter number %d:",i+1);
scanf("%d",(p+i));
}
for(i=0;i<n;i++)
{
sum=sum+*(p+i);
avg=(float)sum/n;
}
printf("\n The Average Of the Number is %0.2f",avg);
getch();
}
OUTPUT:
[Link] WHETHER THE GIVEN NUMBER IS

PRESENT OR NOT

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,m,flag=0;
int a[10];
clrscr();
printf("\n\nHow many elements you want to enter");
scanf("%d",&n);
printf("Enter element in the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enterthe element you want to search\n");
scanf("%d",&m);
for(i=0;i<n;i++)
{
if(a[i]==m)
{
flag=1;
break;
}
}
if(flag==0)
printf("Not Present");
else
printf("\nPresent");
getch();
}
OUTPUT:
27. CALCULATE NCR & NPR

#include<stdio.h>
#include<conio.h>
long int fact(int);
void main()
{
int n,r;
long int ncr,npr;
clrscr();
printf("\n\nEnter the value of N:");
scanf("%d",&n);
printf("\n\nEnter the value of R:");
scanf("%d",&r);
npr=fact(n)/fact(n-r);
ncr=npr/fact(r);
printf("\n NPR value=%1d\n",npr);
printf("NCR value=%1d \n",ncr);
getch();
}
long int fact(int x)
{
int i,f=1;
for(i=2;i<=x;i++)
{
f=f*i;
}
return f;
}
OUTPUT:
28. DRAW A PYRAMID

#include<stdio.h>
#define max 5
int main()
{
int i,j;
int space=4;
for(i=0;i< max;i++)
{
for(j=0;j< space;j++)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
printf("* ");
}

printf("\n");
space--; /* decrement one space after one row*/
}
return 0;
}
OUTPUT:
29. ELSE IF LADDER

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the three value\n");
scanf("%d%d%d",&a,&b,&c);
{
if((a>b)&&(a>c))
printf("Biggest number is a");
else if((b>a)&&(b>c))
printf("\n The given number is b");
else if((c>a)&&(c>b))
printf("\n Biggest number is c");
}
getch();
}
OUTPUT:
30. COUNT NUMBER OF CHARACTER &WORDS

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
int c,i,d=1;
char s[100];
clrscr();
printf("cout the number of words and character \n");
printf("**************************************\n");
printf("enter the string\n");
gets(s);
for(i=0;s[i]!='\0';i++)
for(c=0;c<i;c++)
{
if(s[c]==' ')
d=d+1;
}
printf("\nthe total number of characters=%d",i);
printf("\nthe total number of words=%d",d);
getch();
}
OUTPUT:
31. STUDENT MARKLIST USING ARRAY&STRUCTURE

#include<stdio.h>
#include<conio.h>
struct stud
{
int roll,s1,s2,tot;
char name[10];
float avg;
}s[10];
void main()
{
int i,n;
clrscr();
printf("\n\nEnter the Number of Student:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Roll No:");
scanf("%d",&s[i].roll);
printf("\nEnter the Name:");
scanf("%s",s[i].name);
printf("Enter the marks in 2 Subjects:");
scanf("%d %d",&s[i].s1,&s[i].s2);
s[i].tot=s[i].s1+s[i].s2;
s[i].avg=s[i].tot/2.0;
}
printf("\n rollno\tname\t\tsub1\t\tsub2\t total \taverage\n\n");
for(i=0;i<n;i++)
{
printf("%d\t%s\t\t%d\t\t%d\t%d\t%.2f\
n",s[i].roll,s[i].name,s[i].s1,s[i].s2,s[i].tot,s[i].avg);
}
getch();
}
OUTPUT:
[Link] A PROGRAM FOR RELATIONAL OPERATOR

#include<stdio.h>
#include<conio.h>
int main()
{
int num1,num2;
clrscr();
printf("\n\nEnter the INTEGERS");
scanf("%d %d",&num1,&num2);
if(num1==num2)
{
printf("Result =%d=%d",num1,num2);
}
else if(num1>num2)
{
printf("Result =%d>%d",num1,num2);
}
else
{
printf("Result =%d<%d",num1,num2);
}
return 0;
getch();
}
OUTPUT:
[Link] POWER OF 2

#include<stdio.h>
int main()
{
int num;
int temp,flag;
printf("\n\nEnter an Integer Number:");
scanf("%d",&num);
temp=num;
flag=0;
while(temp!=1)
{
if(temp%2!=0)
{
flag=1;
break;
}
temp=temp/2;
}
if(flag==0)
printf("%d Is a number that is the Power of 2.",num);
else
printf("%d Is not the number power of 2.",num);
return 0;
getch();
}
OUTPUT:
34.C PROGRAM TO PRINT THE
NUMBER PYRAMID

#include<stdio.h>
int main()
{
int i,j,k,l=1;
clrscr();
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
printf(" ");
}

for(k=1; k<=l; k++)


{
printf("%d",k);
}
l = l+2;
printf("\n");
}
return 0;
}
OUTPUT:

You might also like