Date post: | 21-Jul-2016 |
Category: |
Documents |
Upload: | tina-evans |
View: | 215 times |
Download: | 0 times |
AIM: To write an algorithm, to draw flow chart and to write a C programme
(a) To find sum of three numbers(b) Exchange of two numbers(c) Maximum of two numbers(d) To read and print variable values of all data types of C language(e) To find size of all data types(f) To understand the priority and associativity of all operators using expressions(g) To use different library functions of C language.
ALGORITHM
4(a) 1. Start
2. Read the values of a, b, c
3. Calculate sum = a+ b+ c
4. Print sum
5. Stop
FLOW CHART
C- Program:
#include<stdio.h>
main()
{
int a=5, b=10, c=15, sum;
sum=a+ b+c;
printf(“sum of three numbers is %d”, sum); }
Read a, b, c
Sum= a+ b+ c
Print sum
start
stop
ALGORITHM 4(b):
1. Start
2. Read the values of a, b
3. Assign t=a, a=b, b= t
4. Print a, b 5. Stop.
FLOW CHART
C- PROGRAM
#include<stdio.h>
main()
{
int a=10,b=20,t;
t=a;
a=b;
b=t;
printf(“a=%d\t b=%d”,a,b);
}
Read a, b
t=a, a=b, b=t
Print a, b
stop
start
Algorithm 4(c)
1. Start2. Read the values of a and b3. If a is greater than b then4. Print a is greater5. Else6. Printf b is greater7. End if8. Stop
Flow chart:
F
T
C- Program
#include<stdio.h>
#include<conio.h>
main()
{
int a, b;
clrscr();
printf(“enter the values of a and b”);
scanf(“%d%d”,&a,&b);
if(a>b)
printf(“a is greater”);
else
printf(“b is greater”);
}
start
Read a, b
Is a>b
Print a is greater Print b is greater
stop
ALGORITHM 4(d)
1. Start2. Read variables a and b as integers3. Read variable c as character4. Read variables x, y as real or floating point values.5. Print a, b, c, x, y6. Stop.
FLOW CHART:
C- Program:
#include<stdio.h>
main()
{
int a, b;
char c;
float d,e;
scanf(“%d%d%c%f%f”,&a,&b,&c,&d,&e);
printf(“integer values a=%d \t b=%d\t”,a,b);
printf(“character values c=%c\t”,c);
printf(“floating point values d=%f \t e=%f\t”,d,e);
}
Read a, b as integers,c as character d, e as floating point value
Print a, b, c, d, e
stop
start
ALGORITHM 4(e):
1. Start2. Read variables a and b as integers3. Read variable c as character4. Read variables x, y as real or floating point values.5. Print memory occupied by a, b, and the values of a, b6. Print memory occupied by c and the value stored in c7. Print memory occupied by x and y and the values of x and y8. Stop.
FLOW CHART:
C- Program:#include<stdio.h>main(){int a, b;char c;float d,e;scanf(“%d%d%c%f%f”,&a,&b,&c,&d,&e);printf(“memory occupied by integer variable is %d\t”,sizeof(a));printf(“\n memory occupied by character varible is %d\t”,sizeof(c));printf(“memory occupied by float variable is %d\t”,sizeof(e));printf(“integer values a=%d \t b=%d\t”,a,b);printf(“character values c=%c\t”,c);printf(“floating point values d=%f \t e=%f\t”,d,e);}
Read a, b as integers,c as character d, e as floating point value
Print a, b, c, d, e
Print a1, b1, c1, d1, e1
stop
start
Cal a1=sizeof(a) b1= sizeof(b) c1=sizeof(c) d1=sizeof(d) e1= sizeof(e)
ALGORITHM 4(f)
1. Start2. Read x, y, a, b3. Cal res=x * y + x/ y4. Cal r1= ( a/(a+(a*b)))5. Print res, r16. Stop
FLOWCHART
C PROGRAM
#include<stdio.h>
main()
{
int a, b, x, y, res, r1;
printf(“ enter the values of a,b,x and y\n”);
scanf(“%d%d%d%d”,&a,&b,&x,&y);
res=x*y+x/y;
r1=(a/(a+(a*b)));
printf(“result of first statement is%d”,res);
printf(“result of first statement is%d”,r1);}
Read x, y, a, b
Print res, r1
stop
Res= x*y+x/y
R1=(a/(a+(a*b)))
start
ALGORITHM: To illustrate the use of all operators
1. Start2. Read a,b3. Cal add=a+b, sub= a-b, mul=a*b, div=a/b, mod=a%b4. Relational operators r1= a>b, r2=a<b, r3=a<=b, r4=a>=b, r5= a==b,r6=a!=b5. Logical operators l1=a<5&&b>10 l2=a<10||b>1, l3=!a6. Increment and decrement operators incr=++a, incr1=a+++b++ incr2= a++*++b decr=a--+--b7. Assignment operator a+=b, a*=b8. Bit wise operators b1=a&b b2=a|b, b3=a^b,b4= a<<29. Comma operator c=(a=10,b=5,a+b)10. Conditional operator x=a>b?a:b11. Print add, sub, mul, div, mod12. Print r1, r2, r3, r4, r5, r613. Print l1, l2, l314. Print incr, incr1, incr2, decr15. Print b1, b2, b3, b4, c,x16. Stop
FLOWCHART
Read a, b
Print add, sub, mul, div, mod, r1, r2, r3, r4, r5, r6 l1, l2, l3, incr, incr1, incr2, decr, b1, b2, b3, b4, c, x
stop
Add= a+b, sub=a-b, mul=a*b, div = a/b, mod= a%b R1= a>b, r2=a<b, r3=a>=b, r4=a<=b r5=a==b r6= a!=b
start
l1=a<5&&b>10 l2=a<10||b>1, l3=!a incr =++a, incr1=a+++b++ incr2= a++*++b decr=a--+--b a+=b, a*=b b1=a&b b2=a|b, b3=a^b,b4= a<<2 c=(a=10,b=5,a+b) x=a>b?a:b
C- PROGRAM
#include<stdio.h>
main()
{
int a, b, x;
printf(“ enter the values of a, b\n”);
scanf(“%d%d”,&a,&b);
printf(“result of arithmetic operationsof a=%d and b=%d is”, a,b);
printf(“addition of two numbers=%d\n”,a+b);
printf(“subtraction of two numbers=%d\n”,a-b);
printf(“multiplication of two numbers=%d\n”,a*b);
printf(“division of two numbers=%d\n”,a/b);
printf(“modulo division of two numbers=%d\n”,a%b);
printf(“result of relational operators of a=%d and b=%d is\n”, a, b);
printf(“a<b is=%d \n a<=b=%d\n a==b=%d”, a<b, a<=b, a==b);
printf(“a >b=%d\n a>=b=%d\n a!=b=%d”, a>b, a>=b, a!=b);
printf(“result of logical operators of a=%d and b=%d is\n”, a, b);
printf(“a<5&&b>10 =%d”, a<5&&b>10);
printf(“a<10||b>1=%d”, a<10||b>1);
printf(“complement of a=%d”, !a);
printf(“result of increment and decrement operators of a=%d and b=%d is\n”, a, b);
printf(“pre increment=%d\n expr1 a+++b++=%d\n expr2 a++*++b=%d\n expr3 a--+--b=%d ”,++a, a+++b++, a+
+*++b, a--+--b);
printf(“result of assignment operators of a=%d and b=%d is\n”, a, b);
printf(“result of a+=b=%d \n, a*=b=%d is”, a+=b, a*=b);
printf(“result of bitwise operators of a=%d and b=%d is\n”, a, b);
printf(“ bit wise a&b=%d\n bitwise or a|b=%d\n bitwise ex-or a^b=%d\n, bitwise left shift a<<2=%dis\n”, a&b, a|b, a^b, a<<2 );
x=(a=10,b=5,a+b)
printf(“result of comma operator is%d \n”, x);
printf(“result of conditional operators is %d\n” , a>b ?a : b);
}
c-program 4(e)
#include<stdio.h>
#include<conio.h>
main()
{
int a, b, c;
clrscr();
printf(“enter the values of a, b, c”);
scanf(“%d %d %d”, &a, &b, &c);
printf(“sum of three numbers=%d\n, average of three numbers=%d\n ”,a+b+c, (a+b+c)/3);
getch();
}
5. AIM: To write an algorithm, to draw flowchart and to write a C program to find roots of a quadratic equation.
Algorithm:
1. Start2. Read the values of a, b, c3. Calculate discriminant = b2-4ac4. If discriminant <0 then5. Print roots are imaginary6. End if7. If discriminant > 0 then8. R1= (-b +(√discriminant))/2*a;9. R2= (-b -(√discriminant))/2*a;10. End if11. Stop.
Flow chart:
F
T
start
Read a, b, c
Dis=b*b-4*a*c
Is dis>0
R1= (-b+√dis)/2*a
R2= (-b-√dis)/2*a
Print roots are imaginary
stop
C- Program:
#include<stdio.h>
#include<conio.h>
main()
{
float a, b, c, r1, r2, dis;
clrscr();
printf(“enter the values of a, b, c”);
scanf(“%d%d%d”, &a, &b, &c);
dis = b*b-4*a*c;
if(dis>0)
{
r1= (-b+sqrt(dis))/2*a;
r2= (-b-sqrt(dis))/2*a;
printf(“roots r1=%f\n r2=%f”, r1,r2)
}
else
printf(“roots are imaginary”);
getch();
}
6.AIM: To write an algorithm , to draw flow chart and to write C program to find factorial of a number.
Algorithm:
1. Start2. Read a number n3. Assign fact=14. If n==0 or n==1then print 1 go to step 95. If n>1 then6. while i<=n do7. Fact= fact*i8. i++9. End while10. Print fact11. Stop.
Flowchart:
F
T
F
T
start
Read n, fact=1
If(n==0||n==1)
While n>1
Fact=fact*I i++
stop
Print fact
C- Program
#include<stdio.h>
#include<conio.h>
main()
{
int n, fact=1,i=1;
clrscr();
printf(“enter the value of n”);
scanf(“%d”, &n);
if(n==0||n==1)
printf(“%d”, fact);
else
{
while(i<=n)
{
fact= fact*i;
i++;
}
printf(“factorial of given number %d\t%d”, n, fact);
}
getch();
}
2) AIM: To write an algorithm and to draw flow chart to check whether the given number is prime number or not.
ALGORITHM:
1. Start2. Read n, c=03. For i=2 to n do4. if (n % i==0) then5. c++; 6. break;7. end if8. end for9. if c= =1 then print given number is not prime10. else print given number is a prime number.11. end if12. end for13. stop
FLOW CHART:
F
T
F
T
T
F
T
start
Read n, c=0
For I in 2 to n
If n%i ==0
C= c + 1
If c==1
Print prime
stop
Print not prime
C – PROGRAMME:
#include<stdio.h>
main()
{
int n, i, c=0;
printf(“enter the value of n”);
scanf(“%d”, &n);
for( i=2; i<n; i++)
if( n % i = = 0)
{
c++;
break;
}
if(c==1)
printf(“given number is not a prime number\n”);else
printf(“given number is prime”);
}
8 AIM: To write an algorithm and to draw flow chart for printing prime numbers between 1 and n
ALGORITHM:
14. Start15. Read n, c=016. For i=1 to n do17. For j=1to I do18. if (i % j==0) then19. c++; 20. break;21. end if22. end for23. if c= =2 then print i24. end if25. end for26. stop
FLOW CHART:
F
T
F
T
F
T
F
T
start
Read n, c=0
For I in 1 to n
For j=1 to I do
If i%j ==0
C= c + 1
If c==2
Print i
stop
C – PROGRAMME:
#include<stdio.h>
main()
{
int n, i, j, c=0;
printf(“enter the value of n”);
scanf(“%d”, &n);
for( i=1; i<=n; i++)
{
for(j=1;j<=i; j++)
{
if( i % j = = 0)
{
c++;
break;
}
}
if(c==2)
printf(“%d\n”,i);
}
}
9. AIM: To write an algorithm and to draw a flow chart to generate Fibonacci numbers in the given range ALGORITHM:
1. Start2. Read n, i3. Assign f0=0, f1=1, f2=f0+f14. Print f0, f1,f25. For I =0 to n-3 repeat6. F0=f1;7. F1=f2;8. F2=f0+f1;9. Print f210. End for11. Stop.
FLOW CHART:
Read n
start
F0=0, f1=1 f2=f0+f1
Print f0,f1,f2
For i=0 to n-3
F0=f1, f1=f2, f2=f0+f1
Print f2
stop
C-PROGRAMME
#include<stdio.h>
main()
{
int n,f0,f1,f2,i;
printf(“enter the value of n”);
scanf(“%d”, &n);
f0 = 0;
f1= 1;
f2= f0+f1;
printf(“%d \n %d\n %d\n”,f0,f1,f2);
for(i=1; i<=n-3;i++)
{
f0=f1;
f1=f2;
f2=fo+f1;
printf(“%d\n”,f2);
}
}
10 AIM: To write an algorithm, to draw flow chart and to write a C program to find the maximum of a set of numbers.Algorithm:
1. Start2. Read the size of array n 3. Read an array of n numbers4. Assign max= a[0]5. For I in 1 to n do6. if(a[i] > max) then7. max=a[i]8. print max9. stop
FLOW CHART:
F
T
F
T
Read an array of integers values
start
Max=a[0]
For i=1 to n
Max=a[i]
Print max
stop
Is a[i] >max
C- program
#include<stdio.h>
#include<conio.h>
main()
{
int n,a[10], I, max;
clrscr();
printf(“enter the value of n”);
scanf(“%d”, &n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
max=a[0];
for(i=1; i<n;i++)
{
if(a[i]>max)
max=a[i];
}
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
printf(“maximum of all numbers%d\n”,max);
}