Saturday, January 8, 2011

C Program to find Factorial with and without using Recursion Function

Coding:


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

long fact(long);
void main()
{
int i;long f;

/* Initialization for Factorial without using Recursion
int i,n;
long f=1;   */

clrscr();
printf("\nEnter the number: ");
scanf("%d",&i);
f=fact(i);

/* Factorial without using Recursion
for(n=1;n<=i;n++)
f*=n;  */

printf("\nFactorial of %d is %ld",i,f);
getch();
}

long fact(long k)
{
if(k==0)
return 1;
else
return k*(fact(k-1));

}

Output:


C Program to find Largest number of 'n' numbers

Coding:

#include<stdio.h> 
#include<conio.h> 
void main()
{
int n,m,i,max;
clrscr();
printf("\nHow many numbers are you going to enter:");
scanf("%d",&n);
printf("\nEnter the numbers:\n");
textcolor(YELLOW);
scanf("%d",&m);
max=m;
for(i=2;i<=n;i++)
{
scanf("%d",&m);
if(m>max)
max=m;
}
cprintf("\nThe Largest Number is ");
textcolor(GREEN);
cprintf("%d",max);
getch();
}

Output:


Thursday, January 6, 2011

C Program to find a number of lines,words and characters from a given string

Coding:



#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
   {
int l=1,w=0,chars=1;
char c;
clrscr();
textcolor(9);
cprintf("\n********Terminate String by pressing the Tab Key followed by Enter Key ********");
printf("\n\nEnter the String :\n\n");
c=getchar();
while(c!= '\t')
{
c=getchar();
chars++;
if(c==' '|| c=='\n'||c=='.')
w++;
if(c=='\n')
l++;
if(c=='\t')
chars--;
}
textbackground(WHITE);
cprintf("\r\n--------------------------  ");
cprintf("\r\nNumber of Lines      =%d    ",l);
cprintf("\r\nNumber of Words      =%d    ",w+1);
cprintf("\r\nNumber of Characters =%d    ",chars);
cprintf("\r\n--------------------------  ");
getch();
}


Output:



Program to find number of Characters and Words in C Programming

Coding:

#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
   {

char str[200];
int i=0,word=0,ch=0;
clrscr();
textcolor(YELLOW);
printf("\nEnter the String (Max=200 characters): ")  ;
gets(str);
while(str[i]!='\0')
{
if(str[i]==' ')
{
word++;
ch++;
}
else
{
ch++;
}
i++;
}
printf("\nNumber of Characters = ");
cprintf("%d",ch);
printf("\nNumber of Words      = ");
cprintf("%d",word+1);
getch();

}
Output:

Program to find a Prime numbers in C Programming

Coding:

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j=0;
clrscr();
printf("\nEnter the number:");
scanf("%d",&n);

// *LOGIC*
for(i=1;i<=n;i++)
{
if(n%i==0)
j++;
}

if(j==2)
{
printf("\nEntered Number");
textcolor(YELLOW);
cprintf("%2d" ,n);
printf("is Prime");
}
else
{
printf("\nEntered Number");
textcolor(YELLOW);
cprintf("%2d",n);
printf(" is not a Prime",n);
}
getch();
}

Output: