Monday, October 31, 2011

Sorting the String in Dictionary order

Given an Array of Strings Write a Program to Sort the String in Dictionary Order.


Program:



#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[25][25],t[25];
int i,j ,n;
clrscr();
printf("enter the limit\n");
scanf("%d",&n);
printf("enter the elements:\n");
for(i=0;i<n;i++)
scanf("%s",&a[i]);
for(i=0;i<n-1;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 sorted array:\n");
for(i=0;i<n;i++)
printf("%s\n",a[i]);
getch();
}
Read full history - Sorting the String in Dictionary order

Sunday, October 30, 2011

String palindrome program in C (without string function )

Write a Program to find whether the Given String is Palindrome or not without using
string functions.


#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20];
int i,j,len=0,flag=0;
clrscr();
printf ("Enter a string:");
gets(s1);for(i=0;s1[i]!='\0';i++)
len++;
i=0;
j=len-1;
while(i<len)
{
if(s1[i]!=s1[j])
{
flag=1;
break;
}
i++;
j--;
}
if(flag==0)
printf("\nstring is palindrome ");
else
printf("\n string is not palindrome");
getch();
}


Read full history - String palindrome program in C (without string function )

Saturday, October 8, 2011

C program to find Compound Interest using functions


Write a program to display Compound Interest for a given principal amount for 1 yr. Use function to find the interest....



Program..

#include<stdio.h>
#include<conio.h>
float ci(int p,float r);
void main()
{
int p;
float r,i;
clrscr();
printf("Enter the pricipal amount to find the compound interest of:");
scanf("%d",&p);
printf("\nEnter the rate:(r/100)");
scanf("%f",&r);
i=ci(p,r);
printf("\nThe interest for 1 yr: %f",i);
getch();
}

float ci(int p, float r)
{
float i;
i=(p*(1+r))-p;
return i;
}


Output...


Read full history - C program to find Compound Interest using functions

C program to check for Armstrong Number


Write a program in C to check whether the given number is Armstrong or not....



Program...

#include<stdio.h>
#include<conio.h>
void main()
{
int num,r,sum=0,temp;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
temp=num;
while(num)
{
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(temp==sum)
printf("\n%d is an armstrong number",temp);
else
printf("\n%d is not an armstrong number",temp);
getch();
}


Output...

Read full history - C program to check for Armstrong Number

C Program to check for Palindrome Number

Write a program to check whether the given number is palindrome or not....



Program


#include<stdio.h>
#include<conio.h>
void main()
{
int num,r,sum=0,temp;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
temp=num;
while(num)
{
r=num%10;
num=num/10;
sum=(sum*10)+r;
}
if(temp==sum)
printf("\n%d is a palindrome",temp);
else
printf("\n%d is not a palindrome",temp);
getch();
}


Output...

Read full history - C Program to check for Palindrome Number

Wednesday, October 5, 2011

Sum of a given series






Write a program to compute the sum of the following series:

(1/1!)+(2/2!)+(3/3!)+..........................+(n/n!)




Program...

#include<stdio.h>
#include<conio.h>
float factorial(int n);
void main()
{
int l,i;
float sum=0.0;
clrscr();
printf("Enter the limit :\n");
scanf("%d",&l);
for(i=1;i<=l;i++)
{
sum=sum+(i/factorial(i));
}
printf("the sum of the series is:%f",sum);
getch();
}

float factorial(int n)
{
int fact=1,i;
for(i=1;i<=n;i++)
fact=fact*i;
return fact;
}


Output...


Read full history - Sum of a given series

Tuesday, October 4, 2011

Program to find the value of nCr





Write a program to find nCr....



Program:

#include<stdio.h>
#include<conio.h>
long int factorial(int n);
void main()
{
int n,r;
long int ncr;
clrscr();
l1:printf("Enter value of n and r to find the value of ncr(n>r)");
scanf("%d%d",&n,&r);
if(n<r)
{
printf("Enter the value of n>r \n.\n.\n.\n.\n.\n.\n.\n.\n.\n.");
goto l1;
}
ncr=factorial(n)/(factorial(r)*factorial(n-r));
printf("Value of nCr : %ld",ncr);
getch();
}

long int factorial(int n)
{
return n>1?n*factorial(n-1):1;
}



Output:




Read full history - Program to find the value of nCr

Finding Factorial of a number [ using Recursion & Iteration ]


 Write a program to find the factorial of a given number using recursion and iteration .....

Program
:

Using recursion...


#include<stdio.h>
#include<conio.h>
long int recur_factorial(int n);
void main()
{
int n;
long int fact;
clrscr();
printf("Enter the number to find the factorial of :");
scanf("%d",&n);
fact=recur_factorial(n);
printf("\nFactorial of %d is %ld",n,fact);
getch();
}

long int recur_factorial(int n)
{
return n>1?n*recur_factorial(n-1):1;
}


Using iteration....



#include<stdio.h>
#include<conio.h>
int fact_it(int i, int f);
void main()
{
int f=1,i,x;
clrscr();
printf("Enter the number to find the factorial of:");
scanf("%d",&i);
x=i;
for(i;i>0;i--)
f=fact_it(i,f);
printf("\nFactorial of %d is %d ",x,f);
getch();
}

int fact_it (int i,int f)
{
f=f*i;
return f;
}


Output:






Read full history - Finding Factorial of a number [ using Recursion & Iteration ]

Prime number [ using for, while, do-while ]

Write a program to display prime nos between 1 to 10 [ using for, while and do-while loop ]

Program:


Using for loop.....


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,flag=0;
clrscr();
printf("Prime no between 1 and 10 are:\n");
for(i=1;i<=10;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
flag++;
}
if(flag==2)
printf("%d\n",i);
flag=0;
}
getch();
}



