1.Sample C Program To Print Welcome Message.
#include
int main()
{
printf( "Welcome to C Programming World. \n" );
getchar();
return 0;
}
OUTPUT:
Welcome to C Programming World.
2.Sample C Program To Accept & Display A Number.
#include
#include
main()
{
int number;
printf("Enter an integer ");
scanf("%d", &number);
printf("Integer that you have entered is %d",number);
getch();
return 0;
}
OUTPUT:
Enter an integer 9
Integer you have entered is 9
3.Sample C program To Print First Ten Numbers.
#include
#include
main()
{
int c;
for ( c = 1 ; c <= 10 ; c++ ) printf("%d\n", c); getch(); return 0; } OUTPUT: 1 2 3 4 5 6 7 8 9 10 4.Sample C Program To Find Area & Circumference Of A Circle. #include
#include
void main()
{
int r;
float pi=3.14, area, ci;
clrscr();
printf("Enter radius of circle: ");
scanf("%d",&r);
area = pi * r * r;
printf("Area of circle =%f",area);
ci = 2 * pi * r;
printf("Circumference =%f",ci);
getch();
}
OUTPUT:
Enter radius of circle: 5
Area of circle =78.500000 Circumference =31.400002
5.Sample C Program To Swap Two Numbers Using Temporary Variables.
#include
#include
main()
{
int x, y, temp;
printf("Enter the value of x and y ");
scanf("%d %d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x = y;
y = temp;
printf("After Swapping\nx = %d\ny = %d\n",x,y);
getch();
return 0;
}
OUTPUT:
Enter the value of x and y 2
4
Before Swapping
x = 2
y = 4
After Swapping
x = 4
y = 2
6.Sample C Program To Swap Two Numbers Without Using Temp Variable.
#include
main()
{
int a, b;
printf("Enter two numbers to swap ");
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("a = %d\nb = %d\n",a,b);
return 0;
}
OUTPUT:
Enter two numbers to swap 2
4
a = 4
b = 2
7.Sample C Program To Find Factorial Of A Number.
#include
#include
void main()
{
int n, i, fact=1;
clrscr();
printf(" Enter any no: ");
scanf("%d", &n);
for( i = n; i >= 1; i-- )
{
fact = fact * i;
}
printf(" Factorial =%d",fact);
getch();
}
OUTPUT:
Enter any no: 5
Factorial = 120
8.Sample C Program To Print Fibonacci Series Upto 100.
#include
#include
void main()
{
int a = 1, b = 1, c = 0, i;
clrscr();
printf(" %d\t %d\t ", a, b);
for( i = 0; i <= 10; i++) { c = a + b; if(c < 100) { printf("%d\t",c); } a = b; b = c; } getch(); } OUTPUT: 1 1 2 3 5 8 13 21 34 55 89 9.Sample C Program To Calculate Simple Interest. #include
#include
void main()
{
int p, r, t, si;
clrscr();
printf(" Enter principle, rate of interest & time to find simple interest: ");
scanf(" %d %d %d",&p, &r, &t);
si = ( p * r * t ) / 100;
printf(" Simple interest = %d",si);
getch();
}
OUTPUT:
Enter principle, rate of interest & time to find simple interest: 1000
2
5
Simple interest = 100
10.Sample C Program To Check Whether A Number Is Prime Or Not.
#include
#include
void main()
{
int i, n ,r=0;
clrscr();
printf(" Enter any no: ");
scanf("%d",&n);
for( i = 2; i <= n - 1; i++ ) { if( n % i == 0) r = 1; break; } if( r == 0) printf(" Is prime "); else printf(" Not prime "); getch(); } OUTPUT: Enter any no: 8 Not prime 11.Sample C Program To Find The Greatest Among Three Numbers. #include
#include
void main()
{
int a, b, c;
clrscr();
printf(" Enter value of a, b & c: ");
scanf("%d %d %d",&a, &b, &c);
if( ( a>b ) && ( a>c ) )
printf(" a is greatest.");
if( ( b>c ) && ( b>a ) )
printf(" b is greatest.");
if( ( c>a ) && ( c>b ))
printf(" c is greatest.");
getch();
}
OUTPUT:
Enter value of a, b & c: 2
5
10
c is greatest.
12.Sample C Program To Find The Length Of A String.
#include
#include
#include
main()
{
char a[100];
int length;
printf(" Enter a string to calculate it's length ");
gets(a);
length = strlen(a);
printf(" Length of entered string is = %d\n",length);
getch();
return 0;
}
OUTPUT:
Enter a string to calculate it's length: noon
Length of entered string is = 4
13.Sample C Program To Check Whether A Number Is Armstrong Or Not.
#include
#include
main()
{
int n, sum = 0, temp, r;
printf( " Enter a number " );
scanf( " %d ", &n);
temp = n;
while( temp != 0 )
{
r = temp % 10;
sum = sum + r * r * r;
temp = temp / 10;
}
if ( n == sum )
printf( " Entered number is an armstrong number. " );
else
printf( " Entered number is not an armstrong number. " );
getch();
return 0;
}
OUTPUT:
Enter a number6
Entered number is not an Armstrong number.
14.Sample C Program To Find The Reverse Of A Number.
#include
#include
void main()
{
int n, a, r=0;
clrscr();
printf(" Enter any number to get its reverse: ");
scanf("%d",&n);
while(n>=1)
{
a = n % 10;
r = r * 10 + a;
n = n / 10;
}
printf(" Reverse = %d",r);
getch();
}
OUTPUT:
Enter any number to get its reverse: 65
Reverse = 56
15.Sample C Program To Check Whether A String Is Palindrome Or Not.
#include
#include
#include
int main()
{
char a[100], b[100];
printf(" Enter the string to check if it is a palindrome ");
gets(a);
strcpy(b , a);
strrev(b);
if (strcmp(a , b) == 0 )
printf(" Entered string is a palindrome.\n");
else
printf(" Entered string is not a pailndrome.\n");
getch();
return 0;
}
OUTPUT:
Enter the string to check if it is a palindrome: noon
Entered string is a palindrome
#include
int main()
{
printf( "Welcome to C Programming World. \n" );
getchar();
return 0;
}
OUTPUT:
Welcome to C Programming World.
2.Sample C Program To Accept & Display A Number.
#include
#include
main()
{
int number;
printf("Enter an integer ");
scanf("%d", &number);
printf("Integer that you have entered is %d",number);
getch();
return 0;
}
OUTPUT:
Enter an integer 9
Integer you have entered is 9
3.Sample C program To Print First Ten Numbers.
#include
#include
main()
{
int c;
for ( c = 1 ; c <= 10 ; c++ ) printf("%d\n", c); getch(); return 0; } OUTPUT: 1 2 3 4 5 6 7 8 9 10 4.Sample C Program To Find Area & Circumference Of A Circle. #include
#include
void main()
{
int r;
float pi=3.14, area, ci;
clrscr();
printf("Enter radius of circle: ");
scanf("%d",&r);
area = pi * r * r;
printf("Area of circle =%f",area);
ci = 2 * pi * r;
printf("Circumference =%f",ci);
getch();
}
OUTPUT:
Enter radius of circle: 5
Area of circle =78.500000 Circumference =31.400002
5.Sample C Program To Swap Two Numbers Using Temporary Variables.
#include
#include
main()
{
int x, y, temp;
printf("Enter the value of x and y ");
scanf("%d %d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x = y;
y = temp;
printf("After Swapping\nx = %d\ny = %d\n",x,y);
getch();
return 0;
}
OUTPUT:
Enter the value of x and y 2
4
Before Swapping
x = 2
y = 4
After Swapping
x = 4
y = 2
6.Sample C Program To Swap Two Numbers Without Using Temp Variable.
#include
main()
{
int a, b;
printf("Enter two numbers to swap ");
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("a = %d\nb = %d\n",a,b);
return 0;
}
OUTPUT:
Enter two numbers to swap 2
4
a = 4
b = 2
7.Sample C Program To Find Factorial Of A Number.
#include
#include
void main()
{
int n, i, fact=1;
clrscr();
printf(" Enter any no: ");
scanf("%d", &n);
for( i = n; i >= 1; i-- )
{
fact = fact * i;
}
printf(" Factorial =%d",fact);
getch();
}
OUTPUT:
Enter any no: 5
Factorial = 120
8.Sample C Program To Print Fibonacci Series Upto 100.
#include
#include
void main()
{
int a = 1, b = 1, c = 0, i;
clrscr();
printf(" %d\t %d\t ", a, b);
for( i = 0; i <= 10; i++) { c = a + b; if(c < 100) { printf("%d\t",c); } a = b; b = c; } getch(); } OUTPUT: 1 1 2 3 5 8 13 21 34 55 89 9.Sample C Program To Calculate Simple Interest. #include
#include
void main()
{
int p, r, t, si;
clrscr();
printf(" Enter principle, rate of interest & time to find simple interest: ");
scanf(" %d %d %d",&p, &r, &t);
si = ( p * r * t ) / 100;
printf(" Simple interest = %d",si);
getch();
}
OUTPUT:
Enter principle, rate of interest & time to find simple interest: 1000
2
5
Simple interest = 100
10.Sample C Program To Check Whether A Number Is Prime Or Not.
#include
#include
void main()
{
int i, n ,r=0;
clrscr();
printf(" Enter any no: ");
scanf("%d",&n);
for( i = 2; i <= n - 1; i++ ) { if( n % i == 0) r = 1; break; } if( r == 0) printf(" Is prime "); else printf(" Not prime "); getch(); } OUTPUT: Enter any no: 8 Not prime 11.Sample C Program To Find The Greatest Among Three Numbers. #include
#include
void main()
{
int a, b, c;
clrscr();
printf(" Enter value of a, b & c: ");
scanf("%d %d %d",&a, &b, &c);
if( ( a>b ) && ( a>c ) )
printf(" a is greatest.");
if( ( b>c ) && ( b>a ) )
printf(" b is greatest.");
if( ( c>a ) && ( c>b ))
printf(" c is greatest.");
getch();
}
OUTPUT:
Enter value of a, b & c: 2
5
10
c is greatest.
12.Sample C Program To Find The Length Of A String.
#include
#include
#include
main()
{
char a[100];
int length;
printf(" Enter a string to calculate it's length ");
gets(a);
length = strlen(a);
printf(" Length of entered string is = %d\n",length);
getch();
return 0;
}
OUTPUT:
Enter a string to calculate it's length: noon
Length of entered string is = 4
13.Sample C Program To Check Whether A Number Is Armstrong Or Not.
#include
#include
main()
{
int n, sum = 0, temp, r;
printf( " Enter a number " );
scanf( " %d ", &n);
temp = n;
while( temp != 0 )
{
r = temp % 10;
sum = sum + r * r * r;
temp = temp / 10;
}
if ( n == sum )
printf( " Entered number is an armstrong number. " );
else
printf( " Entered number is not an armstrong number. " );
getch();
return 0;
}
OUTPUT:
Enter a number6
Entered number is not an Armstrong number.
14.Sample C Program To Find The Reverse Of A Number.
#include
#include
void main()
{
int n, a, r=0;
clrscr();
printf(" Enter any number to get its reverse: ");
scanf("%d",&n);
while(n>=1)
{
a = n % 10;
r = r * 10 + a;
n = n / 10;
}
printf(" Reverse = %d",r);
getch();
}
OUTPUT:
Enter any number to get its reverse: 65
Reverse = 56
15.Sample C Program To Check Whether A String Is Palindrome Or Not.
#include
#include
#include
int main()
{
char a[100], b[100];
printf(" Enter the string to check if it is a palindrome ");
gets(a);
strcpy(b , a);
strrev(b);
if (strcmp(a , b) == 0 )
printf(" Entered string is a palindrome.\n");
else
printf(" Entered string is not a pailndrome.\n");
getch();
return 0;
}
OUTPUT:
Enter the string to check if it is a palindrome: noon
Entered string is a palindrome
No comments:
Post a Comment