C-Programs with output

C-Programs with output


Write a program to create a linear linked list interactively and print out the list and the total number of items in the list

Posted: 26 May 2013 11:13 PM PDT

#include<stdio.h>
#include<stdlib.h>
#define NULL 0
struct linked_list
{
int number;
struct linked_list *next;
};
typedef struct linked_list node; /* node type defined */
main()
{
node *head;
void create(node *p);
int count(node *p);
void print(node *p);
head = (node *)malloc(sizeof(node));
create(head);
printf("\n");
printf(head);
printf("\n");
printf("\nNumber of items = %d \n", count(head));
}
void create(node *list)
{
printf("Input a number\n");
printf("(type -999 at end): ");
scanf("%d", &list -> number); /* create current node */
if(list->number == -999)
{
list->
next = NULL;
}
else /*create next node */
{
list->
next = (node *)malloc(sizeof(node));
create(list->next); */ Recursion occurs */
}
return;
}
void print(node *list)
{
if(list->next != NULL)
{
printf("%d-->",list ->number); /* print current item */
if(list->next->next == NULL)
printf("%d", list->next->number);
print(list->next); /* move to next item */
}
return;
}
int count(node *list)
{
if(list->next == NULL)
return (0);
else return(1+ count(list->next));
}

Output:

Input a number
(type -999 to end); 60
Input a number
(type -999 to end); 20
Input a number
(type -999 to end); 10
Input a number
(type -999 to end); 40
Input a number
(type -999 to end); 30
Input a number
(type -999 to end); 50
Input a number
(type -999 to end); -999
60 -->20 -->10 -->40 -->30 -->50 --> -999
Number of items = 6

Find 2’s complement of a binary number

Posted: 26 May 2013 10:50 PM PDT

#include<stdio.h>
#include<conio.h>
void complement (char *a);
void main()
{
char a[16];
int i;
clrscr();
printf("Enter the binary number");
gets(a);
for(i=0;a[i]!='\0'; i++)
{
if (a[i]!='0' && a[i]!='1')
{
printf("The number entered is not a binary number. Enter the correct number");
exit(0);
}
}
complement(a);
getch();
}
void complement (char *a)
{
int l, i, c=0;
char b[16];
l=strlen(a);
for (i=l-1; i>=0; i--)
{
if (a[i]=='0') b[i]='1';
else b[i]='0';
}
for(i=l-1; i>=0; i--)
{
if(i==l-1)
{
if (b[i]=='0') b[i]='1';
else
{
b[i]='0'; c=1;
}
}
else
{
if(c==1 && b[i]=='0')
{
b[i]='1'; c=0;
}
else if (c==1 && b[i]=='1')
{
b[i]='0';
c=1;
}
}
}
b[l]='\0';
printf("The 2's complement is %s", b);
}

Implement Trapezoidal Method

Posted: 26 May 2013 10:47 PM PDT

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

char postfix[80];
float stack[80];
char stack1[80];
int top=-1,top1=-1;

float eval(char postfix[], float x1);
void infix_postfix(char infix[]);

main()
{
float x0, xn, h, s,e1,e2;
char exp[80], arr[80];
int i,n,l=0;
clrscr();
printf("\nEnter an expression: ");
gets(exp);
puts("Enter x0, xn and number of subintervals");
scanf("%f%f%d", &x0, &xn, &n);
h=(xn-x0)/n;
if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g')
{
l=strlen(exp);
for(i=0;i<l-3; i++)
arr[0]=exp[i+3];
arr[i]='\0';
infix_postfix(arr);
e1=eval(postfix,x0);
e2=eval(postfix,xn);
s=log(e1)+log(e2);
for (i=1;i<=n-1;i++)
s+=2*log(eval(postfix,x0+i*h));
}
else
{
infix_postfix(exp);
s=eval(postfix,x0)+eval(postfix,xn);
for (i=1;i<=n-1;i++)
s+=2*eval(postfix,x0+i*h);
}
printf("Value of the integral is %6.3f\n",(h/2)*s);
return(0);
}
/*Inserting the operands in a stack. */
void push(float item)
{
if(top==99)
{
printf("\n\tThe stack is full");
getch();
exit(0);
}
else
{
top++;
stack[top]=item;
}
return;
}
/*Removing the operands from a stack. */
float pop()
{
float item;
if(top==-1)
{
printf("\n\tThe stack is empty\n\t");
getch();
}
item=stack[top];
top--;
return (item);
}
void push1(char item)
{
if(top1==79)
{
printf("\n\tThe stack is full");
getch();
exit(0);
}
else
{
top1++;
stack1[top1]=item;
}
return;
}
/*Removing the operands from a stack. */
char pop1()
{
char item;
if(top1==-1)
{
printf("\n\tThe stack1 is empty\n\t");
getch();
}
item=stack1[top1];
top1--;
return (item);
}

