Sie sind auf Seite 1von 31

//print hello

#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
printf("Hello");
getch();
}
program to perform all arithmetic operations
#include<stdio.h>
#include<conio.h>
float a,b;
void main()
{
printf("enter no.\n");
scanf("%f%f",&a,&b);
printf("Sum is : %2.2f\n",a+b);
printf("Difference is : %2.2f\n",a-b);
printf("Multiplication is : %2.2f\n",a*b);
printf("Dicision is
: %2.2f",a/b);
getch();
}
------------------------------------------------------Swapping using 3 variable
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
printf("Enter first variables \n");
scanf("%d",&a);
printf("Enter second varibles \n");
scanf("%d",&b);
printf("before swapping numbers are :\n a = %d\n b = %d",a,b);
c=a;
a=b;
b=c;
printf("\nAfter swapping numbers are :\n a = %d\n b = %d",a,b);
getch();
}
output:
Enter first variables
1
Enter second varibles

2
before swapping numbers are :
a=1
b=2
After swapping numbers are :
a=2
b=1
Swapping using 2 variable
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
printf("Enter first variables \n");
scanf("%d",&a);
printf("Enter second varibles \n");
scanf("%d",&b);
printf("before swapping numbers are :\n a = %d\n b = %d",a,b);
a = a+b;
b = a-b;
a = a-b;
printf("\nAfter swapping numbers are :\n a = %d\n b = %d",a,b);
getch();
}
//output:
Enter first variables
1
Enter second varibles
2
before swapping numbers are :
a=1
b=2
After swapping numbers are :
a=2
b=1
program to compare two no.(if)
#include<stdio.h>
#include<conio.h>
int a,b;
void main()

{
printf("enter no.");
scanf("%d%d",&a,&b);
if(a==b)
{
printf("no. are equal");
}
getch();
}
program to compare two no.(if, else)
#include<stdio.h>
#include<conio.h>
int a,b;
void main()
{
printf("enter no.");
scanf("%d%d",&a,&b);
if(a==b)
{
printf("no. are equal");
}
else
{
printf("no. are not equal");
}
}
getch();
}
program to compare two no.(if, else)
#include<stdio.h>
#include<conio.h>
int a,b;
void main()
{
printf("enter no.");
scanf("%d%d",&a,&b);
if(a==b)
{
printf("no. are equal");
}
else if (a>b)
{

printf("%d is greater",a);
}
else
{
printf("%d is greater",b);
}
getch();
}
program to compare two no.using nested if (if, else if)
#include<stdio.h>
#include<conio.h>
int a,b;
void main()
{
printf("enter no.");
scanf("%d%d",&a,&b);
if(a==b)
{
printf("no. are equal");
}
else
{
if(a>b)
{
printf("%d is greater",a);
printf("\n%d is least",b);
}
else
{
printf("%d is greater",b);
printf("\n%d is least",a);
}
}
getch();
}
program to print nos. from 1 to 10*/
#include<stdio.h>
#include<conio.h>
int i;
int main()
{

printf("nos. from 1 to 10\n");


for(i=1;i<=10;i++)
{
printf("%d\n",i);
}
printf("\n");
getch();
}
program to print odd nos.
#include<stdio.h>
#include<conio.h>
int i;
int main()
{
printf(" odd nos.\n");
for(i=1;i<=10;i=i+2)
{
printf("%d\n",i);
}
printf("\n");
getch();
}
Output:
odd nos.
1
3
5
7
9
program to impliment switch case
#include<stdio.h>
#include<conio.h>
#include<process.h>
int i;
int main()
{
printf("enter i");
scanf("%d",&i);
switch(i)
{

case 1:
printf("i am in case 1");
break;
case 2:
printf("i am in case 2");
break;
case 3:
printf("i am in case 3");
break;
default:
exit(0);
}
getch();
}
goto
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("\tenter the value of a");
scanf("%d",&a);
printf("\tenter the value of b");
scanf("%d",&b);
if(a>b)
{
goto statement1;
}
else
{
goto statement2;
}
statement1:
printf("\tgreater value is a that is %d",a);
goto end;
statement2:
printf("\tgreater value is b that is %d",b);
end:
getch();
}
continue
#include<stdio.h>
#include<conio.h>

