Mathematics(IX, X, XI, XII) CBSE & UP Board & CSE(C programming, Python, JAVA, Data Structure, IoT, IT, CBNST etc.)
Saturday, December 22, 2018
Friday, December 21, 2018
Types of User-defined Functions in c programming.
1) Function with no arguments and no return value
Example:
#include <stdio.h>
void main()
{
sum();
}
void sum() //no arguments and no return value
{
int a,b,c;
printf("Enter two numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("Sum of both numbers is %d",c);
}
Enter two numbers 4
5
Sum of both numbers is 9
2) Function with no arguments and a return value
#include <stdio.h>
void main()
{
int c= sum();
printf("Sum of both numbers is %d",c);
}
int sum() //no arguments and a return value
{
int a,b,c;
printf("Enter two numbers");
scanf("%d%d",&a,&b);
c=a+b;
return c;
}
Enter two numbers 5
7
Sum of both numbers is 12
3)Function with arguments and no return value
#include <stdio.h>
void main()
{
int a,b;
printf("Enter two numbers");
scanf("%d%d",&a,&b);
sum(a,b);
}
void sum(int a, int b) //arguments and no return value
{
int c;
c=a+b;
printf("Sum of both numbers is %d",c);
}
Enter two numbers 6
5
Sum of both numbers is 11
4) Function with arguments and a return value
(This is most appropriate function type)
#include <stdio.h>
void main()
{
int a,b,c;
printf("Enter two numbers");
scanf("%d%d",&a,&b);
c=sum(a,b);
printf("Sum of both numbers is %d",c);
}
int sum(int a, int b) //arguments and a return value
{
return a+b;
}
Enter two numbers 5
5
Sum of both numbers is 10
What are the functions in C programming?
A function is a module or block of program code which deals with a particular task. Making functions is a way of isolating one block of code from other independent blocks of code.
How function work in c:
Advantages of function:
1. The program will be easier to
understand, maintain and debug.
2. Reusable codes that can be used
in other programs
3. A large program can be divided
into smaller modules. Hence, a large project can be divided among many
programmers.
Function signatures are the
"declaration" of the functions in a program. Declaration of a
function instructs a compiler on how to call a function. Function declarations
comprise of the following:
1. Name of the function
2. Return type : type of the value
that will be returned to the program when function is executed
3. Parameter(s) : the type of values
that you pass to the function while calling it
Passing Parameters to a
Function:
There are
two ways to pass parameters to a function:
- Pass by Value: mechanism is used when
you don't want to change the value of passed parameters. When parameters
are passed by value then functions in C create copies of the passed in
variables and do required processing on these copied variables.
- Pass by Reference mechanism is used when
you want a function to do the changes in passed parameters and reflect
those changes back to the calling function. In this case only addresses of
the variables are passed to a function so that function can work directly
over the addresses.
- Function with no arguments and no return value
- Function with no arguments
and a return value
- Function with arguments and
no return value
- Function with arguments and
a return value
Thursday, December 20, 2018
C program to read three values from keyboard and print out the largest & smallest of them without using if statement.(using ternary operator)
#include<stdio.h>
void main()
{
int x,y,z;
printf("Enter Three Numbers:--\n");
scanf("%d %d %d",&x,&y,&z);
((x>y)&&(x>z))?printf("Largest is x :-- %d",x):((y>x)&&(y>z))?printf("Largest is y :--%d",y):printf("Largest is z :-- %d",z);
((x<y)&&(x<z))?printf("Smallest is x :-- %d\n",x):((y<x)&&(y<z))?printf("Smallest is y :--%d\n",y):printf("Smallest is z :-- %d\n",z);
}
void main()
{
int x,y,z;
printf("Enter Three Numbers:--\n");
scanf("%d %d %d",&x,&y,&z);
((x>y)&&(x>z))?printf("Largest is x :-- %d",x):((y>x)&&(y>z))?printf("Largest is y :--%d",y):printf("Largest is z :-- %d",z);
((x<y)&&(x<z))?printf("Smallest is x :-- %d\n",x):((y<x)&&(y<z))?printf("Smallest is y :--%d\n",y):printf("Smallest is z :-- %d\n",z);
}
Enter Three Numbers:--
12
5
40
Largest is z :-- 40 Smallest is y :--5
C program to read a four digit integer and print the sum of its digits.
#include<stdio.h>
void main()
{
int Num,Sum=0,Digit;
printf("Enter a Four Digits Number\n",&Num);
scanf("%d",&Num);
Digit=Num%10;
Num=Num/10;
Sum+=Digit;
Digit=Num%10;
Num=Num/10;
Sum+=Digit;
Digit=Num%10;
Num=Num/10;
Sum+=Digit;
Digit=Num%10;
Num=Num/10;
Sum+=Digit;
printf("\nSum of Digits are :-- %d\n",Sum);
}
void main()
{
int Num,Sum=0,Digit;
printf("Enter a Four Digits Number\n",&Num);
scanf("%d",&Num);
Digit=Num%10;
Num=Num/10;
Sum+=Digit;
Digit=Num%10;
Num=Num/10;
Sum+=Digit;
Digit=Num%10;
Num=Num/10;
Sum+=Digit;
Digit=Num%10;
Num=Num/10;
Sum+=Digit;
printf("\nSum of Digits are :-- %d\n",Sum);
}
Enter a Four Digits Number
1245
Sum of Digits are :-- 12
C program that reads a floating-point number and then displays the right –most digit of the integral part & left-most digit of fractional part of the number.
#include<stdio.h>
void main()
{
float x,y;
int z,w;
printf("Enter floating point number : x= ");
scanf("%f",&x);
z=(int)x%10;
y=x-(int)x;
w=(int)(y*10);
printf(" \nThe Right-most digit of the integral part of the number %.2f is %d",x,z);
printf(" \nThe Left-most digit of the fractional part of the number %.2f is %d",x,w);
}
void main()
{
float x,y;
int z,w;
printf("Enter floating point number : x= ");
scanf("%f",&x);
z=(int)x%10;
y=x-(int)x;
w=(int)(y*10);
printf(" \nThe Right-most digit of the integral part of the number %.2f is %d",x,z);
printf(" \nThe Left-most digit of the fractional part of the number %.2f is %d",x,w);
}
Enter floating point number : x= 123.75
The Right-most digit of the integral part of the number 123.75 is 3
The Left-most digit of the fractional part of the number 123.75 is 7
C program to compute distance between two points (x1,y1) and (x2,y2).
Distance between two points (x1,y1) and (x2,y2) is governed by the formula
#include<stdio.h>
#include<math.h>
void main()
{
int x1,x2,y1,y2;
float D;
D=0;
x1=20;
x2=30;
y1=40;
y2=50;
D=sqrt((x2-x1)*(x2-x1)+ (y2-y1)* (y2-y1));
printf("Distance between to points is= %f",D);
}
Distance between to points is= 14.142136
D2 = (x2 -x1 )2
+(y2 -y1)2
#include<stdio.h>
#include<math.h>
void main()
{
int x1,x2,y1,y2;
float D;
D=0;
x1=20;
x2=30;
y1=40;
y2=50;
D=sqrt((x2-x1)*(x2-x1)+ (y2-y1)* (y2-y1));
printf("Distance between to points is= %f",D);
}
Distance between to points is= 14.142136
C program to compute the area of the triangle using heron's formula.
Heron's formula
A=sqrt(S(S-a)(S-b)(S-c))
Where a, b and c are sides of the triangle and S=(a+b+c)/2.
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c;
float S,Area;
a=b=c=S=0;
a=20;
b=30;
c=40;
S=(a+b+c)/2;
Area=sqrt(S*(S-a)*(S-b)*(S-c));
printf("Area of a triangle is= %f",Area);
}
Area of a triangle is= 290.473755
A=sqrt(S(S-a)(S-b)(S-c))
Where a, b and c are sides of the triangle and S=(a+b+c)/2.
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c;
float S,Area;
a=b=c=S=0;
a=20;
b=30;
c=40;
S=(a+b+c)/2;
Area=sqrt(S*(S-a)*(S-b)*(S-c));
printf("Area of a triangle is= %f",Area);
}
Area of a triangle is= 290.473755
C program to convert the temperature (a) from Celsius to Fahrenheit and (b) from Fahrenheit to Celsius.
Relationship between Celsius and Fahrenheit is governed by the formula
F = (9C/5)+32
#include<stdio.h>
void main()
{
float F,C;
C=200;
F=(((9*C)/5)+32);
printf("Celsius = %f to Fahrenheit = %f\n",C,F);
F=300;
C=((F-32)*5)/9;
printf("Fahrenheit = %f to Celsius = %f\n",F,C);
}
F = (9C/5)+32
#include<stdio.h>
void main()
{
float F,C;
C=200;
F=(((9*C)/5)+32);
printf("Celsius = %f to Fahrenheit = %f\n",C,F);
F=300;
C=((F-32)*5)/9;
printf("Fahrenheit = %f to Celsius = %f\n",F,C);
}
Celsius = 200.000000 to Fahrenheit = 392.000000
Fahrenheit = 300.000000 to Celsius = 148.888885
C program to compute the area of circle. Use a symbolic constant to define the π value.
#include<stdio.h>
#define PIE 3.14
void main()
{
float Rad,Area;
Rad=4;
Area=PIE*Rad*Rad;
printf("Area of a circle is--> %f",Area);
}
#define PIE 3.14
void main()
{
float Rad,Area;
Rad=4;
Area=PIE*Rad*Rad;
printf("Area of a circle is--> %f",Area);
}
Area of a circle is--> 50.240002
C program to convert octal number to decimal number.
Example : (125)8 =(?)10
Solution:
C Program:
#include<stdio.h>
#include<math.h>
void main()
{ int n,r,s=0,p=0;
printf("\nEnter an octal no.");
scanf("%d",&n);
while(n>0)
{
r=n%10;
if(r<0 || r>7)
{
printf("\nInput no is not octal");
exit(0);
}
s=s+r*pow(8,p);
p++;
n=n/10;
}
printf("\nDecimal no. equivalent to input octal no.is:=%d",s);
}
Output:
Solution:
=125
=1X82 +2X81 + 5X80
=64 + 16 + 5
=85C Program:
#include<stdio.h>
#include<math.h>
void main()
{ int n,r,s=0,p=0;
printf("\nEnter an octal no.");
scanf("%d",&n);
while(n>0)
{
r=n%10;
if(r<0 || r>7)
{
printf("\nInput no is not octal");
exit(0);
}
s=s+r*pow(8,p);
p++;
n=n/10;
}
printf("\nDecimal no. equivalent to input octal no.is:=%d",s);
}
Output:
Enter an octal no.125
Decimal no. equivalent to input octal no.is:=85
C program to convert Decimal no. to Hexadecimal no.
Example : (3509)10 =(?)16
Solution:
C Program:
#include<stdio.h>
void main()
{ int n,i=0,r;
char hex[30];
printf("\nEnter a Decimal no.");
scanf("%d",&n);
while(n>0)
{ r=n%16;
switch(r)
{ case 0: hex[i]='0'; break;
case 1: hex[i]='1'; break;
case 2: hex[i]='2'; break;
case 3: hex[i]='3'; break;
case 4: hex[i]='4'; break;
case 5: hex[i]='5'; break;
case 6: hex[i]='6'; break;
case 7: hex[i]='7'; break;
case 8: hex[i]='8'; break;
case 9: hex[i]='9'; break;
case 10: hex[i]='A'; break;
case 11: hex[i]='B'; break;
case 12: hex[i]='C'; break;
case 13: hex[i]='D'; break;
case 14: hex[i]='E'; break;
case 15: hex[i]='F'; break;
}
n=n/16;
i++;
}
printf("\nEquivalent Hexadecimal no. is: ");
for(--i;i>=0;i--)
printf("%c",hex[i]);
}
Output:
Solution:
C Program:
#include<stdio.h>
void main()
{ int n,i=0,r;
char hex[30];
printf("\nEnter a Decimal no.");
scanf("%d",&n);
while(n>0)
{ r=n%16;
switch(r)
{ case 0: hex[i]='0'; break;
case 1: hex[i]='1'; break;
case 2: hex[i]='2'; break;
case 3: hex[i]='3'; break;
case 4: hex[i]='4'; break;
case 5: hex[i]='5'; break;
case 6: hex[i]='6'; break;
case 7: hex[i]='7'; break;
case 8: hex[i]='8'; break;
case 9: hex[i]='9'; break;
case 10: hex[i]='A'; break;
case 11: hex[i]='B'; break;
case 12: hex[i]='C'; break;
case 13: hex[i]='D'; break;
case 14: hex[i]='E'; break;
case 15: hex[i]='F'; break;
}
n=n/16;
i++;
}
printf("\nEquivalent Hexadecimal no. is: ");
for(--i;i>=0;i--)
printf("%c",hex[i]);
}
Output:
Enter a Decimal no.3509
Equivalent Hexadecimal no. is: DB5
C program to convert Decimal no. to Octal no.
Example : (125)10 =(?)8
Solution:
C Program:
#include<stdio.h>
void main()
{ int n,i=0,oct[20];
printf("\nEnter a Decimal no.");
scanf("%d",&n);
while(n>0)
{ oct[i]=n%8;
n=n/8;
i++;
}
printf("\nEquivalent Octal no. is: ");
for(--i;i>=0;i--)
printf("%d",oct[i]);
}
Output:
Solution:
C Program:
#include<stdio.h>
void main()
{ int n,i=0,oct[20];
printf("\nEnter a Decimal no.");
scanf("%d",&n);
while(n>0)
{ oct[i]=n%8;
n=n/8;
i++;
}
printf("\nEquivalent Octal no. is: ");
for(--i;i>=0;i--)
printf("%d",oct[i]);
}
Output:
Enter a Decimal no. 125
Equivalent Octal no. is: 175
C program to convert Decimal no. to Binary no.
Example : (125)10 =(?)2
Solution:
Write in reverse order to get binary no:=(1111101)2
C Program:
#include<stdio.h>
void main()
{
int n,i=0,bin[20];
printf("\nEnter a Decimal no.");
scanf("%d",&n);
while(n>0)
{ bin[i]=n%2;
n=n/2;
i++;
}
printf("\nEquivalent Binary no is:");
for(--i;i>=0;i--)
printf("%d",bin[i]);
}
Solution:
Write in reverse order to get binary no:=(1111101)2
C Program:
#include<stdio.h>
void main()
{
int n,i=0,bin[20];
printf("\nEnter a Decimal no.");
scanf("%d",&n);
while(n>0)
{ bin[i]=n%2;
n=n/2;
i++;
}
printf("\nEquivalent Binary no is:");
for(--i;i>=0;i--)
printf("%d",bin[i]);
}
Enter a Decimal no.125
Equivalent Binary no is:1111101
Course Contents (AKTU-formerly UPTU)
Course Contents
Decimal to Binary,
Decimal to Octal,
Decimal to Hexadecimal,
Binary To Decimal,
Octal To Decimal,
Hexadecimal To Decimal
To Find out the root of the Algebraic and Transcendental equations. Using following methods
1 Bisection
2 Regula-false
3 Newton Raphson
4 Iterative Methods.
To implement Newton’s Forward and Backward Interpolation formula.
To implement Gauss's Forward and Backward,s Interpolation formula
To implement Langranges's Interpolation formula.
To implement Numerical Integration using Trapezoidal, Simpson 1/3 and Simpson 3/8 rule.
To implement Least Square Method for curve fitting.
To draw pie-chart .
To estimate regression equation from sampled data and evaluate values of regression coefficient, value of R2 for at least two independent variables.
Wednesday, December 19, 2018
C program to find the roots of an equation,using Newton Raphson method
Newton-Raphson Method:
The Newton-Raphson method is a powerful and elegant method to find
the root of an equation. This method is
generally used to improve the results obtained by the previous methods. Let x0
be an approximate root of f(x)=0, and
let x1=x0+h be the
correct root which implies that f(x1)=0.
We use Taylor ’s theorem and expand
This is
called Newton-Raphson formula.
Example:
Apply
Newton-Raphson method to find an approximate root,correct to three decimal
places,of the equation
x3-3x-5=0,which
lies near x=2.
Solution:
The Newton-Raphson iterative
formula is
Since x3 and x4 are identical upto 3 places of
decimal, we take x4=2.279 as the required root, correct to three
places of the decimal.
Notes on Newton’s Method
Convergence rate for Newton ’s
method is very high!!
Error estimates are very good (however will be case dependent on the
form of the function f(x)
Newton’s method can find complex roots.
Problem with Newton’s
Method
If the local min/max is selected as an initial guess
C Program:
#include<stdio.h>
float f(float x);
float f1(float x);
void main()
{ float a,b,c,d;
int i,j,n, flag=0;
printf("\nEnter the no. of iterations");
scanf("%d",&n);
//printf("\nEnter the interval");
// scanf("%f%f",&a,&b);
for(j=-5;j<=5;j++)
{ a=j;b=j+1;
if(f(a)*f(b)<0)
{printf("\nInterval found between %fand%f",a,b);
printf("\nn\tc\tf(c)");
for(i=0;i<n;i++)
{ c=a-f(a)/f1(a);
printf("\n%d%10.6f %10.6f",i+1,c,f(c));
a=c;
}
printf("\nThe approximate root of equation is=%f",c);
flag=1;
}
}
if(flag==0)
printf("\n Interval did not find between range");
}
float f(float x)
{ return(x*x*x-3*x-5);
}
float f1(float x)
{return(3*x*x-3); }
Output:
Enter the no. of iterations 4
Interval found between 2.000000 and 3.000000
n c f(c)
1 2.333333 0.703703
2 2.280555 0.019351
3 2.279020 0.000016
4 2.279019 0.000001
The approximate root of equation is=2.279019
Subscribe to:
Posts (Atom)