/*Converting an infix expression to a postfix expression. */
void infix_postfix(char infix[])
{
int i=0,j=0,k;
char ch;
char token;
for(i=0;i<79;i++)
postfix[i]=' ';
push1('?');
i=0;
token=infix[i];
while(token!='\0')
{
if(isalnum(token))
{
postfix[j]=token;
j++;
}
else if(token=='(')
{
push1('(');
}
else if(token==')')
{
while(stack1[top1]!='(')
{
ch=pop1();
postfix[j]=ch;
j++;
}
ch=pop1();
}
else
{

while(ISPriority(stack1[top1])>=ICP(token))
{
ch=pop1();
/*Assigning the popped element into the postfix array. */
postfix[j]=ch;
j++;
}
push1(token);
}
i++;
token=infix[i];
}
while(top1!=0)
{
ch=pop1();
postfix[j]=ch;
j++;
}
postfix[j]='\0';
}

int ISPriority(char token)
{
switch(token)
{
case '(':return (0);
case ')':return (9);
case '+':return (7);
case '-':return (7);
case '*':return (8);
case '/':return (8);
case '?':return (0);
default: printf("Invalid expression");
break;
}
return 0;
}
/*Determining the priority of elements that are approaching towards the stack. */
int ICP(char token)
{
switch(token)
{
case '(':return (10);
case ')':return (9);
case '+':return (7);
case '-':return (7);
case '*':return (8);
case '/':return (8);
case '\0':return (0);
default: printf("Invalid expression");
break;
}
return 0;
}
/*Calculating the result of expression, which is converted in postfix notation. */
float eval(char p[], float x1)
{
float t1,t2,k,r;
int i=0,l;
l=strlen(p);
while(i<l)
{
if(p[i]=='x')
push(x1);
else
if(isdigit(p[i]))
{
k=p[i]-'0';
push(k);
}
else
{
t1=pop();
t2=pop();
switch(p[i])
{
case '+':k=t2+t1;
break;
case '-':k=t2-t1;
break;
case '*':k=t2*t1;
break;
case '/':k=t2/t1;
break;
default: printf("\n\tInvalid expression");
break;
}
push(k);
}
i++;
}
if(top>0)
{
printf("You have entered the operands more than the operators");
exit(0);
}
else
{
r=pop();
return (r);
}
return 0;
}

Calculate the cost of publishing conference

Posted: 26 May 2013 10:28 PM PDT

#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
const int std_size=15;
const float std_cost=5;
const float penalty=0.5;
class paper;
float totalcost(paper*,int);
class paper
{
char author[25];
int page;
   public:
paper()
{
author[0]='\0';
page=0;
}
paper(char a[],int p)
{
strcpy(author,a);
page=p;
}
void input();
float papercost();
};
void paper::input()
{
cout<<"\nEnter name of the author : ";
gets(author);
cout<<"\nEnter number of pages in his research paper : ";
cin>>page;
}
float paper::papercost()
{
int extra;
float cost;
if(page<=std_size)
return page*std_cost;
else
{
extra = page - std_size;
cost  = (std_size*std_cost) + extra*(std_cost + penalty);
return cost;
}
}
int main()
{
int count;
paper *conf;
cout<<"Enter total number of papers : ";
cin>>count;
cout<<"\nEnter details of the papers\n";
cout<<"---------------------------\n";
conf = new paper[count];
for(int i=0;i<count;i++)
conf[i].input();
cout<<"\n\nThe total cost of publishing is : Rs."<<totalcost(conf,count);
delete conf;
return 0;
}
float totalcost(paper *p,int n)
{
float tcost=0;
for(int i=0;i<n;i++)
{
tcost +=  p[i].papercost();
}
return tcost;
}


