Sunteți pe pagina 1din 27

PRACTICAL 6

Exercise1.1a
The program will not work. Because “int x,y” is found outside the main function.

Exercise 1.2

#include <stdio.h>
int main()
{
int x,y;
printf("\nEnter two numbers");
scanf("%d %d",&x,&y);
printf("\n\n%d is bigger",(x>y)?x:y);
return 0;
}

Exercise 1.3

if (x >=1 && x <= 20)


x = y;
else
y = y;

Exercise 1.4

if (x<1 && x>10)


statement;

Exercise 1.5

(a). 7
(b). 0
(c). 9
(d). 5
(e). no value

Exercise 1.6

(a). true
(b). false
(c). error
(d). error

1
Exercise 1.7

if (age>=21 && age<=65)


printf(“The person is legally an adult”);

else if (age>=65)
printf(“The person is a senior citizen”);

Exercise 1.8

#include <stdio.h>
int main()
{
int x= 1;

if( x = 1)
printf(" x equals 1" );
else
printf(" x does not equal 1");
return 0;
}

Exercise 1.9

DECLARATION OF VARIABLES:
int i,j;
long ix ;
short s;
float x;
double dx;
char c;

EXPRESSIONS:

(a) i + c int
(b) x + c float
(c) dx + x float
(d) ((int)dx) + ix int
(e) i + x float
(f) s + j int
(g) ix + j int
(h) s + c int
(i) ix + c int
2
Exercise 1.10

a) k = (i + j ) = 13
b) z = (x + y) = -0.005
c) i=j=5
d) k = (x + y) = 0
e) k = c = garbage value
f) z = i / j = 1.600
g) a = b = d = ‘d’
h) i= j = 1.1 =1
i) z = k = x = 0.000
j) k=z=x=0
k) i += 2 = 10
l) y -= x = -0.015
m) x *= 2 =0.01
n) i /= j = 1
o) i %= j = 3
p) i += ( j - 2) = 11

Exercise 1.11

(a) scanf(“%d%d%f%f”,&i,&j.&x,&dx);
(b) scanf(“%d%L%d%f%u”,&i,&ix,&j,&x,&u);
(c) scanf(“%d%u%c”,&i,&u,&c);
(d) scanf(“%c%f%f%f”,&c,&x,&dx,&s);

Exercise 1.12

#include <stdio.h>
Float x,y,z;
(a) Printf(“x, y and z are %f, %f and %f”, x, y, z);
(b) Printf(“x+y is equal to %f, and x-z is equal to %f”,(x+y), (x-y));
(c) Printf(“The squareroot of (x+y) is %f and the absolute value of (x-z) is %f”,
sqrt(x+y), Fabs(x-z));

Exercise 1.13

#include <stdio.h>
int main()
{
int n=0, sum=0, i=1;
printf("Enter a number. : ");
scanf("%d", &n);
while(n>0)
{
3
i = n%10;
printf("%d", i);
sum+= i; n = n/10;
}
printf("\nSum of the number is %d",sum);
return 0;
}

Exercise 1.14

#include <stdio.h>
int main()
{
int x,y;
printf("Enter a number: ");
scanf("%d", &x);
printf("Now enter a second number: ");
scanf("%d", &y);
if(x%y==0 && x>y)
printf("x is evenly divisible by y");
else
printf("x is not evenly divisible by y");
return 0;
}

Exercise 1.15

#include<stdio.h>
int main()
{
int x,y;
float div;
div = x/y;
printf("Enter first number: ");
4
scanf("%d", &x);
printf("Enter second number: ");
scanf("%d", &y);
if (y==0)
printf("NO SOLUTION!");
else
printf("The answer is %5.3f\n", div);
return 0;
}

Exercise 1.16

