0% found this document useful (0 votes)
64 views

12.1-Comparison of Two Numbers 12.2-Biggest of Three Numbers Are Not Dere in Dis Get It From Some One

The document contains code for various C programs including temperature conversion, swapping variables, roots of a quadratic equation, electricity bill preparation, reversing a number, checking for Armstrong numbers, a menu driven calculator, factorial and Fibonacci series using recursion, matrix addition and multiplication, sorting numbers, and calculating student marks using structures. The code examples demonstrate basic programming concepts like input/output, conditional statements, loops, functions, arrays and structures.

Uploaded by

Misbah Sultana
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

12.1-Comparison of Two Numbers 12.2-Biggest of Three Numbers Are Not Dere in Dis Get It From Some One

The document contains code for various C programs including temperature conversion, swapping variables, roots of a quadratic equation, electricity bill preparation, reversing a number, checking for Armstrong numbers, a menu driven calculator, factorial and Fibonacci series using recursion, matrix addition and multiplication, sorting numbers, and calculating student marks using structures. The code examples demonstrate basic programming concepts like input/output, conditional statements, loops, functions, arrays and structures.

Uploaded by

Misbah Sultana
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

EXERCISE 10.1-summation of two number 12.1-comparison of two numbers 12.

2-biggest of three numbers ARE NOT DERE IN DISGET IT FROM SOME ONE

LIST OF C-PROGRAMS

//10.2 Conversion of Celcius To Farenheit # include <stdio.h> # include <conio.h> void main() { float cel,fah; clrscr(); printf("\n\t\t***Temperature Conversion***\n\n"); printf("Enter the Celcius value : "); scanf("%f",&cel); fah=cel*1.8+32; printf("\nThe Converted Farenheit value : %f",fah); getch(); }

Output: ***Temperature Conversion*** Enter the Celcius value : 37 The Converted Farenheit value : 98.599998

// 11-Swapping Of Two Variables # include <stdio.h> # include <conio.h> void main() { int a,b; clrscr(); printf("\n\t***Swapping Of Two Variables***\n\n"); printf("\nEnter the value of a : "); scanf("%d",&a); printf("\nEnter the value of b : "); scanf("%d",&b); a=a+b; b=a-b; a=a-b; printf("\nAfter swapping a = %d and b = %d",a,b); getch(); }

Output:

***Swapping Of Two Variables***

Enter the value of a :20 Enter the value of b : 30 After swapping a = 30 and b = 20 // 13-Roots of a Quadratic Equation

# include <stdio.h> # include <conio.h> void main() { float a,b,c,d; float r1,r2; clrscr(); printf("\n\t***Roots of a Quadratic Equation***\n\n"); printf("Enter the values of a b & c :\n"); scanf("%f %f %f",&a,&b,&c); d=b*b-4*a*c; if (d==0) { r1 = r2 = (-b+sqrt(d)) / (2*a); printf("The roots are real and equal\n"); printf("r1 = r2 = %f",r1); } else if (d>0) { r1 = (-b+sqrt(d)) / (2*a); r2 = (-b-sqrt(d)) / (2*a); printf("The roots are real and unequal\n"); printf("r1 = %f\n",r1); printf("r2 = %f",r2); } else { r1 = -b / (2*a); r2 = sqrt(-d) / (2*a); printf("The roots are imaginary\n"); printf("r1 = %f + i%f\n",r1,r2); printf("r2 = %f - i%f\n",r1,r2); } getch(); } Output:

***Roots of a Quadratic Equation*** Enter the values of a b & c : 2 4 2 The roots are real and equal r1 = r2 = -1.000000

***Roots of a Quadratic Equation*** Enter the values of a b & c : 3 6 2 The roots are real and unequal r1 = -5449.000000 r2 = 5447.000000 ***Roots of a Quadratic Equation*** Enter the values of a b & c : 3 2 4 The roots are imaginary r1 = -0.333333 + i-5438.000000 r2 = -0.333333 - i-5438.000000