Output:
Enter total number of papers : 2

Enter details of the papers
---------------------------
Enter name of the author :
Enter number of pages in his research paper : 3
Enter name of the author :
Enter number of pages in his research paper : 3
The total cost of publishing is : Rs.30

Area of two triangles - CPP Program

Posted: 26 May 2013 09:54 PM PDT

#include<iostream.h>
#include<conio.h>
#include<math.h>
class right_triangle
{
double base;
double height;
   public:
void initialize(double,double);
double area();
double peri();
};
void right_triangle::initialize(double b, double h)
{
base=b;
height=h;
}
double right_triangle::area()
{
return (0.5*base*height);
}
double right_triangle::peri()
{
double hypt;
hypt = sqrt(base*base + height*height);
return base+height+hypt;
}
int main()
{
right_triangle r1,r2;
double bs,ht;
//Initializing triangles
cout<<"\nINPUT\n";
cout<<"\nEnter base of first triangle : ";
cin>>bs;
cout<<"Enter height of first triangle : ";
cin>>ht;
r1.initialize(bs,ht);
cout<<"\nEnter base of second triangle : ";
cin>>bs;
cout<<"Enter height of second triangle : ";
cin>>ht;
r2.initialize(bs,ht);
//Calculating area and perimeter
cout<<"\nArea of first triangle       : "<<r1.area();
cout<<"\nPerimeter of first triangle  : "<<r1.peri();
cout<<"\n\nArea of second triangle      : "<<r2.area();
cout<<"\nPerimeter of second triangle : "<<r2.peri();
return 0;
}


Output:
INPUT
Enter base of first triangle : 7
Enter height of first triangle : 4
Enter base of second triangle : 6
Enter height of second triangle : 2
Area of first triangle       : 14
Perimeter of first triangle  : 19.0623
Area of second triangle      : 6
Perimeter of second triangle : 14.3246
Process returned 0 (0x0)   execution time : 26.804 s
Press any key to continue.

Income Class in C++

Posted: 26 May 2013 09:44 PM PDT

#include<iostream.h>
#include<conio.h>
class income
{
double BS;
double DA;
   public:
void initialize(double,double);
double pay_sal();
double deduction();
double calc_tax();
void income_detail();
};
void income::initialize(double b,double d)
{
BS=b;
DA=d;
}
double income::pay_sal()
{
double HRA;
HRA = 0.15 * BS;
return (BS+DA+HRA);
}
double income::deduction()
{
double SC,PF;
SC = PF = 0.08 * BS;
return (SC+PF);
}
double income::calc_tax()
{
double sal,tax,sc;
sal = pay_sal();
sal*=12;
if(sal<100000)
{
tax = 0.2 * sal;
}
else
{
tax = 0.3 * sal;
sc  = 0.1 * tax;
tax+=sc;
}
return tax;
}
void income::income_detail()
{
cout<<"\nBasic Salary is         : "<<BS;
cout<<"\nDearness Allowance is   : "<<DA;
cout<<"\nMonthly Deduction is    : "<<deduction();
cout<<"\nTotal Monthly Salary is : "<<pay_sal();
cout<<"\nTotal Annual Salary is  : "<<12*pay_sal();
cout<<"\nAnnual Payable Tax is   : "<<calc_tax();
}
int main()
{


double bs,da;
income s;
//Initializing Income
cout<<"\nEnter Basic Pay : ";
cin>>bs;
cout<<"\nEnter Dearness Allowance : ";
cin>>da;
s.initialize(bs,da);
s.income_detail();
return 0;
}


