Anda di halaman 1dari 30

INTRODUCTION TO

PROGRAMMING

MRIDUL
291/COE/14
COE 2

S.No
.

Title Of The Program

1.

Program to find the sum of digits of an


integer

2.

Program to find GCD and LCM of two


numbers

3.

To find sum of n integers using array

4.
5.

Program to compute the value of sinx


using exponential series
A program that takes coefficients of a
quadratic equation as input and outputs
the roots of the quadratic equation.

6.

To find the factorial of any integer.

7.
8.

Program to make a calculator.


Write a program to find the greatest
amongst three numbers.
To find the multiplication of digits of any
integer.
To find sum of n integers using array

9.
10.
11.

14.

To sort n integers in ascending and


descending order.
To find the average, sum and standard
deviation for n numbers.
To print the multiplication table from 1 to
10.
To find addition of two matrices

15.

To find subtraction of two matrices

16.

To find multiplication of two matrices

17.

To find the factorial of a number by


recursion.
Write a program to find the nth Fibonacci
number using recursion.
Program using recursion to find the sum of
digits of any integer.

12.
13.

18.

19.

COE 2

MRIDUL

Date

Pag
e
No.

Remark
s

291/COE/14

Q1.Program to find the sum of


digits of an integer.
FLOWCHART

COE 2

MRIDUL

291/COE/14

SOURCE CODE
#include<stdio.h>
int main()
{
int a,sum=0;
printf(Enter an integer);
scanf(%d,&a);
while(a>0)
{
sum+=a%10;
a/=10;
}
printf(Sum of digits of integer is %d.,sum);
return 0;
}

OUTPUT

COE 2

MRIDUL

291/COE/14

Q2. Program to find GCD and LCM of


