0% found this document useful (0 votes)
27 views20 pages

CP Lab Manual

The document is a lab manual for Programming in C, intended for first semester BCA students. It contains various C programs with algorithms, including calculations for area and circumference of a circle, finding the largest of three numbers, generating Fibonacci series, and more. Each program is accompanied by its respective algorithm and code implementation.

Uploaded by

Sahana M.k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
27 views20 pages

CP Lab Manual

The document is a lab manual for Programming in C, intended for first semester BCA students. It contains various C programs with algorithms, including calculations for area and circumference of a circle, finding the largest of three numbers, generating Fibonacci series, and more. Each program is accompanied by its respective algorithm and code implementation.

Uploaded by

Sahana M.k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 20
Name:- Programming in C Lab Manual First semester BCA PSSSHE Programming in C Lab Manual ge 1. ¢ Program to find area and circumference of circle Algorithm : Start 2: Input radius 3: let pi = 3.14 4: area = pi * radius * radius print area 6: circumference = 2 * pi * radius 7: print circumference Program: #include aint main() { int rad, float PI = 3.14, area, ci; printf ("\nEnter radius of circle: scanf("%d", grad); area = PI * rad * rad; print£("\nArea of circle : %f ", area); ci = 2 * PI * rad; printf ("\nCixcumference : $f ", ci); return (0); } 2. program to find largest of three given numbers Algorithm Step 1: Start Step 2: Ask the user to enter three integer values. Step 3: Read the three integer values in numi, num2, and num3 (integer variables) . Step 4: Check if numl is greater than num2. Step 5: If true, then check if numl is greater than num3. 1.I£ true, then print ‘numl’ as the greatest number. 2.1£ false, then print ‘num3’ as the greatest number. Step 6: If false, then check if num2 is greater than num3. 1.I£ true, then print ‘num2’ as the greatest number. 2.1£ false, then print ‘num3’ as the greatest number. Step 7:Stop. Programming in C Lab Manual Ba Program: #include void main() { int numi, mum2, num3; print£ ("Enter the values of num1, num2 and num3\n") ; 7 scanf("%d %d ta", gnuml, Snum2, &num3) ; printé("numi = $d\tnum2 = $d\tnum3 = %d\n", num1, num2, num3); if (numl > num2) { if (numi > num3) { print£(™uml is the greatest among three \n"); } else { printf ("num3 is the greatest among three \n"); } } else if (num2 > num3) print£("num2 is the greatest among three \n"); else printf (*num3 is the greatest among three \n"); } 3. Program to compare library functions of math.h with user user defined function Algorithm 1. Start 2. Accept one number from user. 3. Use the function sqrt () to find square root. 4. Print the result. 5. End 3a) Square root of a number without using the math header in Cc include include int main () { int number; float result; "5 print£("Enter a Number seanf (*$d", snumber) ; Programming in C Lab Manual BB result= sqrt (number) ; print£ ("Square root of %d is t1f\n", number, result); return (0); } 3b) Squaxe xoot of a number without using the math header in ¢ Algorithm 1. Start 2. Accept one number from user. 3. store the half of the given number 4. Iterate until sqrt is different of temp, that is upd 5. replace values of sqrt using sart=(number/temp+temp) 6. Print the result. 7. End : Program #include void main () { int number; float temp, sqrt; print£ ("Provide the number: \n"); scanf ("$d", &number) ; sqrt = number / 2; temp = 0; while(sqrt != temp) { temp = sqrt; sqrt = ( mumber/temp + temp) / } print£ ("The square root of 'td' is 't£'", number, sqrt) } 4. C program to generate the factorial of a given number Factorial is calculated as the product of ali positive than or equal to n. Programming in C Lab Manual Ba Algorithm Step 1: start Step 2: read number Step 3: set fact=1, i Step 4: check condition icsnumber if false go to step 7 Step 5: fact=fact*i; Step 6: update i=i+1go to step 4 Step 7:-Display fact. Step 8: Stop Program: #includecstdio.h> int main() { int i,fact-1,number; printf ("Enter a number: "); scanf ("$d", gnumber) ; for (i=1;ic=number;i++) { factefact*i; } printf ("Factorial of %d is: % return 0; } " number, fact) ; 5. ¢ program to generate the N Fibonacci series ‘A series of numbers in which each number is the sum of the two preceding or previous numbers is called Fibonacci Series. Algorithm: Step 1: Start Declare variable £1, £2, £3, n, i Initialize variable £1=0, £2=1 Read n from user Print £1 and £2 Repeat until ic=n-2using for loop Step 6.1: £3=£1+£2 Step 6.2: print £3 Step 6.3: f1=£2, £2=£3 Step 6.4: iei+l Step 7: Stop Program: #include int main() { int £1-0, £2-1, £3, i, n; printf ("Enter the value of n\n"); scanf (*td\n", &n) ; printf ("the first %d fibonacci series are: \n”,n); printé("td td ",£1,£2); = a for(i = 1; n-2; itt) { £3 = £1 + £2; printé("%d ", £3); £1 = £2; £2 = £3; } ’ return 0; 6. Program to read a number, find the sum of the digits, reverse the number and check it for palindrome Algorithm Step 1: declare variables Step 2: Get number by user Step 3: Get the modulus/remainder of the number Step 4: sum the remainder of the number Step 5: Divide the number by 10 Step 7: Hold the number in temporary variable Step 8: Reverse the number Step 9: Compare the temporary number with reversed number Step 10: If both numbers are same, print palindrome number Else print not palindrome number 1: 2 3: 4 5: Step 6: Repeat the step 2 while mumber is greater than 0- 7 8 9 10 Program: #include void main() { int n,sum=0,m, number, rev=0,temp,T7 print£ ("Enter a number:"); scanf ("%d", &n) ; number=1 while (n>0) ~ B { m=nt10; Programming in C Lab Manual B printf ("Sum of the digits is: a", sum) ; temp= number; while (number >0) { = xenumber%10; revs (rev*10) +r; number=number/10; } if (temp==rev) print£("\nThe given number is a palindrome") ; else printf("\n The given number is not a palindrome") ; n e 4 7. Program to read numbers from keyboard continuously till the user presses 999 and to find the sum of only positive numbers Algorithm Stepl: Start Step 2: initialize sum to zero Step 3: Read thenumbers from keyboard. Step 4:if entered number is greater than zero thencalculate sum=sum+number Step S:repeat the step 3 and 4 until the entered number is 999 Step 6:print the sum Step 7: stop Program: #Hincludecstdio.h> int main() { int num, sum=0; printf ("Enter Postive numbers to sum while (1) f { scanf ("%d", &num) Programming in C Lab Manual es else if (num>0) sum=sum+num; } printé("$d", sum) ; return 0; 8. Program to find the roots of quadratic equation (Demonstration of else-if ladder) Algorithm: 2. Initialize all the variables used in the quadratic equation. 2. Take inputs of all coefficient variables x, y and z from the user. 3. And then, find the discriminant of the quadratic equation using the formula: discriminant = b*b-4*a* c; 4. Calculate the roots based on the nature of the discriminant of the quadratic equation. 5. 1£ discriminant>0,then Print the roots are real and distinct. 6. Slse if (discriminant = 0) then,Print both roots are real and equal. 7. Blse (discriminant < 0), the roots are distinct complex where, Print both roots are imaginary Program: #include #include int main() { double a, b, c, discriminant, root1, root2, realpart, imagPart; printf ("Enter coefficients a, b and c: "); scanf("$1f $1f %1f", &a, &b, &C); discriminant = b * b-4* a* c; if (discriminant > 0) { rootl = (-b + sgrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * ali prince (roots = $.21£ and root2 = %-21£", rooti, root2); else if (discriminant == 0) { : rootl = root2 = -b / (2 * a) printf ("root = root2 = %.21f; » F001) ; else: { Programming in C Lab Manual B realPart = -b / (2 * a)5 imagPart = sqrt (-discriminant) / (2 * a); print£("rootl = %.21f+%.21fi and root2 = %.2£-%.2£i", realPart, imagPart, realPart, imagPart); } = return 0; 9. Program to read marks scored by a student and find the sum, average and result using switch case. #include 4 int main() float m1,m2,m3,m4,m5; float total, average, perc; print ("Enter marks of five subjects: \n"); scant ("SESESESE%E", &m1, m2, &m3, &m4, &m5) ; total = ml+ m2+ m3 + m4 + m5; average = total / 5.0; perc = (total / 500.0) * 100; printf ("Total marks = %.2£\n", total); printf ("Average marks = %.2f\n", average); printf ("Percentage = %.2£", perc); switch (perc/10) { case 10 : case 9: case 8 : case 7: printé("\n Your Grade is: First class with Distinction") ; break; case 6 = print£("\n Your Grade is: first class" ); break; case 5: case 4: 2 e print£("\n Your Grade is: second class" ); break; 10. Programmingin C Lab Manual default : } xeturn 0; } print£("\n You Grade is: F or Fail\n"); Program to find the GCD of two numbers using function. #include int ged(int a, int d); int main() { int num1, num2; print£("Enter two numbers : "); scanf("*d 4a", 6num1, snum2) ; int result = gcd(numi, num2) ; print£("The GCD of %d and %d = %4", numi, num2, result); return 0; ) ~ int ged(int a, int b) { int hee; for(int 1-1; icea &&ic=b; i++) { if(atie=0 esbti=20) { hef = i; } } return hef; } Programming in C Lab Manual 1. Program to perform all bitwise operations on two integers PARTB #include int main() { unsigned char a= 5, b = 9; printi("a = %4, 1 b= %a\n", a, b); printf ("ab = % d\n", a & b); printf ("alb = %d\n", a | b); print£("a%b = %a\n", a * b); printf("-a = %d\n", a= -a); printf ("b<<1 = %d\n", b << 1); printf£("b>>1 = %d\n", b >> 1); return 0; } 2. Program to read a string and to find the number of alphabets, digits, vowels ;congonants spaces and special characters. #includecstdio-h> #includecstring.h> int main() { char str[150] ; int i,alphabets, vowels, consonants, digits, spaces, symbols; alphabets =vowels = consonants = digits = spaces = symbols = 0; elrser(); printf ("Enter the string"); gets (str) ; for ( str[i]!="\0"; i++) if((str[i]>=65 && str[i]<=90)|| (str[i]>=97 & str[i]<=122) ) { alphabets++; : @ str [i] =tolower(str[i]); else if(str[i]=='a’ || str[il=="e" || str[i, str[iJ==‘o’ || str{iJ==’u’) . ( vowels++; } else { ~ consonants++; } } else if(str[i] { digits+4; } else if (str[i { spaces++; } else { symbols++; } } printf ("Alphabets: $4”, alphabets) ; print£(*\nVowels: %4”, vowels) ; print£(*\nConsonants: %d”, consonants) ; print£(*\nDigits: %4",digits) ; print£("\nWhite spaces: $d”, spaces) ; printf("\nSymbols: %d”, symbols) ; return 0; } 3. Program to find the length and reverse the string without using built function ='0' && str[i]<="9!) a) #include #include #include void main() { char string[20],temp; int i,length; printf ("Enter String scanf ("%s", string) ; length=strlen (string) -1; for (i=0;icstrlen (string) /2;i++) { temp=string [i]; R string [i] =string [length] ; string [length--]=temp; z a printf ("\nReverse string :%s", string); getch() ; Programming in C Lab Manual a } 4. Program to read display and find the trace and norm of matrix of order M*N #tinclude #include void main () { ae static int array[10] [10]; int i, j, m, n, sum = 0, suml = 0, a = 0,.normal; printf ("Enter the order of the matrix\n") ; scanf("%d %d", &m, &n); printf ("Enter the n coefficients of the matrix \n"); for (i = 0; ic m; +44) { for (j ji #include #include ant main(){ + int array[500], count, i; int max, secondMax; printf ("Enter number of elements in array\n")i scanf("%d", &count); printé("Enter %4@ numbers \n", count) ; for(i = 0; ic count; it) { 4 scané("sa", &array(il); = } Programming in C Lab Manual /* initialize max and secondMax with INT_MIN */ max = secondMax = INT_MIN; for(i = 0; ic count; i++) { if (array[i] >max) { secondMax = max; max = array [i]; } else if (array[i] >secondmax && array{i] int main() { int n, m, c, d, aiff [10] [10]; printf("\nEnter the number of rows and columns of the first matrix \n\n"); scanf("%d%a", &m, &n); first [10] [10], second[10] [10], sum{10] [10], print£("\nEnter the %d elements of the first matrix \n\n", m*n); for(c = 0; c < m; c++) for(d = 0; d < nj; d++) scanf("sa", first [c] [d]); printf ("\nEnter the %d elements of the second matrix \n\n", m*n); for(c = 0; ¢ < m; c++) for(a = 0; d | ; void maltiply(int mat1{12] [12] ,int mat2(12] [12],int ,int ,int ); 15, void main() { int mati [12] [12] ,mat2 (12) (12) ; int i,j,k,m,n,p; printf ("Enter the number of rows and columns for lat matrix\n") ; scanf ("$d%a", m,n) ; printf ("Enter the elements of the 1st matrix\n"); for (i=0;i int isprime (int) ; main () : int n, result; printf ("Enter an integer to check whether it is prime or not.\n"); scanf ("%d", 6m) ; result = isprime(n); if ( result == 1) printf ("sd is prime.\n", n); else print£("%d is not prime.\n", n); return 0; } int isprime(int a) { int ¢; for (c=2; { if ( atc return 0; } return 1; a Programming in C Lab Manual g@ 9. program to demonstrate student structure to read and display the records of n students. ifinclude struct student char name [50] ; int roll; float marks; int main() struct student s; print£("Enter The Information of Students :\n\n"); printf ("Enter Name : "); scan£ (*%s",s.name) ; printf ("Enter Roll No. : "); acanf ("%d", &s.x011) ; print£ ("Enter marks : "); scanf ("%£", &s.marks) ; print£("\nDisplaying Information\n"); print£("Name: %s\n",s.name) ; print£("Roll: %a\n",s.rol1); print£("Marks: %.2£\n",s.maxks) ; return 0; } “10. program to demonstrate the concept of nested structure. #include struct student ( char name [30]; int rollNo; struct dateofsirth{ int dd; int mm; int yy; jon; ye ee main() struct student std; printf ("Enter name: "); scanf ("%s",std-name) ; printf("Enter roll number: "); scanf (*%a", &std.rol1No) ; g printf ("Enter Date of Birth [DD MM YY] format: " scanf ("¥d¥d¥d" , &std.DOB.dd, &std.DOB.mm, &std.DOB-yy) ; aa. Programming in C Lab Manual print£("\nName : %a \nRoL1No : %d \nDate of birth : 3024/%02d/%02d\n", std-name, std. rol1No, std.DOB.dd, std.DOB.mm, std.DOB. yy): xeturn 0; } Program to swap two, integers using call by value and call by reference. a. Call by value #include void swap(int , int); int main() { int a .b: printf ("Enter the value of a and b"); scanf ("$dtd", &a, &b) ; printf ("Before swapping in main a - %d, b = %d\n",a,b); swap (a,b); printé ("After swapping in main a = %d, b = %d\n",a,b); } void swap (int a, int b) { int temp; temp = a; a=b; b=temp; printé ("After swapping in function a = %d, b = %a\n",a,b); } b.call by reference void swap(int *, int *); int main() { int a ,b; printf ("Enter the value of a and b"); scanf ("%d%a", &&, 6D) 7 print£ ("Before swapping the values in main a = %4, b = %4\n",a,b); swap (&a, &b) ; printf ("After swapping values in main a = %d, b = %d\n",a,b); void swap (int *a, int *b) int temp; temp = *a; tas*b; *b=temp; bd); } Programming in C Lab Manual © print£("Agter swapping values in function a = %d, b = $d\n",*a,* 20

You might also like