Output:
Enter Basic Pay : 5000
Enter Dearness Allowance : 5
Basic Salary is  : 5000
Dearness Allowance is   : 5
Monthly Deduction is    : 800
Total Monthly Salary is : 5755
Total Annual Salary is  : 69060
Annual Payable Tax is   : 13812
Process returned 0 (0x0)   execution time : 36.459 s
Press any key to continue.

Example of Stack in C++

Posted: 26 May 2013 09:39 PM PDT

#include<iostream.h>
#include<conio.h>
const int size=10;
class stack
{
int arr[size];
int top;
   public:
void initialize();
void push(int);
int pop();
int stack_top();
void show();
};
void stack::initialize()
{
top=-1;
}
void stack::push(int n)
{
if(top!=size-1)
arr[++top]=n;
else
cout<<"\nOverflow!!!\n";
}
int stack::pop()
{
if(top!=-1)
return arr[top--];
else
{
cout<<"\nUnderflow!!!\n";
return NULL;
}
}
int stack::stack_top()
{
if(top==-1)
{
cout<<"\nStack is Empty!\n";
return NULL;
}
else
return arr[top];
}
void stack::show()
{
if(top==-1)
cout<<"\nEmpty Stack!!!\n";
else
{
for(int i=0;i<=top;i++)
cout<<arr[i]<<" ";
cout<<"<--TOP\n";
}
}
int main()
{
int p;
stack s1;
s1.initialize();
//Pushing Values
cout<<"\nPUSHING 3,5,7 onto stack\n";
s1.push(3);
s1.push(5);
s1.push(7);
cout<<"\nStack is : ";
s1.show();
//Show Top Value
cout<<"\nTop of Stack is :"<<s1.stack_top()<<endl;
//Popping Values
cout<<"\nPOPPING\n";
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
//Show Top of Stack
p=s1.stack_top();
if(p!=NULL)
cout<<"Top of Stack is : "<<p<<endl;
return 0;
}

Output:
PUSHING 3,5,7 onto stack
Stack is : 3 5 7 <--TOP
Top of Stack is :7
POPPING
Popped out value is : 7
Popped out value is : 5
Popped out value is : 3
Stack is Empty!


Complex number

Posted: 26 May 2013 09:37 PM PDT

#include<iostream.h>
#include<conio.h>
class complex
{
                float real;
                float imag;
   public:
                void getnum();
                void putnum();
                void sum(complex,complex);
                void dif(complex,complex);
};
void complex::getnum()
{
                cout<<"\nEnter the real part : ";
                cin>>real;
                cout<<"\nEnter the imaginary part : ";
                cin>>imag;
}
void complex::putnum()
{
                cout<<real;
                if(imag<0)
                                cout<<imag<<"i\n";
                else
                                cout<<"+"<<imag<<"i\n";
}
void complex::sum(complex a,complex b)
{
                real=a.real+b.real;
                imag=a.imag+b.imag;
}
void complex::dif(complex a,complex b)
{
                real=a.real-b.real;
                imag=a.imag-b.imag;
}
int main()
{
                complex c1,c2,c3,c4;
                cout<<"\nEnter first complex number\n";
                c1.getnum();
                cout<<"\nEnter second complex number\n";
                c2.getnum();
                //Sum of two inputted numbers
                c3.sum(c1,c2);
                cout<<"\nThe Sum is : ";
                c3.putnum();
                //Difference of two inputted numbers
                c4.dif(c1,c2);
                cout<<"\nThe Difference is : ";
                c4.putnum();


Output:
Enter first complex number
Enter the real part : 4
Enter the imaginary part : 5
Enter second complex number
Enter the real part : 3
Enter the imaginary part : 6
The Sum is : 7+11i
The Difference is : 1-1i

Find Prime Factor of number

Posted: 20 May 2013 12:12 PM PDT


/*Program to find out prime factors of a given number.*/

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

void main()
{
int number;
printf("Enter a number ");
scanf("%d",&number);
printf("\nThe prime factors of %d are:",number);
for(int num=2;num<32767;num++)
{
int i=2;
while(i<=num-1)
{
if(num%i==0)
{
break;
}
i++;
}
if(i==num)
{
if(number%num==0)
{
number=number/num;
printf("\t%d",num);
num=1;
}
else
{
continue;
}
}
}
getch();
clrscr();
}

Find Generic root of number

Posted: 01 Feb 2013 09:15 AM PST


#include <stdio.h>
int main()
{        

int num,i;
printf("Enter any number: ");
scanf("%d",&num);
printf("Generic root: %d", (i = num % 9) ? i : 9);
return 0;
}

Add Two numbers without using operator

Posted: 01 Feb 2013 09:12 AM PST


#include<stdio.h>

int main(){
  
    int a,b;
    int sum;

    printf("Enter any two integers: ");
    scanf("%d%d",&a,&b);

    //sum = a - (-b);
    sum = a - ~b -1;

    printf("Sum of two integers: %d",sum);

    return 0;
}


Print 1 to 10 Series

Posted: 26 Jun 2012 01:47 AM PDT


#include<stdio.h>
#include<conio.h>
int main()
{
int a;
printf("the no from 1 to 10 are:\n");
for(a=1;a<=10;a++)
printf("%d\n", a);
getch();
}

Convert Decimal to Hexadecimal Number

Posted: 17 Jan 2012 08:16 AM PST


#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
Int x, y=30, z;
clrscr();
printf("Enter the number:");
scanf("%d", &x);
printf("\n conversion of decimal to hexadecimal number\n");
for(;;)
{
if(x= =0)
exit(1);
z=x%16;
x=x/16;
gotoxy(y--,5);
switch(z)
{
Case 10:
Printf("A");
Break;
Case 11:
Printf("%c", „B?);
Break;
Case 12:
Printf(%c", „C");
Break;
Case 13:
Printf("D");
Break;
Case 14:
Printf("E");
Break;
Case 15:
Printf("F");
Default:
Printf("%d", z);
}
}
getch();
}