Using while loop.....


#include<stdio.h>
#include<conio.h>
void main()
{
int count,i=1,a;
clrscr();
printf("Prime no between 1 and 10 are:\n");
while(i<=10)
{
count=0;
a=1;
while(a<=i)
{
if(i%a==0)
count++;
a++;
}
if(count==2)
printf("%d\n",i);
i++;
}
getch();
}


Using do-while loop.....

#include<stdio.h>
#include<conio.h>
void main()
{
int flag,i=1,a;
clrscr();
printf("Prime no between 1 and 10 are:\n");
do
{
flag=0;
a=1;
do
{
if(i%a==0)
flag++;
a++;
}
while(a<=i);
if(flag==2)
printf("%d\n",i);
i++;
}
while(i<=10);
getch();
}


Output:






Read full history - Prime number [ using for, while, do-while ]

Fibonacci series using Functions and Arrays


Write a program to display Fibonacci series by writing the value to the main().... [using function & arrays]

Program:



#include<stdio.h>
#include<conio.h>
long int fibo(int n);
void main()
{
int l,i;
long int fibonacci[50];
clrscr();
printf("Enter the limit:");
scanf("%d",&l);
for(i=0;i<l;i++)
fibonacci[i]=fibo(i);
for(i=0;i<l;i++)
{
printf("%d",fibonacci[i]);
printf("\n");
}
getch();
}

long int fibo(int n)
{
if(n<=1)
return n;
else
return fibo(n-1)+fibo(n-2);
}


Sample Output:


Read full history - Fibonacci series using Functions and Arrays

Pattern 1



Write a program to print the following pattern:
(1,1)  (1,2)  (1,3)
(3,1)  (3,2)  (3,3)
(5,1)  (5,2)  (5,3)

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<6;i++)
{
for(j=1;j<4;j++)
{
if(i==2 || i==4)
continue;
printf("(%d,%d)\t",i,j);
}
printf("\n");
}
getch();
}


Output:

 
Read full history - Pattern 1

Monday, October 3, 2011

Arithmatic operation using Switch case

Write a program to perform addition, subtraction, multiplication, division by reading arithmetic operators using Switch case.....



Program:


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
char ch;
clrscr();
printf("Enter two numbers for arithmetic operation:\n");
scanf("%d%d",&a,&b);
printf("Enter the arithmetic operator (+,-,*,/)");
ch=getch();
switch(ch)
{
case '+':printf("\nSum=%d",a+b);
break;
case '-':printf("\ndiff=%d",a-b);
break;
case '*':printf("\nproduct=%d",a*b);
break;
case '/':printf("\nquotient=%d",a/b);
break;
default:printf("\nInvalid choice");
}
getch();
}

Sample Output:



Read full history - Arithmatic operation using Switch case

About Me