#include <stdio.h>
int main (int argc, const char * argv[])
{
int number, digit1, digit2, digit3, digit4, digit5;
printf("Enter a number (max 5 digits): ");
scanf("%i", &number);
if (number < 100000){
digit1 = number % 100000 / 10000;
switch (digit1)
{
case 0 : printf("Zero "); break;
case 1 : printf("One "); break;
case 2 : printf("Two "); break;
case 3 : printf("Three "); break;
case 4 : printf("Four "); break;
case 5 : printf("Five "); break;
case 6 : printf("Six "); break;
case 7 : printf("Seven "); break;
case 8 : printf("Eight "); break;
case 9 : printf("Nine "); break;
default : printf("Unknown Interger "); break;
}

digit2 = number % 10000 / 1000;

switch (digit2)
{
case 0 : printf("Zero "); break;
case 1 : printf("One "); break;
case 2 : printf("Two "); break;
case 3 : printf("Three "); break;
5
case 4 : printf("Four "); break;
case 5 : printf("Five "); break;
case 6 : printf("Six "); break;
case 7 : printf("Seven "); break;
case 8 : printf("Eight "); break;
case 9 : printf("Nine "); break;
default : printf("Unknown Interger "); break;
}

digit3 = number % 1000 / 100;

switch (digit3)
{
case 0 : printf("Zero "); break;
case 1 : printf("One "); break;
case 2 : printf("Two "); break;
case 3 : printf("Three "); break;
case 4 : printf("Four "); break;
case 5 : printf("Five "); break;
case 6 : printf("Six "); break;
case 7 : printf("Seven "); break;
case 8 : printf("Eight "); break;
case 9 : printf("Nine "); break;
default : printf("Unknown Interger "); break;
}

digit4 = number % 100 / 10;

switch (digit4)
{
case 0 : printf("Zero "); break;
case 1 : printf("One "); break;
case 2 : printf("Two "); break;
case 3 : printf("Three "); break;
case 4 : printf("Four "); break;
case 5 : printf("Five "); break;
case 6 : printf("Six "); break;
case 7 : printf("Seven "); break;
case 8 : printf("Eight "); break;
case 9 : printf("Nine "); break;
default : printf("Unknown Interger "); break;
}

digit5 = number % 10;

switch (digit5)
{
case 0 : printf("Zero "); break;
case 1 : printf("One "); break;
case 2 : printf("Two "); break;
case 3 : printf("Three "); break;
6
case 4 : printf("Four "); break;
case 5 : printf("Five "); break;
case 6 : printf("Six "); break;
case 7 : printf("Seven "); break;
case 8 : printf("Eight "); break;
case 9 : printf("Nine "); break;
default : printf("Unknown Interger "); break;
}
}
else
{
printf("Entered Number is too long.");
}
return 0;
}

7
PRACTICAL TUTORIAL 7

NO 1.

#include <stdio.h>
#include <math.h>
int main()
{
int usenum;
printf("Type in your number: ");
scanf("%d", &usenum);

if(usenum < 0)
printf("The absolute value is %d \n", abs(usenum));

else
printf("The absolute value is %d \n", usenum);
return 0;
}

NO 2.
#include <stdio.h>
int main()
{
int x;
printf("Type in a number: ");
scanf("%d", &x);

if(x%2 == 0)
printf("The number is even");

else
printf("The number is odd");
return 0;
}

8
NO 3.
#include<stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);

if (year%4 == 0 && year%100 == 0 && year%400 == 0)


printf("The year is a leap one");

else
printf("The year is not a leap one");
return 0;
}

NO 4.
#include <stdio.h>
int main()
{
int firstnum, secondnum;
printf("Enter first number:");
scanf("%d",&firstnum);
printf("\nEnter Second number:");
scanf("%d",&secondnum);
if (firstnum%secondnum==0)
if((firstnum/secondnum)%2==0)
printf("\nThe Frist number is evenly divisible by Second number\n");
else
printf("\nThe Frist number is NOT evenly divisible by Second number\n");

else
printf("\nThe Frist number is evenly divisible by Second number\n");
9
return 0;
}

NO 5.
#include <stdio.h>
int main()
{
int num1,num2;
float division;
printf ("Enter First number:");
scanf ("%d",&num1);
while (num1==0)
{
printf ("Number Cannot be Zero\nEnter another number:");
scanf ("%d",&num1);
}
printf ("Enter Second number:");
scanf ("%d",&num2);
while (num2==0)
{
printf ("Number Cannot be Zero\nEnter another number:");
scanf ("%d",&num2);
}
division=(num1/num2);
printf ("Division of First and Second number=%.3f",division);
return 0 ;
}

NO 6.
#include <stdio.h>
int main (void)
{
float accumulator, value;
char op;

printf ("Begin Calculations\n");

do
{
10
scanf("%f %c", &value, &op);

switch (op)
{
case 'S':
accumulator = value;
printf("= %f\n",accumulator);
break;

case '+':
accumulator = accumulator + value;
printf("= %f\n",accumulator);
break;

case '-':
accumulator = accumulator - value;
printf("= %f\n",accumulator);
break;

case '*':
accumulator = accumulator * value;
printf("= %f\n",accumulator);
break;

default:
printf ("Error! Unknown operator.\n");
}

}
while (op != 'E' );

printf("= %f\n",accumulator);
printf("End of Caculations\n");
return 0;
}