two numbers.
#include <stdio.h>
#include <stdlib.h>
int gcd(int a,int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
int main()
{
int x,y,lcm,g;
printf("Enter two numbers.");
scanf("%d %d",&x,&y);
g=gcd(x,y);
printf("GCD=%d\n",g);
lcm=x*y/g;
printf("LCM=%d",lcm);

return 0;}
OUTPUT

Q3.Program to find the greatest


among three numbers.
COE 2

MRIDUL

291/COE/14

FLOWCHART

SOURCE CODE
#include<stdio.h>
COE 2

MRIDUL

291/COE/14

int main()
{
int a,b,c,max;
printf(Enter three integers:);
scanf(%d%d%d,&a,&b,&c);
max=a;
if(max<b)
max=b;
if(max<c)
max=c;
printf(Greatest among three integers is %d.,max);
return 0;
}
OUTPUT

Q4.Program to compute the value


of sinx using exponential series
FLOWCHART

COE 2

MRIDUL

291/COE/14

SOURCE CODE
#include<stdio.h>
#include<conio.h>
#include<math.h>
long fact(int i)
{
if(i!=0)
COE 2

MRIDUL

291/COE/14

return i*fact(i-1);
else
return 1;
}
void main()
{
int i;
float x, y, sum=0, temp;
clrscr();
printf("Enter angle in degrees\n");
scanf("%f", &x);
y=x*3.1415/180;
for(i=1;i<=15;i=i+2)
{
temp=pow(y,i)/fact(i)*pow(-1,((i-1)/2));
sum=sum+temp;
}
printf("value of sin(%f) is %f\n", x, sum );
getch();
}
OUTPUT

Q5. A program that takes


coefficients of a quadratic equation
as input and outputs the roots of
the quadratic equation.
SOURCE CODE :
COE 2

MRIDUL

291/COE/14

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main() {
float a, b, c, D ,x1, x2, rp , ip;
printf("\nEnter the coefficients of quadratic equation in the form of
ax^2+bx+c = \n");
scanf("%f %f %f", &a, &b,&c);
D=b*b-4*a*c;
if(D>=0) {
x1= (-b+sqrt(D))/(2*a);
x2= (-b-sqrt(D))/(2*a);
printf("The roots are %f %f", x1, x2);
}
else {
rp=b/(2*a);
ip=sqrt(-D)/(2*a);
printf("The roots are %f +i%f ,%f -i%f", rp, ip, rp,ip);
}
return 0 ;
getch();
}
OUTPUT:

Q6.To find the factorial of any


integer.
FLOWCHART

COE 2

MRIDUL

291/COE/14

SOURCE CODE
#include<stdio.h>
COE 2

MRIDUL

291/COE/14

#include<conio.h>
void main()
{
int num, num1;
long fact=1;
clrscr();
printf("Enter any number\n");
scanf("%d",&num);
num1=num;
while(num>0)
{
fact=fact*num;
num--;
}
printf("Factorial of %d is %ld", num1, fact);
getch();
}

OUTPUT

Q7.Program to make a calculator.


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
float a,b,c;

COE 2

MRIDUL

291/COE/14

int ch;
do
{
printf("\nEnter The Numbers :");
scanf("%f%f",&a,&b);
printf("\t\tMenu\n1.Add\n2.Subtract\n3.Multiply\n4.Divide\n5.Exit\nEnter
Your Choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("The Sum Is :%f\n",a+b);
break;
case 2:printf("The Diff. Is :%f\n",a-b);
break;
case 3:printf("The Product Is :%f\n",a*b);
break;
case 4: if(b!=0)
printf("The Result Is :%f\n",a/b);
else
printf("Try Again!! ");
break;
case 5:exit(0);
default :printf("Wrong Choice!!!!!\n");
}
}while(1);
return 0;
}
OUTPUT

COE 2

MRIDUL

291/COE/14

Q8.Program to find the greatest


among three numbers.
FLOWCHART

COE 2

MRIDUL

291/COE/14

SOURCE CODE
#include<stdio.h>
int main()
{
int a,b,c,max;
COE 2

MRIDUL

291/COE/14

printf(Enter three integers:);


scanf(%d%d%d,&a,&b,&c);
max=a;
if(max<b)
max=b;
if(max<c)
max=c;
printf(Greatest among three integers is %d.,max);
return 0;
}
OUTPUT

Q9. To find the multiplication of


digits of any integer.
#include<stdio.h>
int main()
{
int p=1,n;
printf("Enter any integer: ");
scanf("%d",&n);

COE 2

MRIDUL

291/COE/14

while(n)
{
p*=(n%10);
n/=10;
}
printf("%d",p);
return 0;
}

OUTPUT

Q10.To find sum of n integers using


array
#include <stdio.h>
int main()
{
int n;
printf("How many numbers\n");

COE 2

MRIDUL

291/COE/14

scanf("%d",&n);
int a[n],i,sum=0;
printf("Enter the numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
printf("Sum of the numbers entered is %d",sum);
return 0;
}
OUTPUT

Q11.To sort n integers in ascending


and descending order.
SOURCE CODE
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n,temp,a[10];
printf("How many numbers will you enter\n");

COE 2

MRIDUL

291/COE/14

scanf("%d", &n);
printf("Enter numbers\n");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
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("\nAscending Order\n");
for(i=0;i<n;i++)
printf("%d\t", a[i]);
printf("\nDescending Order\n");
for(i=n-1;i>-1;i--)
printf("%d\t", a[i]);
getch();
}

OUTPUT

COE 2

MRIDUL

291/COE/14

Q12.To find the average, sum and


standard deviation for n numbers.
SOURCE CODE
#include<stdio.h>
#include<math.h>
int main()
{
int i,n;
float a[1000],avg,sum=0,sd;
printf("Enter the value of n:");
scanf("%d",&n);
printf("Enter %d integers:",n);
for(i=0;i<n;i++)

COE 2

MRIDUL

291/COE/14

{
scanf("%f",&a[i]);
sum+=a[i];
}
printf("Sum is %g.\n",sum);
avg=sum/n;
printf("Average is %g.\n",avg);
sum=0;
for(i=0;i<n;i++)
{
a[i]=pow((a[i]-avg),2);
sum+=a[i];
}
avg=sum/n;
sd=sqrt(avg);
printf("Standard deviation is %g.\n",sd);
return 0;
}
OUTPUT

Q13.To print the multiplication


table from 1 to 10.
SOURCE CODE

#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=10;i++)
{ for(j=1;j<=5;j++)
printf("%d * %d = %d\t", j,i,i*j);
}
printf("\n");
getch();
}

