|
C - Sample Programms
|
Programe
C Software Download and Program Compilation
Steps to Install C and C++ IDE
1. Search in the Web Browser as free download Turbo C Software

2. Install in to your System
3. Open new document and type your C coding
4. Save with filename.c (eg. first.c)
5. Compile saved c program: (In Menu -> Execute -> Compile or Press F9 Function Key)
6. Run compiled program without error: (In Menu -> Execute -> Run or Press F10 Function Key)
7. Or use Compile & Run (Or Use F11 Function Key)
8. See your output in seperate DOS prompt window.
|
Program 1: Display a String
Display String
#include<stdio.h>
int main()
{
printf("Welcome to C Program");
return 0;
}
============================
Output
Welcome to C Program
============================
|
Program 2: Sum of given Digits
Sum of given Digits
#include<stdio.h>
int main()
{
int n, sum = 0, m;
printf("Enter a number:");
scanf("%d", &n);
while(n>0)
{
m = n%10;
sum = sum + m;
n = n/10;
}
printf("Sum is = %d", sum);
return 0;
}
============================
Output
Enter a number:12345
Sum is=15
============================
|
Program 3: Add or Even Numbers
Add or Even Numbers
#include<stdio.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}
============================
Output
Enter an integer: 7
7 is odd.
============================
|
Program 4: Prime number
Prime Number
#include<stdio.h>
void main(void)
{
int n,i,m=0,flag=0;
printf("Enter the number to check prime \n");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
}
}
============================
Output
Enter the number to check prime:45
Number is not prime
Enter the number to check prime:19
Number is prime
============================
|
Program 5: Table of number
Table of number
#include<stdio.h>
void main(void)
{
int i, tab, len;
printf("Enter the Table number\n");
scanf("%d",&tab);
printf("Enter the Table length\n");
scanf("%d",&len);
printf("\n%d Tables \n",tab);
printf("-------- \n");
for(i=1;i<=len;i++)
{
printf("%d X %d = %d\n",i,tab,i*tab);
}
}
============================
Output
Enter the Table number
6
Enter the Table length
15
6 Tables
-----------
1 X 6 = 6
2 X 6 = 12
3 X 6 = 18
4 X 6 = 24
5 X 6 = 30
6 X 6 = 36
7 X 6 = 42
8 X 6 = 48
9 X 6 = 54
10 X 6 = 60
11 X 6 = 66
12 X 6 = 72
13 X 6 = 78
14 X 6 = 84
15 X 6 = 90
============================
|
Program 6: Factorial
Factorial (1x2x3x4x5...n)
#include<stdio.h>
int main()
{
int i, fact=1, num;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
fact=fact*i;
}
printf("Factorial of %d is: %d",num,fact);
return 0;
}
============================
Output
Enter a number: 6
Factorial of 6 is: 720
============================
|
Program 7: Armstrong number
Armstrong numberis a number that is equal to the sum of cubes of its digits.
For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. Example of 153 is an Armstrong number is
1. 153 = (1*1*1)+(5*5*5)+(3*3*3)
2. where:
3. (1*1*1)=1
4. (5*5*5)=125
5. (3*3*3)=27
6. So:
7. 1+125+27=153
#include<stdio.h>
int main()
int n,r,sum=0,temp;
printf("Enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("Armstrong Number");
else
printf("Not Armstrong Number");
return 0;
}
============================
Output
enter the number=153
Armstrong Number
enter the number=5
Not Armstrong Number
============================
|
Program 8: Palindrome number
Palindrome number
#include<stdio.h>
int main()
{
int num, remain, sum=0;
printf("Enter the number where polindrom or not\n");
scanf("%d", &num);
int temp;
temp = num;
while(num>0)
{
remain = num%10;
sum = (sum*10)+remain;
num = num/10;
}
if(temp==sum)
{
printf("%d This is polintrom no", temp);
}
else
{
printf("%d This is NOT polintrom no", temp);
}
return 0;
}
============================
Output
Enter the number where polindrom or not
252
252 This is polintrom no
============================
|
Program 9: Fibonacci Series
Fibonacci Series
#include<stdio.h>
int main()
{
int num, n1, n2, n3, i;
n1 = 0;
n2 = 1;
printf("Enter the number \n");
scanf("%d", &num);
printf("%d \t %d \t",n1, n2
for(i=2; i<=num; i++)
{
n3 = n1 + n2;
printf("%d \t", n3);
n1 = n2;
n2 = n3;
}
return 0;
}
============================
Output
Enter the number
10
0 1 1 2 3 5 8 13 21 34 55
============================
|
Program 10: Reverse Numbering
Reverse Numbering
#include<stdio.h>
int main()
{
int n, reverse=0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number: %d",reverse);
return 0;
}
============================
Output
Enter a number: 12345
Reversed Number: 54321
============================
|
Program 11: Reverse String
Reverse String
#include<stdio.h>
int main()
{
char str[1000], rev[1000];
int begin, end, count = 0;
printf("Enter the string to reverse\n");
gets(str);
// Calculating string length
while (str[count] != '\0')
count++;
end = count - 1;
for (begin = 0; begin < count; begin++)
{
rev[begin] = str[end];
end--;
}
rev[begin] = '\0';
printf("%s\n", rev);
return 0
}
============================
Output
Enter the string to reverse
Welcome to c Programe
emargorP c ot emocleW
============================
|
Program 12: Swap two numbers (Two ways)
Swap two numbers using Temp variable and Math operators
// Using Temp variable
#include<stdio.h>
void swap(int a, int b)
{
int temp;
temp = a;
a = b
b = temp;
printf("After Swap value are %d, %d", a, b);
}
int main()
{
int q=10, w=20;
swap(q, w);
printf("\nBefore Swap value are %d, %d", q, w);
}
============================
Output
After Swap value are 20, 10
Before Swap value are 10, 20
============================
// Using math operators
#include<stdio.h>
int main()
{
int num1, num2;
printf("Enter number1 and number2 \n");
scanf("%d%d",&num1, &num2);
printf("Before Swap Number1=%d Number2=%d\n",num1,num2);
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
printf("\nAfter Swap Number1=%d Number2=%d",num1,num2);
return 0;
}
============================
Output
Enter number1 and number2
45 75
Before Swap Number1=45 Number2=75
After Swap Number1=75 Number2=45
============================
|
Program 13: Leap Year
Leap Year
#include<stdio.h>
int main()
{
int year;
printf("Enter a Year: \n");
scanf("%d", &year);
if (year % 400 == 0)
{
printf("%d is a Leap Year.", year);
}
else if (year % 100 == 0)
{
printf("%d is not a Leap Year.", year);
}
else if (year % 4 == 0)
{
printf("%d is a Leap Year.", year);
}
else
{
printf("%d is NOT a Leap Year.", year);
}
return 0;
}
============================
Output
Enter a Year:
2020
2020 is a Leap Year.
Enter a Year:
2021
2021 is NOT a Leap Year.
============================
|
Program 14: Alphabet Triangle
Program 15: Matrix Mulitiplication
Program 16: Decimal to Binary Convertion
Program 17: C Program without Main
Program 18: Assembly code in C
Program 19: Number in Characters
Program 20: User defined header file (filename.h)
Program 21: fork() Function (using in linux)
|
|
|