11
NO 7.
(a).
#include<stdio.h>
int main()
{
int i, total;
i = 2;
total = 0.0;

while(i<100)
{
total = total + i;
i = i+3;
printf("The answer is %d\n",total);
}
return 0;
}

(c).
#include<stdio.h>
int main()
{
int i, total;
i = 2;
total = 0.0;

for(i = 2; i<=100; i = i+3)


{
total = total + i;
printf("The answer is %d\n",total);
}
return 0;
}

12
NO 8.
(a).
#include <stdio.h>
void main ()
{
int i, total, testnum;
total = 0;

for(i=2; i<100; i+=3)


{
testnum = i%5;
total = (testnum==0)? total+i: total;
}

printf ("The sum of every third integer, beginning with i=2, is %i.\n", total);
}

(b).
#include <stdio.h>
void main ()
{
int i, total;
total = 0;

for(i=2; i<100; i+=3)


{
if (i%5==0)
total = total + i;
}

printf ("The sum of every third integer, beginning with i=2, is %i.\n", total);
}

13
14
PRACTICAL TUTORIAL 8
NO 1.
float do_it(char a, char b, char c)

NO 2.
void print_a_number(int num)

NO 3.
(a). int
(b). long

NO 4.
function does not take 1 arguments
no value to return

NO 5.
There should be no semicolon after "int twice(int y)".

NO 6.

#include<stdio.h>
int main()
{
int product(int, int);
int x,y,xy;

printf("Enter first number: ");


scanf("%d", &x);
printf("Enter second number: ");
scanf("%d", &y);

xy = product(x,y);
printf("The product is %d\n", xy);
return 0;
}
int product(int a, int b)
{
int prod;
prod = a*b;
return (prod);
}