//14-Electricity Bill Preparation #include<stdio.h> #include<conio.h> void main() { char name[10]; int units,uc,cid; float charges; clrscr(); printf("\n\t*******ELECTRICITY BILL PREPARATION******\n\n"); printf("Enter the cid : "); scanf("%d",&cid); printf("Enter the cname : "); scanf("%s",name); printf("Enter the unit consumed : "); scanf("%d",&uc); if(uc<=200) charges=.50*uc; else if((uc>200)&&(uc<=400)) charges=100+.65*(uc-200); else if((uc>400)&&(uc<=600)) charges=230+.80*(uc-400); else if(uc>600) charges=390+1*(uc-600); printf("\nCUSTID CNAME UNITCONSUMED AMOUNT"); printf("\n%d\t %s\t %d\t %f",cid,name,uc,charges); getch(); }

Output: *******ELECTRICITY BILL PREPARATION******

Enter the cid : 101 Enter the cname : preetha Enter the unit consumed : 367 CUSTID 101 CNAME UNITCONSUMED preetha 367 AMOUNT 208.550003

//15-Reversing The Given Number # include <stdio.h> # include <conio.h> void main() { int no,r; clrscr(); printf("\n\t***Reversing the given number***\n\n"); printf("Enter the number : "); scanf("%d",&no); printf("\nThe Reversed number is : "); while (no>0) { r=no%10; printf("%d",r); no=no/10; } getch(); } Output:

***Reversing the given number*** Enter the number : 378 The Reversed number is : 873

//16- Checking for Armstrong Number # include <stdio.h> # include <conio.h> void main() { int a,no,r,sum=0; clrscr(); printf("\n\t***Checking for Armstrong Number***\n\n"); printf("Enter the number : "); scanf("%d",&no); a=no; do { r=no%10; sum=sum+r*r*r; no=no/10; } while (no>0); if (a==sum) printf("\n%d is an Armstrong number",a); else printf("\n%d is not an Armstrong number",a) getch(); }

Output:

***Checking for Armstrong Number*** Enter the number : 153 153 is an Armstrong number

***Checking for Not Armstrong Number*** Enter the number : 325 325 is not an Armstrong number

//17-menu driven calculator #include<stdio.h> #include<conio.h> #include<process.h> int add(int n1,int n2); int sub(int n1,int n2); int mul(int n1,int n2); int div(int n1,int n2); void display(int res); void main() { int num1,num2,choice,r; char c; clrscr(); l1: do { printf("Calclator\n"); printf("select your choice\n"); printf("1-addition\n"); printf("2-subtraction\n"); printf("3-multiplication\n"); printf("4-division\n"); printf("5-exit\n"); scanf("%d",&choice); if (choice<=4) { printf("enter two numbers\n"); scanf("%d%d",&num1,&num2); } switch(choice) { case 1: { r=add(num1,num2); display(r); break; }

case 2: { r=sub(num1,num2); display(r); break; } case 3: { r=mul(num1,num2); display(r); break; } case 4: { r=div(num1,num2); display(r); break; } case 5:exit(1); default: { printf("wrong choice\n"); goto l1; } } printf("you want to continue y/n \n"); scanf("%c",&c); } while((c=='y')||(c=='Y')); } int add(int n1,int n2) { int s; s=n1+n2; return s; }

int sub(int n1,int n2) { int s; s=n1-n2; return s; } int mul(int n1,int n2) { int m; m=n1*n2; return m; } int div(int n1,int n2) { int d; d=n1/n2; return d; } void display(int res) { printf("result=%d\n",res); } OUTPUT: Calclator select your choice 1-addition 2-subtraction 3-multiplication 4-division 5-exit 1 enter two numbers 4 5 result=9 you want to continue y/n n

