APM Welcome, APM North West Network Conference, Synergies Across Sectors
C-programs
1. INDEX
S.No. Program Page Date Sign
1. Program in C, to calculate area and circumference of the circle.
2. Program in C, to find the real roots of a quadratic equation.
3. Program in C, to find whether a number is odd or even.
4.
Program in C, to find largest, second largest and smallest among
the given three numbers.
5.
Program in C, to calculate grade of a student having three
subjects .The calculation of grade be according to the condition
as following:
If percentage >=80 then grade A
If percentage>=60 but <80% then grade B
If percentage>=40 but <60% then grade C
Otherwise fail
6. Program in C, to find the reverse of a given integer number.
7. Program in C, to find the factorial of a given number.
8. Program in C, to print prime numbers between 1 to 100.
9.
Program in C, to print the following Series.
a). 1 b). *
2 1 * *
3 2 1 * * *
4 3 2 1 * * * *
10.
Program in C, to perform arithmetic calculations on integers
using switch case statement
11.
Program in C, to find maximum and minimum element in a given
array
12. Program in C, to perform addition of two matrices
13. Program in C, to perform multiplication of two matrices
14. Program in C, to find X to power Y using user defined function
15. Program in C, to swap two numbers using call by reference
16.
Program in C, to generate first 25 Fibonacci numbers using
recursion
2. S.No. Program Page Date Sign
17.
Program in C, to process the student data –name, class, roll
number using structures
18 Program in C, to compare two strings using strcmp () function
19.
Program in C, to find whether a string is palindrome or not
without using string functions
20. Program in C, to sort given elements of array using bubble sort
21. Program in C, to sort given elements of array using selection sort
22. Program in C, to search an element using linear search
23. Program in C, to search an element using binary search
24. Program in C, to copy contents of one file to another
25.
Program in C, to count number of characters, vowels, constant
and words in a given file.
3. 1. Program in C, to calculate area and circumference of the circle.
#include <stdio.h>
main()
{
float radius , area , circumference;
printf("nEnter Radius : ");
scanf(" %f" , &radius);
do
{
area = 3.14159 * radius * radius;
circumference = 2 * 3.14159 * radius;
printf("nArea = %0.2f" , area);
printf("nCircumference = %0.2f" , circumference);
printf("nEnter new Radius (0 to quit): ");
scanf(" %f" , &radius);
}
while(radius != 0);
}
4. 2. Program in C, to find the real roots of a quadratic equation.
#include <stdio.h>
#include <math.h>
main()
{
float a , b , c , det , x1 , x2 , x;
printf("nEnter the values:");
printf("na = ");
scanf("%f" , &a);
printf("nb = ");
scanf("%f" , &b);
printf("nc = ");
scanf("%f" , &c);
if(a == 0)
{
x = -c/b;
printf("nRoots are Linear:tx = %f" , x);
}
else
{
det = b*b - 4*a*c;
if(det == 0)
{
x = -b / (2*a);
printf("nRoots are Identical:tx1 = x2 = %f" , x);
}
else if(det < 0)
{
printf("nRoots are Complex.");
}
else
{
x1 = (-b + sqrt(det)) / (2*a);
x2 = (-b - sqrt(det)) / (2*a);
printf("nRoots are Real:");
printf("nx1 = %f" , x1);
printf("nx2 = %f" , x2);
}
}
}
5. 3. Program in C, to find whether a number is odd or even.
#include <stdio.h>
void main()
{
int n;
printf("nEnter a Number: ");
scanf("%d" , &n);
if(n%2 == 0)
printf("n%d is EVEN" , n);
else
printf("n%d is ODD" , n);
}
7. 5.
Program in C, to calculate grade of a student having three subjects .The calculation of
grade be according to the condition as following:
If percentage >=80 then grade A
If percentage>=60 but <80% then grade B
If percentage>=40 but <60% then grade C
Otherwise fail
#include <stdio.h>
main()
{
float p;
printf("nPercentage of Student ? : ");
scanf("%f" , &p);
if(p >= 80)
printf("nGrade = A");
else if(p >= 60 && p < 80)
printf("nGrade = B");
else if(p >= 40 && p < 60)
printf("nGrade = C");
else
printf("nFail");
}
8. 6. Program in C, to find the reverse of a given integer number.
#include <stdio.h>
main()
{
long unsigned n, rev , rem ;
char c;
do
{
rev=0;
printf("nnEnter number: ");
scanf(" %lu" , &n);
while(n > 0)
{
rem = n%10;
rev = rev * 10 + rem;
n = n/10;
}
printf("nReverse of the number is: %lu" , rev);
printf("nnRepeat again? ");
scanf(" %c" , &c);
}
while((c == 'y')||(c == 'Y'));
printf("n<<Good Bye!>>");
}
9. 7. Program in C, to find the factorial of a given number.
#include <stdio.h>
main()
{
int n ;
long unsigned fact = 1 ;
printf("nEnter Number : ");
scanf("%d" , &n);
if(n==1)
printf("%d ! (factorial) = %d" , n , fact);
else
{
for (int i = 1 ; i <= n ; i++)
{
fact = fact * i;
}
printf("%d ! (factorial) = %lu" , n , fact);
}
}
10. 8. Program in C, to print prime numbers between 1 to 100.
#include <stdio.h>
main()
{
int u , l , prime , num , div;
printf("nEnter lower limit: ");
scanf("%d" , &l);
printf("nEnter upper limit: ");
scanf("%d" , &u);
if(l < 2)
{
l = 2;
}
printf("nPrime Number between %d and %d are:n" , l , u);
if(l == 2)
{
printf("%dt" , l);
l++;
}
for(num=l ; num <= u ; num++)
{
for(div=2 ; div <= num-1 ; div++)
{
if(num%div != 0)
{
prime=num;
continue;
}
else
{
prime=0;
break;
}
}
if(prime==0)
continue;
else
printf("%dt" , prime);
}
}
11. 9.
Program in C, to print the following Series.
a). 1 b). *
2 1 * *
3 2 1 * * *
4 3 2 1 * * * *
#include <stdio.h>
main()
{
int rows , n , i;
printf("nHow many rows ? ");
scanf("%d" , &rows);
for ( n = 1 ; n <= rows ; n++ )
{
i=1;
while(i <= n)
{
printf("%dt" , n);
i++;
}
printf("n");
}
}
…………………………………………………………………………………………………………………………………………………………………………………………………….
#include <stdio.h>
main()
{
int rows , i , n;
printf("nHow many rows ? ");
scanf("%d" , &rows);
for(i = 1 ; i <= rows ; i++)
{
for(n = rows-i ; n >= 1 ; n--)
{
printf(" ");
}
n=1;
while(n <= i)
{
printf("* ");
n++;
}
printf("n");
}
}
12. 10. Program in C, to perform arithmetic calculations on integers using switch case statement
#include <stdio.h>
main()
{
float n1 , n2;
char option;
printf("nEnter a number: ");
scanf(" %f",&n1);
printf("nEnter another number: ");
scanf(" %f",&n2);
printf("nSelect an operation:n+ to addn- to subtractn* to
multiplyn/ to dividen");
scanf(" %c",&option);
switch(option)
{
case '+': printf("%ft+t%ft=t%f",n1,n2,n1+n2);
break;
case '-': printf("%ft-t%ft=t%f",n1,n2,n1-n2);
break;
case '*': printf("%ft*t%ft=t%f",n1,n2,n1*n2);
break;
case '/': printf("%ft/t%ft=t%f",n1,n2,n1/n2);
break;
default: printf("nInvalid Option selected");
main();
}
printf("nRepeat Program ? (Y/N) ");
scanf(" %c",&option);
if((option == 'Y') || (option == 'y'))
{
main();
}
else
{
printf("nGood Bye !");
}
}
13. 11. Program in C, to find maximum and minimum element in a given array
#include <stdio.h>
#define size 10
main()
{
float a[size] , min , max;
int i;
printf("nEnter Array Elements:n");
for(i = 0 ; i < size ; i++)
{
printf("nA[%d] = ",i+1);
scanf("%f",&a[i]);
}
max = a[0];
min = a[0];
for(i = 1 ; i < size ; i++)
{
if(a[i] > max)
{
max = a[i];
}
if(a[i] < min)
{
min = a[i];
}
}
printf("nMaximum value = %0.2f" , max);
printf("nMinimum value = %0.2f" , min);
}
14. 12. Program in C, to perform addition of two matrices
#include <stdio.h>
#define rows 3
#define cols 3
main()
{
int a[rows][cols] , b[rows][cols] , i , j;
printf("nEnter Elements for Matrix - An");
for(i = 0 ; i < rows ; i++)
{
for(j = 0 ; j < cols ; j++)
{
printf("A[%d][%d] = ", i,j);
scanf("%d" , &a[i][j]);
}
}
printf("nEnter Elements for Matrix - Bn");
for(i = 0 ; i < rows ; i++)
{
for(j = 0 ; j < cols ; j++)
{
printf("B[%d][%d] = ", i,j);
scanf("%d" , &b[i][j]);
a[i][j] += b[i][j];
}
}
printf("nA + Ba");
for(i = 0 ; i < rows ; i++)
{
printf("n");
for(j = 0 ; j < cols ; j++)
{
printf("%dt", a[i][j]);
}
}
}
15. 13. Program in C, to perform multiplication of two matrices
#include <stdio.h>
main()
{
int a[3][3] , b[3][3] , c[3][3] , i , j , k;
printf("nEnter Elements for Matrix - Ana");
for(i = 0 ; i < 3 ; i++)
{
for(j = 0 ; j < 3 ; j++)
{
printf("A[%d][%d] = ", i,j);
scanf("%d" , &a[i][j]);
}
}
printf("nEnter Elements for Matrix - Bna");
for(i = 0 ; i < 3 ; i++)
{
for(j = 0 ; j < 3 ; j++)
{
printf("B[%d][%d] = ", i,j);
scanf("%d" , &b[i][j]);
}
}
for(i = 0 ; i < 3 ; i++)
{
for(j = 0 ; j < 3 ; j++)
{
c[i][j] = 0;
for(k = 0 ; k < 3 ; k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("nA x B:na");
for(i = 0 ; i < 3 ; i++)
{
printf("n");
for(j = 0 ; j < 3 ; j++)
{
printf("%dt", c[i][j]);
}
}
}
16. 14. Program in C, to find X to power Y using user defined function
#include<stdio.h>
int power_function(int , int);
main()
{
int number , power , i , result;
printf("Enter Number: ");
scanf("%d",&number);
printf("Enter power: ");
scanf("%d",&power);
printf("%d raised to the power %d is %d",number , power,
power_function(number , power));
}
int power_function(int num , int pow)
{
int i , res = num;
for(i = 1 ; i < pow ; i++)
{
res = res * num;
}
return res;
}
17. 15. Program in C, to swap two numbers using call by reference
#include<stdio.h>
#include<conio.h>
void swap (int *a , int *b);
main()
{
int a,b,*aa,*bb;
clrscr();
a=1234;
b=5678;
aa=&a;
bb=&b;
printf("nnValue of a = %d & Value of b = %d before swapping",a,b);
swap(aa,bb);
printf("nnValue of a = %d & Value of b = %d after swapping",a,b);
getch();
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
18. 16. Program in C, to generate first 25 Fibonacci numbers using recursion
#include<stdio.h>
void fibonacci (long unsigned prev_num, long unsigned next_num);
main()
{
static double prev_num=0, next_num=1;
printf ("nFollowing are the first 25 Numbers of the Fibonacci
Series:n");
printf("n%u",prev_num + next_num);
fibonacci (prev_num , next_num);
}
void fibonacci (long unsigned prev_num, long unsigned next_num)
{
static int i=1;
long unsigned fibo;
fibo = prev_num + next_num;
if (i == 25)
{
printf ("naDone..!");
}
else
{
printf ("n%lu", fibo);
prev_num = next_num;
next_num = fibo;
i++;
fibonacci (prev_num , next_num);//recursion
}
}
19. 17. Program in C, to process the student data – name, class, roll number using structures
#include<stdio.h>
main()
{
struct student
{
char s_name[30];
char s_class[10];
int s_num;
};
student s1;
printf("nEnter data for Student:n");
printf("nName: ");
gets(s1.s_name);
printf("nClass: ");
gets(s1.s_class);
printf("nRoll Number: ");
scanf("%d", &s1.s_num);
printf("n-------------------------nData Entered by you is:n");
printf("nName: ");
puts(s1.s_name);
printf("nClass: ");
puts(s1.s_class);
printf("nRoll Number: %d", s1.s_num);
}
20. 18 Program in C, to compare two strings using strcmp () function
#include<stdio.h>
#include<string.h>
main()
{
char str_1[20] , str_2[20];
printf("String_1: "); gets(str_1);
printf("String_2: "); gets(str_2);
int i = strcmp (str_1 , str_2);
if (i == 0)
puts("nComparison Result: Strings are Equal");
if(i < 0)
puts("nComparison Result: Strings NOT Equal, String_1 is smaller");
if(i > 0)
puts("nComparison Result: Strings NOT Equal, String_2 is smaller");
}
21. 19. Program in C, to find whether a string is palindrome or not without using string functions
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20] , s2[20];
clrscr();
printf("nEnter a string: ");
scanf("%s",s1);
strcpy(s2 , s1);
strrev(s2);
if(strcmp(s1,s2)==0)
printf(""%s" is a PALINDROME",s1);
else
printf(""%s" is NOT a PALINDROME",s1);
getch();
}
22. 20. Program in C, to sort given elements of array using bubble sort
#include <stdio.h>
#define size 50
main()
{
int a[size] , i , j , temp , value , n = 0 , index = -1;
printf("nEnter Array Elements:n[Enter 0 (zero) to Stop]n");
for( i = 0 ; i < size ; i++)
{
printf("nA[%d] = ",i);
scanf("%d", &value);
if(value == 0)
{
break;
}
else
{
a[i] = value;
n++;
}
}
puts("nnThe Array you have Entered is:n");
for (i = 0 ; i < n ; i++)
{
printf("nA[%d] = %d", i , a[i]);
}
for (i = 0 ; i < n ; i++)
{
for (j = i+1 ; j < n ; j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
puts("nnAfter Bubble Sort:n");
for (i = 0 ; i < n ; i++)
{
printf("nA[%d] = %d", i , a[i]);
}
}
23. 21. Program in C, to sort given elements of array using selection sort
24. 22. Program in C, to search an element using linear search
#include <stdio.h>
#define size 50
main()
{
int a[size] , i , value , n = 0 , index = -1;
printf("nEnter Array Elements:n[Enter 0 (zero) to Stop]n");
for( i = 0 ; i < size ; i++)
{
printf("nA[%d] = ",i);
scanf("%d", &value);
if(value == 0)
{
break;
}
else
{
a[i] = value;
n++;
}
}
for (i = 0 ; i < n ; i++)
{
printf("nA[%d] = %d", i , a[i]);
}
printf("nnEnter Item to Search: ");
scanf("%d",&value);
for (i = 0 ; i <= n ; i++)
{
if(a[i] == value)
{
index = i;
break;
}
}
if(index == -1)
{
printf("nItem not found");
}
else
{
printf("nItem found at Index: %d", index);
}
}
25. 23. Program in C, to search an element using binary search
#include <stdio.h>
#define size 50
main()
{
int a[size] , i , j , temp , value , n = 0 , index , beg , end , mid;
printf("nEnter Array Elements:n[Enter 0 (zero) to Stop]n");
for( i = 0 ; i < size ; i++)
{
printf("nA[%d] = ",i);
scanf("%d", &value);
if(value == 0)
{
break;
}
else
{
a[i] = value;
n++;
}
}
for (i = 0 ; i < n-1 ; i++)
{
for (j = i+1 ; j < n ; j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("nnArray after Bubble Sort:");
for (i = 0 ; i < n ; i++)
{
printf("nA[%d] = %d", i , a[i]);
}
printf("nnEnter Item to search: ");
scanf("%d" , &value);
index = -1;
beg = 0;
end = n-1;
mid = (beg + end) / 2;
while(end >= beg)
{
mid = (beg + end) / 2;
26. if(value == a[mid])
{
index = mid;
break;
}
else if(value < a[mid])
{
end = mid-1;
continue;
}
else if(value > a[mid])
{
beg = mid+1;
continue;
}
}
if(index < 0)
{
printf("nItem not Found");
}
else
{
printf("Item Found at Index: %d" , index);
}
}
27. 24. Program in C, to copy contents of one file to another
28. 25. Program in C, to count number of characters, vowels, constant and words in a given file.