Documente Academic
Documente Profesional
Documente Cultură
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
Exercise 1.4
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
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;
}
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;
}
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;
}
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;
}
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);
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;
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;
12
NO 8.
(a).
#include <stdio.h>
void main ()
{
int i, total, testnum;
total = 0;
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;
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;
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;
if (secnum!=0)
printf ("The division of the 1st number by the 2nd number is %1.3f.\n",
result);
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;
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;
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;
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));
}
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));
}
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;
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;
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);
}
printf("\n");
}
}
26
27