void main()
{
clrscr();
for(int i = 1 ; i<=10 ;i++)
{
if(i == 6)
{
continue;
}
printf("\nthe value of i is %d",i);
}
getch();
}
Program to calculate average using array
#include<stdio.h>
#include<conio.h>
int main()
{
int array[5],i,n,sum=0;
float avg;
printf("How much numbers you want to be averaged : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
sum=sum+array[i];
}
avg=sum/n;
printf("Average=%2.2f\n",avg);
getch();
}
program for array sorting (deceding order)
#include<stdio.h>
#include<conio.h>

long int x[20],i,j,temp;


int main()
{
printf("\t\t\t Enter five numbers \n");
for(i=0;i<5;i++)
scanf("%lu",&x[i]);
printf("\t\t\tBefore sorting \n");
for(i=0;i<5;i++)
printf("%lu\n",x[i]);
//***********Descending sorting********
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(x[i]>x[j])
{
/* if we want it Ascending then
only change value of condition in
if statement change it to "if(array[i]<array[j]"
*/
temp=x[i];
// swaping
x[i]=x[j];
// swaping
x[j]=temp;
// swaping
}
printf("\t\t\tAfter sorting \n");
for(i=0;i<5;i++)
printf("%lu\n",x[i]);
getch();
}
program to calculate factorial
#include<stdio.h>
#include<conio.h>
long int n,i,f=1;
int main()
{
printf("Enter number : ");
scanf("%ld",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("Factorial is : %ld",f);

getch();
return(0);
}
program to calculate factorial using recursive function
#include<stdio.h>
#include<conio.h>
long int n,f;
long int fac(long int n);
int main()
{
printf("Enter number whom you want factorial : ");
scanf("%ld",&n);
f=n*fac(n-1);
printf("factorial is %ld",f);
getch();
return 0;
}
long int fac(long int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
else
return n*fac(n-1);
}
Program to Print Fibonacci series
#include<stdio.h>
#include<conio.h>
int main()
{
int i,k=1,j,sum=0,n;
printf("enter limit : ");
scanf("%d",&n);
printf("sequence is :-\n");
for(i=1;i<=n;i++)
{
j=k;

k=sum;
sum=k+j;
printf("%d,",sum);
}
getch();
}
Output:
enter limit : 13
sequence is :1,1,2,3,5,8,13,21,34,55,89,144,233,
program to convert tempreture from Farenheit to Celcius or Celcius to Fahrenheit
#include<stdio.h>
#include<conio.h>
int choice,a;
float f,fare,c,cel;
void ftoc();
void ctof();
int main()
{
printf("\t\t\tTempreture Calculator\n");
printf("\t\tPress 1. to Convert Farenheit to Celcius\n");
printf("\t\tPress 2. to Convert Celcius to Farenheit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
ftoc();
break;
case 2:
ctof();
break;
}
getch();
}
void ftoc()
{
printf("Enter Tempreture in Farenheit : ");
scanf("%f",&f);
cel=(f-32)*5/9;

printf("Tempreture in Celcius : %2.2f\n",cel);


}
void ctof()
{
printf("Enter Tempreture in Celcius: ");
scanf("%f",&c);
fare=c*9/5+32;
printf("Tempreture in Farehite : %2.2f\n",fare);
}
Call by value
program to check that call by value cahnges values of given integers or not*/
#include<stdio.h>
#include<conio.h>
int a,b,t;
int swap(int x, int y)
{
t=x;
x=y;
y=t;
printf("\n\nvalue of x,y\n\n");
printf("x is %d\ny is %d\n",x,y);
}
int main()
{
printf("enter number \n");
scanf("%d%d",&a,&b);
printf("value of a,b before swaping\n");
printf("a is %d\nb is %d\n",a,b);
swap(a,b);
printf("\n\nvalue of a,b after swaping\n");
printf("a is %d\nb is %d\n",a,b);
getch();
}
Output:
enter number
1
2
value of a,b before swapinf
a is 1
b is 2
value of x,y
x is 2

y is 1
value of a,b after swapinf
a is 1
b is 2
call by reference: example1:
program to check that call by reference cahnges values of given integers or not*/
#include<stdio.h>
#include<conio.h>
int a,b,t;
void swap(int &x, int &y) //
{
t=x;
x=y;
y=t;
printf("\n\nvalue of x,y\n\n");
printf("x is %d\ny is %d\n",x,y);
}
int main()
{
printf("enter number \n");
scanf("%d%d",&a,&b);
printf("value of a,b before swaping\n");
printf("a is %d\nb is %d\n",a,b);
swap(a,b);
printf("\n\nvalue of a,b after swaping\n");
printf("a is %d\nb is %d\n",a,b);
getch();
}
Output:
enter number
1
2
value of a,b before swaping
a is 1
b is 2
value of x,y
x is 2
y is 1
value of a,b after swaping

a is 2
b is 1
call by reference: example2:
program to check that call by reference cahnges values of given integers or not*/
#include<stdio.h>
#include<conio.h>
int a,b,t;
int swap(int *x, int *y) //
{
t=*x;
*x=*y;
*y=t;
printf("\n\nvalue of x,y\n\n");
printf("x is %d\ny is %d\n",*x,*y);
}
int main()
{
printf("enter number \n");
scanf("%d%d",&a,&b);
printf("value of a,b before swaping\n");
printf("a is %d\nb is %d\n",a,b);
swap(&a,&b);
printf("\n\nvalue of a,b after swaping\n");
printf("a is %d\nb is %d\n",a,b);
getch();
}
Output:
enter number
1
2
value of a,b before swaping
a is 1
b is 2
value of x,y
x is 2
y is 1
value of a,b after swaping
a is 2
b is 1

Program to calculate simple interest


#include<stdio.h>
#include<conio.h>
int main()
{
int p;
float r,t,si;
printf("Enter Principal money : ");
scanf("%d",&p);
printf("Enter rate of interest : ");
scanf("%f",&r);
printf("Enter time of interest : ");
scanf("%f",&t);
si=(p*t*r)/100;
printf("\nSimple Interest is: %2.2f",si);
getch();
}
Output:
Enter Principal money : 3
Enter rate of interest : 2
Enter time of interest : 1
Simple Interest is: 0.06
program to swap to variables two numbers
#include<stdio.h>
#include<conio.h>
int x,y;
int swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=x;
printf("After swapping numbers are :\n first is %d\n second is %d",x,y);
}
int main()
{
printf("Enter first variables \n");
scanf("%d",&x);
printf("Enter second varibles \n");
scanf("%d",&y);
swap(x,y);

getch();
}
program to swap to varibles without using temp variables
#include<stdio.h>
#include<conio.h>
int x,y;
int swap(int x,int y)
{
x=x+y;
y=x-y;
x=x-y;
printf("After swapping numbers are :\n first is %d\n second is %d",x,y);
}
int main()
{
printf("Enter first variables \n");
scanf("%d",&x);
printf("Enter second varibles \n");
scanf("%d",&y);
swap(x,y);
getch();
}
12345
1234
123
12
1
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}

