#include<stdio.h>
#include<stdlib.h>
int *a,n;
void bubblesort();
void swap_w(int,int);
main()
{
int i,j;
printf("Enter the number of elements ");
scanf("%d",&n);
a=(int *)calloc(n,sizeof(int));
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",a+i);
bubblesort(0,n-1);
printf("The sorted sequence is\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
return;
}
void bubblesort()
{
int i,j;
for(i=0;i<n-1;i++)
for(j=n-1;j>=i+1;j--)
if(a[j]<a[j-1])
swap_w(j,j-1);
}
void swap_w(int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
return;
}
Showing posts with label Simple programs. Show all posts
Showing posts with label Simple programs. Show all posts
Wednesday, 12 March 2014
Thursday, 25 July 2013
Posted by Kousik
This is a program to calculate your
age./*AGE CALCULATE*/#include<stdio.h>int main(){int dd,dm,dy,cd,cm,cy,d,m,y;printf(“Enter your DATE OF BIRTH in dd mm yyyy format\n”);scanf(“%d %d %d”,&dd,&dm,&dy);printf(“Enter Current Date in dd mm yyyy format\n”);scanf(“%d %d %d”,&cd,&cm,&cy);y=cy-dy;if(dm>cm){y--;m=12-(dm-cm);if(dd>cd){m--;d=30-(dd-cd);}else{d=cd-dd;}}else{m=cm-dm;if(dd>cd){m--;d=30-(dd-cd);}else{d=cd-dd;}printf(“You AGE is \n”);printf(“%d days %d months %d years\n”,d,m,y);}
Sunday, 10 March 2013
This is a program show the applications the mod (%) operation.
/*This is a program to input three digit number from user and display square of first and last number */
#include<stdio.h>
int main()
{
int num,rem,sqr1,sqr2;
start:
printf("Enter a three digit number ");
scanf("%d",&num);
if(num>999){
printf("\nSorry you have Entered long number\n");
goto start;
}
if(num<100){
printf("\nSorry you have entered short number\n");
goto start;
}
rem=num%10;
sqr2=rem*rem;
num=num/10;
num=num/10;
sqr1=num*num;
printf("\nThe square of the first digit is %d and the square of the last digit is %d \n",sqr1,sqr2);
return 0;
}
The output of this program ....
Saturday, 9 March 2013
In C every character has a ASCII number. Here we can use this concept for this program.
First we take a input from user. Then we check whether it is an alphabet or not and also check whether it is in capital or in small letter. If it is in capital then we add 32 with the character's ASCII value.
If it is in small letter then we subtract 32 from the character's ASCII value.
For your convenience here is the ASCII chart.
Now the the example ....
#include<stdio.h>
#include<ctype.h>
int main()
{
char c;
printf("Enter a alphabet ");
scanf("%c",&c);
if(isalpha(c))
{
if(c<'a')
{
c+=32;
printf("\nYou have entered capital letter\nThe corresponding small letter is %c\n",c);
}
else
{
c-=32;
printf("\nYou have entered small letter\nThe corresponding capital letter is %c\n",c);
}
}
return 0;
}
The output of this program will be....
First we take a input from user. Then we check whether it is an alphabet or not and also check whether it is in capital or in small letter. If it is in capital then we add 32 with the character's ASCII value.
If it is in small letter then we subtract 32 from the character's ASCII value.
For your convenience here is the ASCII chart.
Now the the example ....
#include<stdio.h>
#include<ctype.h>
int main()
{
char c;
printf("Enter a alphabet ");
scanf("%c",&c);
if(isalpha(c))
{
if(c<'a')
{
c+=32;
printf("\nYou have entered capital letter\nThe corresponding small letter is %c\n",c);
}
else
{
c-=32;
printf("\nYou have entered small letter\nThe corresponding capital letter is %c\n",c);
}
}
return 0;
}
The output of this program will be....
Friday, 8 March 2013
We can inter change values with out using third variable.
//This is a program to interchange values of to number without using third variable
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the frist number ");
scanf("%d",&a);
printf("\nEnter the scond number ");
scanf("%d",&b);
a=a*b;
b=a/b;
a=a/b;
printf("\nThe interchanged values are\nThe frist number is %d\nThe scond number is %d\n",a,b);
return 0;
}
The output will be.....
//This is a program to interchange values of to number without using third variable
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the frist number ");
scanf("%d",&a);
printf("\nEnter the scond number ");
scanf("%d",&b);
a=a*b;
b=a/b;
a=a/b;
printf("\nThe interchanged values are\nThe frist number is %d\nThe scond number is %d\n",a,b);
return 0;
}
The output will be.....
This is a simple program to swap the values using third variable.
//This is a program to interchange vaules of to number using thrid variable
#include<stdio.h>
int main()
{
int fst_num,snd_num,trd_num;
printf("Enter the frist number ");
scanf("%d",&fst_num);
printf("\nEnter the scond number ");
scanf("%d",&snd_num);
trd_num=fst_num;
fst_num=snd_num;
snd_num=trd_num;
printf("\nThe interchanged values are\nThe frist number is %d\nThe scond number is %d\n",fst_num,snd_num);
return 0;
}
The output of this program will be.....
//This is a program to interchange vaules of to number using thrid variable
#include<stdio.h>
int main()
{
int fst_num,snd_num,trd_num;
printf("Enter the frist number ");
scanf("%d",&fst_num);
printf("\nEnter the scond number ");
scanf("%d",&snd_num);
trd_num=fst_num;
fst_num=snd_num;
snd_num=trd_num;
printf("\nThe interchanged values are\nThe frist number is %d\nThe scond number is %d\n",fst_num,snd_num);
return 0;
}
The output of this program will be.....
This is a example of simple program to find out the square and cube of a number.
This program can be extended to find out any power of a number using recursion function.
//This is a program to find out Square and Cube of a given number
#include<stdio.h>
int main()
{
int num,sqrt,cube;
printf("\nEnter a number to Square and Cube ");
scanf("%d",&num);
sqrt=num*num;
cube=num*num*num;
printf("\nThe Square of the give number is %d\nand the cube of the give number is %d",sqrt,cube);
return 0;
}
This program can be extended to find out any power of a number using recursion function.
//This is a program to find out Square and Cube of a given number
#include<stdio.h>
int main()
{
int num,sqrt,cube;
printf("\nEnter a number to Square and Cube ");
scanf("%d",&num);
sqrt=num*num;
cube=num*num*num;
printf("\nThe Square of the give number is %d\nand the cube of the give number is %d",sqrt,cube);
return 0;
}
Subscribe to:
Posts (Atom)
















