Showing posts with label sample c program. Show all posts
Showing posts with label sample c program. Show all posts

Saturday, December 24, 2011

C Program to Print Patterns of Stars

It is a sample c program to print patterns of stars.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int i,j,n,k;
printf (“Enter the value of n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-1;j++)
{
printf(“ ”);
}
for(k=1;k<=i;k++)
{
printf(“* ”);
}
printf(“\n”);
}
getch();
}

Output


Print Patterns of Stars

C Program to Print Patterns of Numbers

It is a sample c program to print patterns of numbers.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int i,j,n,k;
printf (“Enter the value of n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-1;j++)
{
printf(“ ”);
}
for(k=1;k<=i;k++)
{
printf(“1”);
}
printf(“\n”);
}
getch();
}

Output


Print Patterns of Numbers

Check Whether Given Number is Armstrong or Not in C Program

It is a sample c program to check whether given number is Armstrong or not.

Program Source Code


#include"stdio.h"
#include"conio.h"
#include"math.h"
void main()
{
int number, a , i , j , sum=0 , count=0;
clrscr();
printf(“Enter any number”);
scanf(“%d”, &number);
i=number;
j=number;
while(j>0)
{
j=j%10;
count++;
}
while(i>0)
{
a=i%10;
sum=sum + pow(a, count);
i=i/10;
}
if(sum==number)
printf(“%d is a Armstrong number”, number);
else
printf(“%d is a not Armstrong number”, number);
getch();
}

Output


Armstrong or Not in C Program

C Program to Generate Fibonacci Series

It is a sample c program to generate a Fibonacci series.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int a , b , c , i=1;
clrscr();
a=0;
b=1;
printf(“Fibonacci Series”);
printf(“%d\n”, a);
printf(“%d\n”, b);
while(i<=10)
{
c=a+b;
printf(“%d”, c);
a=b;
b=c;
i++;
}
getch();
}

Output


Generate Fibonacci Series Output

Tuesday, November 22, 2011

C Program to Swap Two Numbers

It is a sample c program to swap two number.

Program Source Code


#include"stdio.h"
#include"conio.h"
void swap(int,int);
void main()
{

int a,b;
clrscr();
printf("\n Enter value of a:-");
scanf("\n%d",&a);
printf("\n Enter value of b:-");
scanf("\n%d",&b);
printf("\n\n a before swapping is:--%d",a);
printf("\n\n b before swapping is:--%d",b);
swap(a,b);
printf("\n\n a after swapping is:--%d",a);
printf("\n\n b after swapping is:--%d",b);
getch();

}
void swap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
printf("\n\n a in swapping is:--%d",x);
printf("\n\n b in swapping is:--%d",y);
}

Output


c program to swap two number

C Program to Calculate Factorial of a Number

It is a sample c program to calculate factorial of a number.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int fact=1,i,n;
clrscr();
printf("\n ***FIND THE FACTORIAL NUMBER OF ANY
NUMBER***");
printf("\n Enter the value of n:--");
scanf("\n %d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
printf("\n factorialis:-- %d",fact);
}
getch();
}

Output


Calculate Factorial of a Number

Saturday, November 19, 2011

C Program for Check Armstrong Number

It is a sample C program to find accepted number is Armstrong number on not.


Program Source Code
//Write a program to check accepted number is armstrong number or not.
#include"stdio.h"
#include"conio.h"
void main()
{
  int i,no,r,sum=0;
  clrscr();
  printf("*******Armstrong Number******");
  printf("\n Enter a number");
  scanf("%d",&no);
  i=no;
  while(no!=0)
   {
    r=no%10;
    sum=sum+(r*r*r);
    no=no/10;
   }
   if(sum==i)
    printf("Number is a Armstrong number\n");
   else
    printf("Number is not a armstrong number\n");
   getch();
}


Output