getch();
}
------------------------------12345
2345
345
45
5
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=i;j<=5;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
-----------------------------54321
4321
321
21
1
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
printf("%d",j);
}
printf("\n");

}
getch();
}
--------------------------------54321
5432
543
54
5
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
--------------------------------5
45
345
2345
12345
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=i;j<=5;j++)
{
printf("%d",j);
}

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

//BINARY SEARCH
/***** Program to Search an Array using Binary Search *****/
#include <stdio.h>
#include <conio.h>
void binary_search();
int a[50], n, item, loc, beg, mid, end, i;
void main()
{
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array in sorted form:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter ITEM to be searched: ");
scanf("%d", &item);
binary_search();
getch();
}
void binary_search()
{
beg = 0; end = n-1;
mid = (beg+end)/2;
while ((beg<=end) && (a[mid]!=item))
{
if (item < a[mid])
end = mid-1;

else
beg = mid+1;
mid = (beg+end)/2;
}
if (a[mid] == item)
printf("\n\nITEM found at location %d", mid+1);
else
printf("\n\nITEM doesn't exist");
}
------------------------------------------------------------------BINARY SEARCH
/***** Program to Search an Array using Binary Search *****/
#include <stdio.h>
#include <conio.h>
void binary_search();
int a[50], n, item, loc, beg, mid, end, i;
void main()
{
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array in sorted form:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter ITEM to be searched: ");
scanf("%d", &item);
binary_search();
getch();
}
void binary_search()
{
beg = 0; end = n-1;
mid = (beg+end)/2;

while ((beg<=end) && (a[mid]!=item))


{
if (item < a[mid])
end = mid-1;
else
beg = mid+1;
mid = (beg+end)/2;
}
if (a[mid] == item)
printf("\n\nITEM found at location %d", mid+1);
else
printf("\n\nITEM doesn't exist");
}

