C-programs

35
INDEX S.No . Program Page Date Sig n 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

description

here are the all basic and advanced C language programmes.. just refer them to enhance your skills and be the advanced programmer at your own .......

Transcript of C-programs

Page 1: C-programs

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 AIf percentage>=60 but <80% then grade BIf percentage>=40 but <60% then grade COtherwise 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

S.No. Program Page Date Sign

Page 2: C-programs

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.

Page 3: C-programs

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);

}

Page 4: C-programs

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);

}}

}

Page 5: C-programs

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);

elseprintf("\n%d is ODD" , n);

}

Page 6: C-programs

4. Program in C, to find largest, second largest and smallest among the given three numbers.

#include <stdio.h>

main(){

float n1 , n2 , n3;

printf("\nEnter 1st Number: ");scanf("%f" , &n1);

printf("\nEnter 2nd Number: ");scanf("%f" , &n2);

printf("\nEnter 3rd Number: ");scanf("%f" , &n3);

printf("\nSmallest < Second Largest < Largest");

if(n1 < n2 && n2 < n3){

printf("\n%f < %f < %f" , n1 , n2 , n3);}

else if(n1 < n3 && n3 < n2){

printf("\n%f < %f < %f" , n1 , n3 , n2);}

else if(n2 < n1 && n1 < n3){

printf("\n%f < %f < %f" , n2 , n1 , n3);}

else if(n2 < n3 && n2 < n1){

printf("\n%f < %f < %f" , n2 , n3 , n1);}

else if(n3 < n1 && n1 < n2){

printf("\n%f < %f < %f" , n3 , n1 , n2);}

else if(n3 < n2 && n2 < n1){

printf("\n%f < %f < %f" , n3 , n2 , n1);}

}

Page 7: C-programs

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 AIf percentage>=60 but <80% then grade BIf percentage>=40 but <60% then grade COtherwise 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");

elseprintf("\nFail");

}

Page 8: C-programs

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("\n\nEnter 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("\n\nRepeat again? ");scanf(" %c" , &c);

}while((c == 'y')||(c == 'Y'));

printf("\n<<Good Bye!>>");}

Page 9: C-programs

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);

}}

Page 10: C-programs

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("%d\t" , 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("%d\t" , prime);}

}

Page 11: C-programs

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("%d\t" , 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");

}}

Page 12: C-programs

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 add\n- to subtract\n* to multiply\n/ to divide\n");scanf(" %c",&option);

switch(option){

case '+': printf("%f\t+\t%f\t=\t%f",n1,n2,n1+n2);break;

case '-': printf("%f\t-\t%f\t=\t%f",n1,n2,n1-n2);break;

case '*': printf("%f\t*\t%f\t=\t%f",n1,n2,n1*n2);break;

case '/': printf("%f\t/\t%f\t=\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 !");}

}

Page 13: C-programs

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);

}

Page 14: C-programs

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 - A\n");

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 - B\n");

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 + B\a");

for(i = 0 ; i < rows ; i++){

printf("\n");

for(j = 0 ; j < cols ; j++){

printf("%d\t", a[i][j]);}

} }

Page 15: C-programs

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 - A\n\a");

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 - B\n\a");

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:\n\a");

for(i = 0 ; i < 3 ; i++){

printf("\n");

for(j = 0 ; j < 3 ; j++){

printf("%d\t", c[i][j]);}

} }

Page 16: C-programs

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;

}

Page 17: C-programs

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("\n\nValue of a = %d & Value of b = %d before swapping",a,b);

swap(aa,bb);

printf("\n\nValue 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;

}

Page 18: C-programs

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 ("\n\aDone..!");}

else{

printf ("\n%lu", fibo);prev_num = next_num;next_num = fibo;i++;fibonacci (prev_num , next_num);//recursion

}}

Page 19: C-programs

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);

}

Page 20: C-programs

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");

}

Page 21: C-programs

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);elseprintf("\"%s\" is NOT a PALINDROME",s1);getch();

}

Page 22: C-programs

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("\n\nThe 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("\n\nAfter Bubble Sort:\n");

for (i = 0 ; i < n ; i++){

printf("\nA[%d] = %d", i , a[i]);}

}

Page 23: C-programs

21. Program in C, to sort given elements of array using selection sort

Page 24: C-programs

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("\n\nEnter 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);}

}

Page 25: C-programs

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("\n\nArray after Bubble Sort:");

for (i = 0 ; i < n ; i++){

printf("\nA[%d] = %d", i , a[i]);}

printf("\n\nEnter Item to search: ");scanf("%d" , &value);

index = -1;beg = 0;end = n-1;mid = (beg + end) / 2;

while(end >= beg){

mid = (beg + end) / 2;

Page 26: C-programs

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);}

}

Page 27: C-programs

24. Program in C, to copy contents of one file to another

Page 28: C-programs

25. Program in C, to count number of characters, vowels, constant and words in a given file.