Output:
Enter the number: 31
Conversion of decimal to Hexa decimal number
1F

Mark list Analysis using Structures

Posted: 17 Jan 2012 07:26 AM PST


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
struct stud
{
int rno;
char name[15];
int marks[5];
int total;
float avg;
char class[15];
}
st[10],temp;
int i,n,j;
clrscr();
printf("\n enter\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n enter the roll no..");
scanf("%d",&st[i].rno);
printf("name...\n");
scanf("%s",&st[i].name);
printf("enter three marks..");
for(j=1;j<=3;j++)
scanf("%d",&st[i].marks[j]);
}
for(i=1;i<=n;i++)
{
st[i].total=0;
for(j=1;j<=3;j++)
{
st[i].total=st[i].total+st[i].marks[j];
}
st[i].avg=st[i].total/30;
if(st[i].avg>=75)
strcpy(st[i].grade,"distinction");
elseif(st[i].avg>=60)
strcpy(st[i].grade,"first");
elseif(st[i].avg>=50)
strcpy(st[i].grade,"second");
else
strcpy(st[i].grade,"fail");
}
for(i=1;i<=n;i++)
{
for(j=j+1;j<=n;j++)
{
if(st[i].total<st[j].total)
{
temp=st[i];
st[i]=st[j];
st[j]=temp;
}
}
}
printf("\n the student details in rankwise\n");
for(i=1;i<=n;i++)
{ 
printf("\n\n roll no:%d",st[i].rno);
printf("\n name :%s",st[i].name);
printf("\n marks in three subjects");
for(j=1;j<=3;j++)
{
printf("\n %d,st[i].marks[j]);
}
printf ("\n total: %d", st[i].total);
printf("\n average:%f",st[i].avg);
printf("\n grade:%s",st[i].grade);
}
getch();
}

Output:
enter
2
enter the roll no...105
name...sheik raja
enter the three marks...89
87
78
enter roll no...110
name...sriram
enter the three marks...98
96
95

the student details in rankwise

roll no:105
name:sheik raja
marks in three subjects
89
87
78
total:254
average:84.666664
grade:distinction

roll no:110
name:sriram
marks in three subjects
98
96
95
total:289
average:96.3333336
grade:distinction 

Standard Deviation using Function

Posted: 17 Jan 2012 07:21 AM PST