//18.1- Factorial-Recursion # include <stdio.h> # include <conio.h> void main() { int n; long int f; long int fact(); clrscr(); printf("\n\t***Factorial Of A Given Number***\n\n"); printf("Enter the number : "); scanf("%d",&n); f=fact(n); printf("\nFactorial is : %ld",f); getch(); } long int fact(int n) { if (n==0) return (1); else return (n*fact(n-1)); }

Output:

***Factorial Of A Given Number*** Enter the number : 6 Factorial is : 720

//18.2Fibonacci Series - Recursion # include <stdio.h> # include <conio.h> void main() { int n, f = -1, s =1; void fibo(); clrscr(); printf("\n\n\t***Fibonacci Series****\n\n"); printf("Enter the no. of Terms : "); scanf("%d", &n); printf("\nFibonacci Series is :\n\n"); fibo(n, f, s); getch(); } void fibo(int n,int f, int s) { int no; if (n > 0) { no=f+s; printf("%d ", no); fibo(n-1, s, no); } }

Output: ***Fibonacci Series**** Enter the no. of Terms : 8 Fibonacci Series is : 0 1 1 2 3 5 8 13

//19.1matrix addition #include<stdio.h> #include<conio.h> void main() { int m1[10][10],m2[10][10],res[10][10]; int r1,c1,r2,c2,i,j; clrscr(); l1: printf("enter order of matrix 1\n"); scanf("%d%d",&r1,&c1); printf("enter order of matrix 2\n"); scanf("%d%d",&r2,&c2); if((r1==r2)&&(c1==c2)) goto m; else { printf("enter order again\n"); goto l1; } /*matrix input*/ m: { printf("enter matrix 1 elements\n"); for(i=0;i<r1;i++) for(j=0;j<c1;j++) scanf("%d",&m1[i][j]); printf("enter matrix 2 elements\n"); for(i=0;i<r2;i++) for(j=0;j<c2;j++) scanf("%d",&m2[i][j]); /*matrix addition*/ for(i=0;i<r1;i++) for(j=0;j<c1;j++) res[i][j]=m1[i][j]+m2[i][j];

/*resultant matrix*/ printf("resultant matrix\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { printf("%4d",res[i][j]); } printf("\n"); } } getch(); } OUTPUT: enter order of matrix 1 3 3 enter order of matrix 2 3 3 enter matrix 1 elements 2 2 2 2 2 2 2 2 2 enter matrix 2 elements 3 3 3 3 3 3 3 3 3 resultant matrix 5 5 5 5 5 5 5 5 5

//19.2-matrix multiplication #include<stdio.h> #include<conio.h> void main() { int m1[10][10],m2[10][10],res[10][10]; int r1,c1,r2,c2,i,j,k; clrscr(); l1: printf("enter order of matrix 1\n"); scanf("%d%d",&r1,&c1); printf("enter order of matrix 2\n"); scanf("%d%d",&r2,&c2); if(c1==r2) { printf("multiplication possible\n"); goto m; } else { printf("enter order again\n"); goto l1; } /*matrix input*/ m: { printf("enter matrix 1 elements\n"); for(i=0;i<r1;i++) for(j=0;j<c1;j++) scanf("%d",&m1[i][j]); printf("enter matrix 2 elements\n"); for(i=0;i<r2;i++) for(j=0;j<c2;j++) scanf("%d",&m2[i][j]);

/*matrix multiplication*/ for(i=0;i<r1;i++) for(j=0;j<c2;j++) { res[i][j]=0; for(k=0;k<r2;k++) { res[i][j]=res[i][j]+m1[i][k]*m2[k][j]; } } /*resultant matrix*/ printf("resultant matrix\n"); for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { printf("%4d",res[i][j]); } printf("\n"); } } getch(); } OUTPUT: enter order of matrix 1 2 1 enter order of matrix 2 1 2 multiplication possible enter matrix 1 elements 2 4 enter matrix 2 elements 5 5 resultant matrix 10 10 20 20

