Showing posts with label cprogramming. Show all posts
Showing posts with label cprogramming. Show all posts

Thursday, December 2, 2010

C Program For Floyd's Triangle

What is a Floyd's Triangle
It is a pattern of numbers arranging in this format.
Format of numbers like
1
2 3
4 5 6
7 8 9 10


Program Code


//C program to print Floyd’s triangle
#include < stdio.h>
#include < conio.h>
void main()
{
int i , j , r ;
int num=0 ;
clrscr();
printf(“How many rows you want in the triangle:”);
scanf(“%d”,&r);
for(i=1;i<=r;i++)
{
for(j=1;j<=i;j++)
{
printf(“\t%d”,++num);
}
printf(“\n”);
}
getch();
}

Input/Output


floyd triangle

Monday, November 15, 2010

c program to check number is prime or not

Program Code


// Check whether given number is prime number and not prime number
#include "stdio.h"
#include "conio.h"
void main()
{
int num,i,z;
printf("enter the value of num");
scanf("%d",&num);
i= 2;
while (i less then num)
{
if(num%i==0)
{
z=1;
}
i=i+1;
}
printf("After checking the result is");
if(z== 1)
{
printf("given number is not prime number");
}
else
{
printf("given number is prime number");
}
getch();
}

Input/Output


enter the value of num 7
After checking the result is
given number is prime number


What is Prime Number?


Prime number is a natural number which is divisible by 1 and itself.
Example 7

First 25 Prime Number :-
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97

Wednesday, November 10, 2010

C Program to calculate factorial of given number

You write this code into any c compiler and get factorial of given number by run this code.

C Program Code
//factorial of a number
#include "stdio.h"
#include "conio.h"
void main()
{
int fact=1,n;
clrscr();
printf("enter any number");
scanf("%d",&n);
while(n>0)
{
fact=fact*n;
n--;
}
printf("\n factorial of given number= %d",fact);
getch();
}




Input/Output
enter any number 4
factorial of given number= 24


Factorial of 4 = 4*3*2*1 =24