0% found this document useful (0 votes)
18 views16 pages

C-Lab-manual

Lab manual For PST

Uploaded by

Haseeb Pasha
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)
18 views16 pages

C-Lab-manual

Lab manual For PST

Uploaded by

Haseeb Pasha
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/ 16
Bangalore University CA-C4P: Problem Solving Lab using C 1. program to read radius ofa circle and to find area and circumference #include #include Hdefine PI 3.142 void main() Peet ens { uke firea of a circle = 78.55 double area,circum; clrser(): printf("\n Enter radius of a circle :") seant("%d",&r); area=PI*r*1; *PItr, \n Area ofa circle =%0.21,area); printf("\n Circumference ofa circle = %0.2If" circum); getch(): 2. Program to read three numbers and find biggest of three # include # include # define MAX(a,b) (a>b ? a: b) en eee ee void main() a Oe int a.b.c,.L1,L2: clrscr() printf("\n Enter three numbers(a,b,c) "):scanf("%od %d %d" Sa, &b,&c); LI=MAX(ab); L2-MAX(.LI); printi(’\n Biggest number = %d",L2): getchQ; 3. Program to check whether the number is prime or not #include #include void main() re) { ere int num,count=0.1; clrser(); printf("\n Enter a number : ") scanf("%ed" &num), for(i=1:i<-num.i++) if(aum%i-=0) count++; printf("\n Yd is a Prime number" mum): else printf("\n %d is not a Prime number” num); getch(); } 4. C program to read a number, find the sum of the digits reverse the number and check it for palindrome # include if include void main() { Se eee Ce int num.orgnum: int sum=0,rev clrscr(); printi("\n Enter a number scanf("%d",&num): orgnum=num; while(num>0) t Tee) rem: cea rem=num%10; su um+rem,; eee hoe rev=rev* 10+rem: a a "| pe J num=num/10; reece Tre } printi("\n Sum of digits = %d",sum); printi(’\n Reversed number = %d" rev); if(orgnum==rev) printf("\n Number is a Palindrome"); else printf("\n Number is not a Palindrome"); geich(): J 5. Program to read numbers from keyboard continuously till the user presses 999 and find the sum of only positive numbers # include # inchude ater a mimber void main() one int num,sum=0; Tec , ener ene ts do “Tr { wearers printi("\n Enter a number : "); eer) scant("%d" &enum): Se ara) if(num > 0 && num!=999) eC) sum er) um + num; printi("\n Sum = %d" sum); }while(num!=999); printf("\n You have pressed 999 : STOP"); getch(); ; } 6. Program to read percentage of marks and to display appropriate message Demonstration of (else-if) ladder Percentage Grade > 90% Exemplary 180% -90% Outstanding 170% -79% First Division with Distinction 11 60% -69% First Division 150% -59% Second Division 138% -49% Pass class 1<35% Fails : Re-appear # include # include void main0) £ float per; clrser(); printf(’\n Enter Percentage : "); scanf("%f" per); ¥ if(per > 90.0) 7 ra pninti("\n Grade = EXEMPLARY"); else if(per >=80.0 && per < 90.0) printi("\n Grade = OUTSTANDING") else iff(per>=70.0 && per < 80.0) printi("\n Grade = FIRST DIVISION WITH DISTINCTION"); else if(per >=60.0 && per <70.0) printi("\n Grade = FIRST DIVISION"); else if(per >=50.0 && per < 60.0) printf(’\n Grade = SECOND DIVISION") else iffper >=35.0 && per < 50.0) printi("\n Grade = PASS CLASS"); else ptinti("\n Grade = FAILS : RE-APPEAR"); getchQ; 7. Program to find roots of quadratic equation using Switch-Case # include # include # include and Distinct eT rer void main() { inta,b,c,choice; double disc.root] root2.real,img: clrser(): printf("\n Enter a,b,c :") seanf("%d %d %od" fea, Seb, Bec): disc=(b*b) - (4.0 a*c); if(dise > 0) choice=1; else if(disc < 0) choice=2: else choice=3; switch(choice) { case 1 printf("\n Real and Distinct Roots"); rootl=(-b + sqri(disc) ) / (2.0 * a): root2= (-b - sqrt(disc) ) / (2.0 * a); printf("\n Root] = %0.21f" root]): printf("\n Root2 = %0.2If" root2); } break: case 2 { printf("\n Roots are complex and imaginary"); real = -b/(2.0 * a); img = sqrt(abs(disc))/(2.0 * a), printf("n Root] = %0.21f +i %0.21f",real,img); printf("\n Root2 = %0.21f -i %0.21f"real.img); } break; case 3 £ ” print{("\n Roots are Equal"); rootl = -b/(2.0 * a); root2 = -b/(2.0 * a) printi("\n Root = printi("\n Root2 } break; Y00.21f" root); Y00.21f" ro0t2); default : printf{"\n Invalid inputs"); } geich: } 8. Program to read marks scored by n students and find the average of marks // Demonstration of single dimension array # include # include void main() { ee ee ee ae t int ni, Da float marks[10], tot_marks=0.0,avg_marks=0.0;elrser(); printf("\n Enter students count(n) : "); scanf("%d" &n); nter mark sco 5 student. + 56 for(i=O;i # include Creer) ima t_ main() CCE: inta[20].n,ij,ele; aes Ex cliscx(); printf("\n Enter array limit(n) : "); ae Yo" Sen), aa Ef for(i=0;i # include void main() { int a[ 10][10], b[10][ 10], e[ 10][ 10], d[10][10]; intij.n; clrscr(); printf("\n Enter order of square matrix(n) : "); seanf("%d" Sn); printf("\n Enter A matrix elements\n"); ue for(i=0:i # include void main() { Factorial of 4 = 24 int num.i long int fact=1; chsex(); printf("\n Enter a number : "); seanf("%d",&num); forG=1;i<-num:i++) { fact=fact*i: } printf{("\n Factorial of %d = %ld" num, fact); getch(); 3 ee ae! 12. Program to generate fibonacci series # include # include . ey void:main() ee { rea int ni int F=0,S=1,NXT; clrscr(); printf("\n Enter series limit(n) : ") scanf("%d" én), printi("\n ******+* FIBONACCI SERIES *******\n"); printf("%d \t %d"F,S); for(i=3:i<=n;i++) { NXT=FH printi("\t F=S; S=NXT,; } } getch() %d". NXT); 14. program to find length of string without using built-in function # include # include fa-string + UNIVERSITY void main() @ STRIOI { esiee RUZ] int len=0,i-0; ma char *str; rien clrser(); printf("\ Enter a string : "); scanf("%s" str) UIC while(sty{i]!=\0') t Ien+4; im; } printf("\n Length of string = %d" Jen); getch(; } 15. Program to demonstrate string functions #include # include # include void main() char *strl="Bangalore"; char *str2="University"; clrser(): printf("\n String 1 = %s String 2 = %s\n" str] tr2); printf("\n 1. Length of %s = %d'"strl strlen(strl)): printf("\n 2. String copy in str2 strepy(str2,"CITY")); priatf("\n 3. Concatenation = %s", streat(strl str2)); printf("\n 4. Compare strl & str? %d",stremp(str1,"bangalore")); printf("\n 5. String in lower case = %s",strhvr(str1)); printf("\n 6. String in upper case = %s",strupr(strl)): printf("\n 7. Substring search = %s" strchr(strl,'L’)); priatf("\n 8. Duplicate string = %s" strdup(str1)): printf("\n 9. String reverse = %s" strrev(str1)); printf("\n 10. Set all character to #= %s",strset(str! String 1 = Bangalore Pa Tecra tet) ee ee O Ee ae String copy in st eects Ces Cu formers ss sir Cato) era ancl Ty ubstr Nereis) een art eT 17. to read a string and to find the number of alphabets, digits, vowels, consonants, spaces and special characters. /C program to read a string and find the number of alphabets, digits, // vowels, consonants, spaces and special characters # include # include # include void main() t char str[100],ch; int acount=0, deount=0, vcount=0, ccount=0, scount=0,speount=0, clrser(); printf("\n Enter a siring gets(str); while(str[il { , if(isalphatst[i))) Senne ree { Oe aed acount'-+; a near ch=tolower(str[i]); ees ewilsbteh) Samra ree case 'a’ case 'e" case’: case ‘0’ case 'u': vcount++; break; default : ccount++; } } else iffisdigit(str[i])) deount++ else iffisspace(str[i])) seount++; else speount++: itl; } printf("in No printi(\n No. printf("\n No. printf("in No printf("\n No. printi("\n No. getchQ; of Alphabets = %d" acount); of Vowels = %d" count); of Consonants = %d" count); of Spaces = %d",scount); of Digits = %d" count); of Special symbols = %d",spcount); 18. to Swap Two Numbers using Pointers //C program to swap two numbers using pointers # include # include Se ee void mainQ) eee { int n1n2.4ptrl ¥ptr2, temp: clrser(): printf(’\n Enter two numbers : "); seanf("%d %d" Senl, &en2); Coe a printf("\n Before swapping nl =%d_n2= %d\n" nln *ptr2=temp; printi(’\n After swapping nl getch() %d n2=%d\n",n1n2); 19. Program to demonstrate student structure to read & display records of n students // To demonstrate student structure to read & display records of n students # include # include stract STUDENT { int regno: char name[50]; char grade: }BCAI50]: A ero ee Ce Te) void mainQ { ee i ae Sa a 1a) RAMESH printf(’\n Enter student count(n) : printi("\n Enter %d student(regno.name,grade) : "it 1); scanf("%ed %s %e", &BCALi].regno, BCA[i].name, &BCA[i].grade); } } printf(’\n REGNO \t STUDENT NAME \t GRADE") for(i-0;i # include struct EMP! { intempid; —_//2 bytes char name[50]; _ // 50 bytes float salary; //4bytes (Total A union EMP2 { t intempid; —_// 2 bytes 50 + 4=56) char name[50];_ // 50 bytes float salary; // 4 bytes (highest = 50 bytes) 3 void main() { elrser(); printi("\n Size of Structure EMP] = %d" sizeof{ struct EMP1)); printf("\n Size of Union EMP2 = %d" sizeof(union EMP2)) getch; } Size of Struct Size of Union

You might also like