NO 7.
#include <stdio.h>
float division(float, float);
15
void main (void)
{
float firstnum, secnum;
float result;

printf ("Enter a value for first number:\n");


scanf ("%f", &firstnum);

printf ("Enter a value for second number:\n");


scanf ("%f", &secnum);

division (firstnum, secnum);


result = firstnum/secnum;

if (secnum!=0)
printf ("The division of the 1st number by the 2nd number is %1.3f.\n",
result);

float division(float firstnum, float secnum)


{
if (secnum==0)
printf ("Error. A value cannot be divided by zero.\n");

else
{
float result = firstnum/secnum;
return (result);
}
}

NO 8.
#include<stdio.h>
int main()
{
float average(float, float,float,float,float);
float a,b,c,d,e,ave;

printf("Enter first number: ");


scanf("%f", &a);
printf("Enter second number: ");
scanf("%f", &b);
16
printf("Enter third number: ");
scanf("%f", &c);
printf("Enter fourth number: ");
scanf("%f", &d);
printf("Enter fifth number: ");
scanf("%f", &e);

ave = average(a,b,c,d,e);
printf("The average is %f\n", ave);

return 0;
}
float average(float p, float q,float r,float s,float t)
{
float aver;
aver = (p+q+r+s+t)/5;

return (aver);
}

NO 8.
#include <stdio.h>
#include <math.h>
int power (int);
void main (void)
{
int x;

printf ("Enter a value:\n");


scanf ("%i", &x);

printf ("3 raised to the power of %i is %i.\n", x, power(x));


}
int power(int x)
{
if(x==0)
return 1;
else
return power(x-1)*3;
}

17
18
PRACTICAL TUTORIAL 9

NO 1.
Semicolon after function header must not be included

NO 2.
The output would be: 4 16
The function show() has already returned the square of x to the main function. Hence
the second printf function and the return of cube x to the main are ignored.

NO 3.
The function will return the value of 1 to the main only if a=b AND a=c. Else if a!=b
and a!=c, or if a!=b but a=c, or if a=b but a!=c, the function will return the value of
zero to the main.
NO 4.
#include <stdio.h>
int main()
{
int maxnum(int, int);
int x,y,max;
printf("Enter first number: ");
scanf("%d", &x);
printf("Enter second number: ");
scanf("%d", &y);

max = maxnum(x,y);
return 0;
}
int maxnum(int a, int b)
{
int maxi;
if (a>=b)
maxi = a;
else
maxi = b;
printf("The larger number is %d\n", maxi);
return (maxi);
}

NO 5.
#include <stdio.h>
int main()
{

19
int maxnum(int, int, int);
int x,y,z, max;
printf("Enter first number: ");
scanf("%d", &x);
printf("Enter second number: ");
scanf("%d", &y);
printf("Enter third number: ");
scanf("%d", &z);

max = maxnum(x,y,z);
return 0;
}
int maxnum(int a, int b, int c)
{
int maxi;
if (a>=b && a>=c)
maxi = a;
else if (b>=a && b>=c)
maxi = b;
else
maxi = c;
printf("The largest number is %d\n", maxi);
return (maxi);
}

NO 6.
#include<stdio.h>
#include <math.h>
int main ()
{
int absolute(int);
int x, absol;

printf("Enter a value: ");


scanf("%d", &x);

absol = absolute(x);
printf("The absolute value of %d is %d\n", x, absol);
return 0;
}
absolute(int a)
{
int abst;
abst = abs(a);
20
return (abst);
}

NO 7.
int f(int n)
{
int i = 1;
while (i < n)
i += i;
return i;
}

NO 8.
#include<stdio.h>
int prime (int);
void main()
{
int n;
printf ("Enter a positive integer number you want to test: ");
scanf("%i", &n);
prime(n);
printf ("If '1' is displayed, then it is a prime.\n");
printf ("If '0' is displayed, then it is not a prime.\n");
printf ("%i.\n", prime(n));
}

int prime (int n)


{
int i, c=0;
for(i=1; i<=n ;i++)
{
if(n%i==0)
{
c=c+1;
}
}
if(c==2)
return 1;
else
return 0;
}

21
NO 9.
#include<stdio.h>
int prime (int);
void main()
{
int n;
printf ("Enter a positive integer number you want to test: ");
scanf("%i", &n);
prime(n);
printf ("If '0' is displayed, then it is a prime.\n");
printf ("If '1' is displayed, then it is not a prime.\n");
printf ("%i.\n", prime(n));
}

int prime (int n)


{
int i, c=0;
for(i=1; i<=n ;i++)
{
if(n%i==0)
{
c=c+1;
}
}
if(c==2)
return 0;
else
return 1;
}

NO 11.
The output is: 9 11

NO 12.
The output is: 26 12

22
NO 13.
'{' : missing function header (old-style formal list?)
syntax error : missing ';' before identifier 'b'
syntax error : missing ';' before 'else'

PRACTICAL TUTORIAL 10
NO 1.

NO 2.

NO 3.

23
NO 4.
#include<stdio.h>

void main ()
{
float value[11];
float sum, averageValue;
int i;

printf ("Enter any ten values below:\n");


for (i=1; i<=10; ++i)
scanf ("%f", &value[i]);

sum = 0;
for (i=1; i<=10; ++i)
sum += value[i];

averageValue = sum/10;
printf ("The average of the entered values are %f.\n", averageValue);
}

NO 5.
#include<stdio.h>
void main()
{
24
int P[150], i, j;

for(i=0; i<=149; i++)


P[i]=i+1;

for(i=1; i<=149; i++)


{
if(P[i]!=0)
{
for(j=(i+1); j<=149; j++)
{
if(P[j]!=0)
{
if((P[j]%P[i])==0)
P[j]=0;
}
}
}
}

for(i=0; i<=149; i++)


{
if(P[i]!=0)
printf("%d\n",P[i]);
}
}

NO 6.
#include <stdio.h>
#define r 2
#define c 3
void readinput(int[r][c],int[r][c]);
void computesums(int [r][c], int [r][c],int [r][c]);
void writeoutput(int [r][c]);

void main()
25
{
int Table1[r][c],Table2[r][c];
int sum[r][c];

readinput(Table1,Table2);
computesums(Table1, Table2, sum);
writeoutput(sum);
}

void readinput(int Table1[r][c],int Table2[r][c])


{
int i,j;
printf("For Table 1\n");
for (i=0; i<r; i++)
{
printf("Enter numbers for row %i:\n", i+1);
for(j=0; j<c; j++)
scanf("%d",&Table1[i][j]);
}
printf("For Table 2\n");
for (i=0; i<r; i++)
{
printf("Enter numbers for row %i:\n",i+1);
for(j=0; j<c; j++)
scanf("%d",&Table2[i][j]);
}
}

void computesums(int Table1[r][c],int Table2[r][c],int sum[r][c])


{
int i,j;
for (i=0; i<r; i++)
for (j=0; j<c; j++)
sum[i][j] = Table1[i][j] + Table2[i][j];
}

void writeoutput (int sum[r][c])


{
int i,j;
printf("The sum of the two tables is:\n");
for (i=0; i<r; i++)
{
printf("\n");

for (j=0; j<c; j++)


printf("%7d",sum[i][j]);

printf("\n");
}
}

26
27

S-ar putea să vă placă și