Showing posts with label c examples with code. Show all posts
Showing posts with label c examples with code. 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

C Program to Check Largest Number in Two Variable

It is a sample C program to check largest number in two variable.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int a,b;
clrscr();
printf("***PRINT LARGEST VALUE AMONG TWO OR MORE
NUMBER***");
printf("Enter the value of a:--");
scanf("%d",&a);
printf("Enter the value of b:--")
scanf("%d",&b);
if(a>b)
{
printf("a is greater");
}
else
{
printf("b is greater");
}
getch();
}

Output


Largest Number in Two Variable in C Program

Program in C to Find Even or Odd Number

It is a sample c program to find even or odd number.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int n ;
clrscr();
printf("\n ***Check 'even' ** or ** 'odd'**** ");
printf("\n Enter the number to check:-- ");
scanf("%d",&n);
if (n%2==0)
printf("\n Given no is even:--");
else
printf("\n Given no is odd:--");
getch();
}

Output


Even or Odd Number in C Program

Saturday, December 4, 2010

C program to find transpose of a matrix

Hello I want to discuss about the c program for transpose of the matrix.
Transpose matrix means interchange the rows with the columns and columns with the rows.

Program Code


#include < stdio.h >
#include < conio.h >
void main()
{
int arr[3][3],i,j;
clrscr();
printf("Enter elements for the array \n");
for(i=0;i < 3;i++)
{
for(j=0;j < 3;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("Original array entered by the user is \n");
for(i=0;i < 3;i++)
{
for(j=0;j < 3;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
printf("\n Transpose of the array is \n");
for(i=0;i < 3;i++)
{
for(j=0;j < 3;j++)
printf("%d ",arr[j][i]);
printf("\n");
}
getch();
}

Input:


Enter elements for the array
1
2
3
4
5
6
7
8
9

Output:


Original array entered by the user is
1 2 3
4 5 6
7 8 9

Transpose of the array is
1 4 7
2 5 8
3 6 9