#include<stdio.h>
#include<conio.h>
#include<math.h>
float mean(int a[],int n);
float std(int a[],int n,float m);
void main()
{
float m,sd;
int n,a[10],i;
clrscr();
printf("\nenter the number of values\n");
scanf("%d",&n);
printf("\n enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&n);
m=mean(a,n);
printf("mean=%f\n",m);
sd=std(a,n,m);
printf("\n sd=%f",sd);
getch();
}
flaot mean (inta[],intn)
{
float f;
int sum=0;
for(i=0;i<n;i++)
sum=sum+ a[i];
f=(float)sum/n;
return f;
}
float std(int a[],int n,float m)
{
int i;
float std,sum=0.0,d;
for(i=0;i<n;i++)
{
d=a[i]-m;
a=d*d;
sum=sum+d;
}
sd=sqrt(sum\n);
return sd;
}

Output:
enter the number of values
5
enter the elements
2
4
6
8
10
mean=6.000000
sd=2.828427 

Substring Replacement

Posted: 17 Jan 2012 07:20 AM PST


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50],str1[15],str2[15],temp[50];
char *ptr;
int cnt;
clrscr();
printf("enter a line of text..... \n");
gets(str);
printf("enter the string to be replaced... \n");
gets(str1);
printf("enter the replacing string...");
gets(str2);
printf("\n the replaced line of the text....");
while(1)
{
ptr=strstr(str,str1);
if(ptr==?\o?)
break;
cnt=ptr-str;
strncpy(temp,str,cnt);
temp[cnt]=?10?;
strcat(temp,str+cnt+strlen(srt1));
strcpy(str1,temp);
puts(str);
}
getch();
}

Output:
enter the line of text... i love india
enter the string to be replaced..india
enter the replacing string...my parents
the replaced line of text
i love my parents 

the Swapping of two Values using Functions

Posted: 17 Jan 2012 07:19 AM PST


#include<stdio.h>
#include<conio.h>
int swapval(int,int);
int swapref(int*,int*);
int a,b;
void main()
{
clrscr();
printf("enter the two values\n");
scanf("%d%d",&a,&b);
printf("pass by value\n");
printf("before function call a=%d b=%d ",a,b);
swapval(a,b);
printf("after function swapval a=%d b=%d ",a,b);
printf("pass by reference\n");
printf("before function call a=%d b=%d ",a,b);
swapref(&a,&b);
printf("after function swapref a=%d b=%d ",a,b);
getch();
}
swapval(int x,int y)
{
int t;
t=x;
x=y;
y=t;
printf("\nwith swap val x=%d y=%d",x,y);
}
swapref(int*x,int*y)
{
int *t;
*t=*x;
*x=*y;
*y=*t;
printf("\nwith swapref x=%d y=%d ",*x,*y);
}

Output:
give two numbers 
5
6
pass by value
 before function call a=5 b=6
with swapval x=6 y=5
after function swapval a=5 b=6
pass by reference
before function call a=5 b=6
with swapref x=6 y=5l
after function swapref a=6 b=5 

Fibonacci Series using Recursive Function

Posted: 17 Jan 2012 07:18 AM PST


#include<stdio.h>
#include<conio.h>
int fibo(int,int)
int t1,t2,t3,count;
void main()
{
printf("enter the number of terms\n");
scanf("%d",&n);
t1=0;
t2=1;
printf("%d\t%d",t1,t2);
count=2;
fibo(t1,t2);
getch();
}
int fibo(int t1,int t2)
{
if(count>=n)
return 0;
else
{
t3=t1+t2;
printf("\t%d",t3);
count++;
t1=t2;
t2=t3;
fibo(t1,t2);
}
}



Output:
enter the number of terms
5
0 1 1 2 3

Sorting Strings in Ascending Order

Posted: 17 Jan 2012 07:17 AM PST


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,n,x;
char str[20][20],str1[20][20];
clrscr();
printf("enter the number of strings:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter str[%d]",i+1);
scanf("%s",&str[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
x=strcmp(str[i],str[j])
if(x>0)
{
strcpy(str[1],str[j]);
strcpy(str[j],str[i]);
strcpy(str[i],str[1]);
}
}
}
printf("\nthe sorted strings in ascending order is\n");
for(i=0;i<n;i++)
{
printf("\n%s",str[i]);
}
getch();
}