program to find power of given number


#include<stdio.h>
#include<conio.h>
#include<math.h>
int n,p,power;
int main()
{
printf("Enter number : ");
scanf("%d",&n);
printf("Enter power : ");
scanf("%d",&p);
power=pow(n,p);
printf("numbers is : %d",power);
getch();
}

Factorial program working


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

void main()
{
int number,fact;
fact=1;
printf("\n enter the number");
scanf("%d",&number);
while(number>1)
{
fact=fact*number;
number--;
}
printf("fact %d",fact);
getch();
}
//array
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[] = {2,4,7,9,3};
printf("\t%d\n",a[4]);
printf("\t%d\n",a[2]);
getch();
}
// array with for
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5] = {1,2,3,4,5};
for (int i = 0; i<5; i++)
{
printf("\t%d\n",a[i]);

}
getch();
}

// array with for


#include<stdio.h>
#include<conio.h>
void main()
{int i;
clrscr();
int a[5];
for (i = 0; i<5; i++)
{
printf("enter the value in index %d",i);
scanf("%d",&a[i]);
}

for (i = 0; i<5; i++)


{
printf("\t%d\n",a[i]);
}
getch();
}
//2d array
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5][2] = { {1,2},
{3,4},
{5,6},
{7,8},

{9,10} };
for (int i = 0; i<5; i++)
{
for(int j = 0; j<2; j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
getch();
}
// array with for
#include<stdio.h>
#include<conio.h>
void main()
{int i,j;
clrscr();
int a[5][2];
for (i = 0; i<5; i++)
{
for(j = 0; j<2; j++)
{
printf("enter the value in index %d",i);
scanf("%d",&a[i][j]);
}
}
for (i = 0; i<5; i++)
{
for(j = 0; j<2; j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
getch();

}
File handling
#include<stdio.h>
#include<conio.h>
FILE *fp;
struct student
{
int rno;
char nm[25];
float m1,m2,m3;
}stud;
int i;
void main()
{
fp=fopen("mark.txt","w");
for(i=1;i<=2;i++)
{
printf("enter roll no.");
scanf("%d",&stud.rno);
printf("enter name->");
scanf("%s",&stud.nm);
printf("enter 3 subjet marks");
scanf("%f%f%f",&stud.m1,&stud.m2,&stud.m3);
fprintf(fp,"%d\n%s\n%f\n
%f\n",stud.rno,stud.nm,stud.m1,stud.m2,stud.m3);
}
fclose(fp);
}
File handling
#include<stdio.h>
#include<conio.h>
FILE *fp;

struct student
{
int rno;
char nm[25];
float m1,m2,m3;
}stud;
int i;
void main()
{
fp=fopen("mark.txt","w");
for(i=1;i<=2;i++)
{
printf("Enter Roll No.->");
scanf("%d",&stud.rno);
printf("Enter Name->");
scanf("%s",&stud.nm);
printf("Enter 3 Subject Marks");
scanf("%f%f%f",&stud.m1,&stud.m2,&stud.m3);
fprintf(fp,"%d\n%s\n%f\n%f\n
%f\n",stud.rno,stud.nm,stud.m1,stud.m2,stud.m3);
}
fclose(fp);
}
File handling
#include<stdio.h>
#include<conio.h>
FILE *fp;
struct student
{
int rno;
char nm[25];
float m1,m2,m3;
}stud;

int i;
void main()
{
fp=fopen("mark.txt","u");
for(i=1;i<=2;i++)
{
printf("enter roll no.");
scanf("%d",&stud.rno);
printf("enter name->");
scanf("%s",&stud.nm);
printf("enter 3 subjet marks");
scanf("%f%f%f",&stud.m1,&stud.m2,&stud.m3);
fprintf(fp,"%d\n%s\n%f\n
%f\n",stud.rno,stud.nm,stud.m1,stud.m2,stud.m3);
}
fclose(fp);
}
File handling
#include<stdio.h>
#include<conio.h>
#include<process.h>
FILE *fp;
struct student
{
int rno;
char nm[25];
float m1,m2,m3;
}stud;
int i,n,p,gd;
void main()
{
printf("Press 1 To Give Info. By Own\nPress 2 To Give Info. By
Computer\nPress 3 To Exit");
scanf("%d",&n);

switch(n)
{
case 1:
fp=fopen("mark.txt","w");
for(i=1;i<=1;i++)
{
printf("Enter Roll No.->");
scanf("%d",&stud.rno);
printf("Enter Name->");
scanf("%s",&stud.nm);
printf("Enter 3 Subject Marks->");
scanf("%f%f%f",&stud.m1,&stud.m2,&stud.m3);
fprintf(fp,"%d\n%s\n%f\n%f\n
%f\n",stud.rno,stud.nm,stud.m1,stud.m2,stud.m3);
}
break;
case 2:
fp=fopen("mark.txt","r");
for(i=1;i<=1;i++)
{
fscanf(fp,"%d%s%f%f
%f",&stud.rno,&stud.nm,&stud.m1,&stud.m2,&stud.m3);
p=((stud.m1+stud.m2+stud.m3)*100)/300;
if(p<40)
gd='F';
else
if(p<50)
gd='C';
else
if(p<60)
gd='B';
else
gd='A';
printf("\nRoll No.
printf("\nName
printf("\nMarks
%f",stud.m1,stud.m2,stud.m3);

:%d",stud.rno);
:%s",stud.nm);
:%f%f

printf("\nPercentage :%f",p);
printf("\nGrade
:%d",gd);
break;
case 3:
exit(01);
break;
}
getch();
}}
Read file from user
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *in_file;
char mark[20];
int c;
clrscr();
printf("enter the file name\n");
scanf("%s",&mark);
if((in_file=fopen(mark,"r"))==NULL)
{
printf("file does not exist\n");
}
else
{
while((c=getc(in_file))!=EOF)

{
putchar(c);

}
}
fclose(in_file);
getch();
}
Quick sort
#include<iostream.h>
#include<conio.h>
const int max=5;
class array
{
private:
int ar[max];
int count;
public:array();
void add(int item);
int getcount();
static int split(int*,int,int);
void quicksort(int low,int high);
void display();
};
array::array()
{
count=0;
for(int i=0;i<max;i++)
ar[i]=0;
}
void array::add(int item)
{
if(count<max)
{
ar[count]=item;
count++;
}
else

cout<<"\n array is full"<<endl;


}
int array::getcount()
{
return count;
}
void array::quicksort(int low,int high)
{
if(high>low)
{
int i=split(ar,low,high);
quicksort(low,i-1);
quicksort(i+1,high);
}
}
int array::split(int *a,int low,int high)
{
int i,p,q,t;
p=low+1;
q=high;
i=a[low];
while(q>=p)
{
while(a[p]<i)
p++;
while(a[q]>i)
q--;if(q>p)
{
t=a[p];a[p]=a[q];
a[q]=t;
}
}
t=a[low];
a[low]=a[q];
a[q]=t;
return q;
}
void array::display()

{
for(int i=0;i<count;i++)
cout<<ar[i]<<"\t";
cout<<endl;
}
void main()
{
array a;
a.add(22);
a.add(66);
a.add(77);
a.add(44);
a.add(11);
cout<<"\n element of quick sort before sorting ";
a.display();
int c=a.getcount();
a.quicksort(0,c-1);
cout<<"\n elements of quick sort after sorting";
a.display();
getch();
}

Das könnte Ihnen auch gefallen