COE 2

MRIDUL

291/COE/14

OUTPUT

Q14. To find addition of two


matrices
#include <stdio.h>
int main()
{
intn,m,i,j;
printf("Enter the no. of rows and columns in matrix-\n");
scanf("%d%d",&m,&n);
int a[m][n],b[m][n],c[m][n];
printf("Enter the first matrix(row-wise)\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the second matrix(row-wise)\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)

COE 2

MRIDUL

291/COE/14

c[i][j]=a[i][j]+b[i][j];
printf("Sum of the two matrices entered is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d ",c[i][j]);
}
return 0;
}

OUTPUT

COE 2

MRIDUL

291/COE/14

Q15.To find subtraction of two


matrices
#include <stdio.h>
int main()
{
intn,m,i,j;
printf("Enter the no. of rows and columns in matrix-\n");
scanf("%d%d",&m,&n);
int a[m][n],b[m][n],c[m][n];
printf("Enter the first matrix(row-wise)\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the second matrix(row-wise)\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)

COE 2

MRIDUL

291/COE/14

for(j=0;j<n;j++)
c[i][j]=a[i][j]-b[i][j];
printf("Difference of the two matrices entered is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d ",c[i][j]);
}
return 0;
}
OUTPUT

COE 2

MRIDUL

291/COE/14

Q16. To find multiplication of two


matrices
#include <stdio.h>
int main()
{
intn,m,p,q,i,j,k;
printf("Enter the no. of rows and columns in first matrix-\n");
scanf("%d%d",&m,&n);
int a[m][n];
printf("Enter the no. of rows and columns in second matrix-\n");
scanf("%d%d",&p,&q);
int b[p][q];
printf("Enter the first matrix(row-wise)\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the second matrix(row-wise)\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
if(n!=p)
printf("Matrix multiplication not possible\n");
else
{
int c[m][q];
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{

COE 2

MRIDUL

291/COE/14

c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]+=a[i][k]*b[k][j];
}
printf("Product of the two matrices entered is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf("%d ",c[i][j]);
}
}
return 0;}

OUTPUT

COE 2

MRIDUL

291/COE/14

Q17.To find the factorial of a


number by recursion.
#include<stdio.h>
long int fact(long int n)
{
if(n==1)
return 1;
return n*fact(n-1);
}
int main()
{
long int x;
printf("Enter the Number :");
scanf("%d",&x);
printf("The Factorial is :%d",fact(x));
return 0;
}

OUTPUT

COE 2

MRIDUL

291/COE/14

Q18.Program using recursion to


find the nth Fibonacci number.
#include<stdio.h>
int fib(int n)
{

if(n==1||n==2)
return 1;

return fib(n-1)+fib(n-2);
}
int main()
{
int n,x;
printf("Enter the value of n:");
scanf("%d",&n);
if(n>=1)
{
x=fib(n);
printf("%dth Fibonacci no. is %d.",n,x);
}
else
printf("Fibonacci no. does not exist.");
return 0;
}

COE 2

MRIDUL

291/COE/14

OUTPUT

Q19. Program using recursion to


find the sum of digits of any
integer.
#include<stdio.h>
int sum(long int n)
{
if(n==0)
return 0;
return (n%10)+sum(n/10);
}
int main()
{
long int x;
printf("Enter the Number :");
scanf("%d",&x);
printf("The Sum Of Digits is :%d",sum(x));
return 0;
}
OUTPUT

COE 2

MRIDUL

291/COE/14

Anda mungkin juga menyukai