Output:
enter the number of strings:
3
enter str[1] raja
enter str[2] vignesh
enter str[3] adhi
the sorted strings in ascending order is
adhi
raja
vignesh 

Merging The Elements In An Array

Posted: 17 Jan 2012 07:16 AM PST


#include<stdio.h>
#include<conio.h>
void main()
{
int j,h=0,k=0;
int x[4]={1,2,3,4};
int y[4]={5,6,7,8};
int z[8];
clrscr();
printf("array x:\n");
for(j=0;j<4;j++)
printf("%d",x[j]);
printf("array y:\n");
for(j=0;j<4;j++)
printf("%d",y[j]);
j=0;
while(j<8)
{
if(j%2==0)
z[j]=x[k++];
else
z[j]=y[h++];
j++;
}
printf("array z:\n");
for(j=0;j<8;j++)
printf("%d",z[j]);
getch();
}

Output:
array x:
1  2  3  4 
array y:
5 6 7 8
array z:
1 2 3 4 5 6 7 8 

Deleting Element In An Array

Posted: 17 Jan 2012 07:15 AM PST


#include<stdio.h>
#include<conio.h>
void main()
{
int num[20],j,p,n,s;
clrscr();
printf("enter the number of elements\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(j=0;j<n;j++)
scanf("%d",&num[j]);
printf("enter the position to delete\n");
scanf("%d",&p);
p--;
for(j=p;j<n;j++)
num[j]=num[j+1];
for(j=0;j<n-1;j++)
printf("%d",num[j]);
getch();
}

Output:
enter the number of elements
3
enter elements
1
2
3
enter the position to delete 
2
1  3  


Inserting Elements In An Array

Posted: 17 Jan 2012 07:14 AM PST


#include<stdio.h>
#include<conio.h>
void main()
{
int num[20],j,p,n,s;
clrscr();
printf("enter the number of elements\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(j=0;j<n;j++)
scanf("%d",&num[j]);
printf("enter the element and positon to be inserted\n");
scanf("%d%d",&s,&p);
p--;
for(j=n;j!=p;j--)
{
num[j]=num[j-1];
}
num[j]=s;
for(j=0;j<=n;j++)
printf("%d",num[j]);
getch();
}

Output:
enter the number of elements
4
enter elements
1
2
3
5
enter the element and position to be inserted
4
4
1  2  3  4 

Searching the Element in an Array

Posted: 17 Jan 2012 07:13 AM PST


#include<stdio.h>
#include<conio.h>
void main()
{
int j=0,n,x[5];
clrscr();
printf("enter the elements of array\n");
for(j=0;j<5;j++)
scanf("%d",&x[j]);
printf("enter the element to search\n");
scanf("%d",&n);
for(j=0;j<5;j++)
{
if(x[j]==n)
break;
}
if(x[j]==n)
printf("element found");
else
printf("element not found");
getch();
}

Output:
enter the elements of array:
1
2
3
4
5
enter the elements to search
3
element found

Find Maximum Value in Array

Posted: 17 Jan 2012 07:11 AM PST


#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],max,i;
clrscr();
printf("enter elements for the array\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
max=a[0];
for(i=1;i<5;i++)
{
if(max<a[i])
max=a[i];
}
printf("the maximum value is%d",max);
getch();
}

Output:
enter the elements for array
4
6
3
8
5
the maximum value is 8 

Calculate Electric Energy Bill

Posted: 17 Jan 2012 07:09 AM PST


#include<stdio.h>
#include<conio.h>
void main()
{
float r,a=2.5,b=3.5,c=1.5;
clrscr();
printf("enter the readings\n");
scanf("%f",&r);
if(r>=200)
printf("rupees=%f",r*b);
else if((r>=100)&&(r<200))
printf("rupees=%f",r*a);
else
printf("rupees=%f",r*c);
getch();
}

Output:
enter the readings
140
rupees=350 



Post a Comment