//20-sorting the given numbers

#include<stdio.h> #include<conio.h> void main() { int marks[25],i , j, temp , n; clrscr(); printf("enter no.of elements\n"); scanf("%d",&n); for( i=0;i<n;i++) { printf("enter mark %d\n",i+1); scanf("%d",&marks[i]); } /*sorting process*/ for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(marks[i]>marks[j]) { temp=marks[i]; marks[i]=marks[j]; marks[j]=temp; } } } /*printing sorted elements of marks array*/ printf("marks sorted in ascending order\n"); for(i=0;i<n;i++) printf("%d\n",marks[i]); getch(); }

OUTPUT: Enter no.of elements 5 Enter mark 1 80 Enter mark 2 90 Enter mark 3 60 Enter mark 4 70 Enter mark 5 90 Marks sorted in ascending order 60 70 80 90 90

//21-Students Mark Calculation Using Structure # include <stdio.h> # include <conio.h> void main() { struct student { int Rollno; char name[15]; int m1,m2,m3,total; float avg; char grade; }; struct student s[10]; int i,n; clrscr(); printf("\n\t\t***Students Mark Calculation***\n\n"); printf("Enter the no. of students : "); scanf("%d",&n); for (i=0;i<n;i++) { printf("\nEnter the details of student %d",i+1); printf("\n\nRollNo : "); scanf("%d",&s[i].Rollno); printf("\nName : "); scanf("%s",s[i].name); printf("\nM1, M2, M3 : "); scanf("%d %d %d",&s[i].m1,&s[i].m2,&s[i].m3); s[i].total=s[i].m1+s[i].m2+s[i].m3; s[i].avg=s[i].total/3.0; if (s[i].avg>=75) s[i].grade='A'; else If ((s[i].avg>=60)&&(s[i].avg<75)) s[i].grade='B'; else if ((s[i].avg>=50)&&(s[i].avg<60)) s[i].grade='C';

else s[i].grade='F'; } printf("\n\nRollNo\tName\tM1\tM2\tM3\tTotal\tAverage\tGrade\n"); printf("_____________________________________________________________\n") for (i=0;i<n;i++) { printf("\n%d\t",s[i].Rollno); printf("%s",s[i].name); printf("\t%d\t%d\t%d",s[i].m1,s[i].m2,s[i].m3); printf("\t%d\t%5.2f\t%c",s[i].total,s[i].avg,s[i].grade); } getch(); }

Output: ***Students Mark Calculation*** Enter the no. of students : 3 Enter the details of student 1 RollNo : 1009 Name : ABC M1, M2, M3 : 78 90 67 Enter the details of student 2 RollNo : 1142 Name : DEF M1, M2, M3 : 67 59 87 Enter the details of student 3 RollNo : 1256 Name : GHI M1, M2, M3 : 37 40 45 RollNo Name M1 M2 M3 Total Average Grade ____________________________________________________ 1009 ABC 78 90 67 235 78.33 A 1142 DEF 67 59 87 213 71.00 B 1256 GHI 37 40 45 122 40.67 F

//22- Program to calculate the sine series # include <stdio.h> # include <conio.h> # include <math.h> void main() { int i, n ; float x, val, sum, t ; clrscr() ; printf("Enter the value for x : ") ; scanf("%f", &x) ; printf("\nEnter the value for n : ") ; scanf("%d", &n) ; val = x ; x = x * 3.14159 / 180 ; t=x; sum = x ; for(i = 1 ; i < n + 1 ; i++) { t = (t * pow((double) (-1), (double) (2 * i - 1)) * x * x) / (2 * i * (2 * i + 1)) ; sum = sum + t ; } printf("\nSine value of %f is : %8.4f", val, sum) ; getch() ; } RUN 1 : ~~~~~~~ Enter the value for x : 30 Enter the value for n : 20 Sine value of 30.000000 is : 0.5000

You might also like