Lab Exercise For C Programming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 51

Lab exercise

Lab-1
1. Type the following program in c editor and execute it.mention the error.
#include<stdio.h>
#include<conio.h>
Void main()
{
Clrscr();
Printf(“this is my first program in c”);
Getch();
}

The out put is:- this is my first program in c

2. Add the following line at the beginning of the above program. Recompile the
program. What is the output?

#include<stdio.h>
 The out put is error b/c the #unclude<conio.h> and getch();
also needed. After this it is ok the out put is this is my
first program in c.

3. Make the following changes to the program. What Errors are observed?
i. Write Void instead of void .
ii. Write void main (void);
iii. Remove the semi colon ‘;’.
iv. Erase any one of brace ‘{’ or ‘}’.

 Answer
i. Declaration syntax error b/c void V is a capital letter.
ii. Declaration terminated incorrectly b/c void main (void);semi colon is
unnessacery /is not needed
iii. Success or correct
iv. Declaration syntax error – style of function definition is now absolute
,unexpected
4. Type and execute the following program
#include<stdio.h>
void main()
{
printf(“******”);
printf(“******”);
printf(“******”);
printf(“******”);
printf(“******”);
printf(“******”); }
 Answer
#include<stdio.h>

#include<conio.h>
Void main()
{
Clrscr();
printf(“******”);
printf(“******”); The output is ******
printf(“******”); ******
printf(“******”); ******
printf(“******”); ******
printf(“******”); ******
getch();
}

Lab-2
Exercise
1. Write a program to convert the temperature unit from Fahrenheit to Celsius using
the formula C=(F-32)/ 1.8 .
#include<stdio.h>
#include<conio.h>
Void main()
{
Float F,C;
Clrscr();
Printf(“enter degree fahrenhet\n”);
Scanf(“%f”,&F);
C=(F=32)/1.8;
Printf(“F=%f to c =%f\n”,F,C);
Getch();
}
The out put:- is if you enter 42F, the out put is F= 42.000000 to C= 5.555555
2. Write a C program to display Your Name, Address and City in different lines.
#include<stdio.h>
#include<conio.h>
void main()
{
char Name[20];
char Address[20];
char City[20];
clrscr();
printf("\n enter your information");
scanf("%s %s,%s",&Name,&Address,&City);
printf("\n your Name=%s",Name);
printf("\n your Address=%s",Address);
printf("\n your City=%s",City);
getch();
}
OR
#include<stdio.h>
#include<conio.h>
void main()
{
char Name[20]="kebede";
char Address[20]="showa";
char City[20]="sendafa";
clrscr();
printf("Name = kebede\n”);
printf(“Address = showa\n”);
printf(“City= sendafa\n);
getch();
}
The output is :- Name = kebede
Address = showa
City= sendafa
3. Write a C program to find the area of a circle using the formula: Area =PI * r2
#include<stdio.h>
#include<conio.h>
void main()
{
float Area,Radius;
const float pi=3.14;
clrscr();
printf("\n enter the Radius of circle");
scanf("%f",&Radius);
Area=pi*Radius*Radius;
printf("\n the Area of circle=%f",Area);
getch();
}

The output is :- if you enter the radius of a circle is 4 the output is 50.24 b/c
area=3.14x4x4=50.24

OR the output is the same


#include<stdio.h>
#include<conio.h>
#define pi 3.14
void main()
{
float Area,Radius;
clrscr();
printf("\n enter the Radius of circle");
scanf("%f",&Radius);
Area=pi*Radius*Radius;
printf("\n the Area of circle=%f",Area);
getch(); }
4. Write a C program to find the area and volume of sphere. Formulas are:

Area = 4*PI*R*R Volume = 4/3*PI*R*R*R

5. Write a C program to print the product of two numbers input from the user.
#include<stdio.h>
#include<conio.h>
void main()
{
int A;
int B;
int product;
clrscr();
printf("\n enter the two nubmer");
scanf("%d %d",&A,&B);
product=A*B;
printf("\n product=%d",product);
getch();
}
The output is :- if you enter two num that are 40 and 50 the output is 2000 b/c 40 x
50=2000
Lab 3
Per lab activity
1. Identify the output of the following codes; if there are errors try to correct them
a. int x,y=3,z=2;
x=y+z;
float k=9.0;
printf(« x/y=%d,x mod k=%d ,x*y=%d»,x/y,x%k,x*y) ;
printf( «( x==y)=%d,(x<y)=%d ,(x !=y)=%d\n»,x==y,x<y,x 
!=y)
Answer
#include <stdio.h>
#include <conio.h>
void main()
{
int x,y=3,z=2;
int k= 9.0;
clrscr ();
x=y+z;
printf(" \n x/y=%d \nx mod k=%d \n x*y=%d\n",x/y,x%k,x*y);
printf("\n(x==y)=%d \n(x<y)=%d \n(x!=y)=%d\n",x==y,x<y,x!=y);
getch ();
}
The output is x /y=1 (x==y)=0 false
X mod k = 5 (x<y) = 0 false
X * y= 15 (x!=y)=1 true
b. int x,y=3,z=2;
printf(« x=%d,y=%d\n »,x,y) ;
printf(« %d %d %d \n »,x&y,x|y,x^y) ;
x<<=2 ; y>>=2 ;
printf(« x=%d,y=%d ~z=%d\n »,x,y,~z) ;
printf(« (x==y)&&(y==z)=%d\n », (x==y)&&(y==z)) ;
printf(« (x==y)||(y==z)=%d\n », (x==y)&&(y==z)) ;
printf(« !(x==y)&&(y==z)=%d\n », !( (x==y)&&(y==z))) ;
Answer
#include<stdio.h>
#include<conio.h>
void main()
{
int x=5,y=3,z=2;
clrscr();
printf(" x=%d\n y=%d\n z=%d\n",x,y,z);
printf(" x&y=%d\n x|y=%d\n x^y=%d\n",x&y,x|y,x^y) ;
x<<=2 ;
y>>=2 ;
printf(" x=%d\n y=%d\n ~z=%d\n",x,y,~z) ;
printf(" (x==y)&&(y==z)=%d\n ", (x==y)&&(y==z)) ;
printf(" (x==y)||(y==z)=%d\n ", (x==y)||(y==z)) ;
printf(" !(x==y)&&(y==z)=%d\n ",!((x==y)&&(y==z)));
getch();
}
The output is:- X=5 X= 20
Y=3 Y= 0
Z=2 Z= -3
X&y=1 true (x==y)&&(y==z)=0 false
X|y=7 true (x==y)||(y==z)=0 false
X^y= 6 true !(x==y)&&(y==z)=1 true
Any non zero number is represent true
c. int x=6,y=2,z=2,k,a,b,c ;
k=x++;
a=++x+y--;
b=--a+k++;
c=((++a>b)&&(k<=y)
printf(“ %d %d %d %d %d %d\n”,x,y,k,a,b,c)
Answer
#include <stdio.h>
#include <conio.h>
void main()
{
int x=6,y=2,z=2,k,a,b,c;
k=x++;
a=++x+y--;
b=--a+k++;
c=((++a>b)&&(k<=y));
clrscr ();
printf("\nx=%d\ny=%d\nz=%d\nk=%d\na=%d\nb=%d\nc=
%d",x,y,z,k,a,b,c);
getch(); }
the output is :- x= 8 y=1 z=2 k=7 a=10 b=15 c=0.
d. Write an algorithm and draw a flow chart, and write a program to accept a
number and display its digits

#include <stdio.h>
#include <conio.h>
void main()
{
int a=(1,2,3,4,5,6);
clrscr ();
if (a==1)
printf("one");
if (a==2)
printf ("two");
if(a==3)
printf("three");
if(a==4)
printf("four");
printf(" One\n Two\n Three\n Four\n",&a);
getch();
}
The output :- One THREE
TWO FOUR
Lab 3
Lab activity
 Write a C program to read in a three digit number and produce the output as
given in the example
Example: assuming that the input is 347, the output will look like:
3 hundreds
4 tens
7 units)

#include <stdio.h>
#include <conio.h>
void main()
{
int n,a,b,c;
clrscr();
printf("enter the number\n");
scanf("%d",&n);
a=n/100;
b=(n%100)/10;
c=n%10;
printf("\n%d hundreds\n%d tens\n%d units",a,b,c);
getch();
}
The out put is if you enter 347 number
3 hundreds
4 tens
7 units
Lab 4
Operators /conditional , size of cost ,comma)
Per-lab activity.
Trace the output of the following code fragments:
1.
t a=9,b=2;
(a>b)?printf(“ a is greater than b”):printf(“ b is greater
than a”);
By using conditional operator
#include<stdio.h>
#include<conio.h>
void main()
{
int a=9,b=2;
clrscr();
(a>b)?
printf("a is greater than b"):
printf("b is greater than a");
getch ();
}
The output is:- a is greater than b.
By using if else statements
#include<stdio.h>
#include<conio.h>
void main()
{
int a=9,b=2;
clrscr();
if(a>b)
printf("a is greater than b");
else
printf("b is greater than a");
getch ();
}
The output is: - a is greater than b.
2. int a=1,b=2,m;
m=3*((a<b)?a++:b++)+2;
printf(“m=%d”,m);

#include <stdio.h>
#include<conio.h>
void main()
{
int a,b,m;
a=1;
b=2;
clrscr();
m=3*((a<b)?a++:b++)+2;
printf("\n m=%d",m);
getch();
}
The output is m= 5
3. int x;
long int y;
double z;
long double w;
printf(“%d%d%d%d”,sizeof(x),sizeof(y),sizeof(z),sizeof(w));
printf(«%d%d%d%d\n »,sizeof(char), sizeof(float),sizeof(long
int)) ;
printf(“ sum=%d\n”,(x=90,y=78,x+y));

#include<stdio.h>
#include<conio.h>
void main()
{
int x;
long int y;
double z;
long double w;
clrscr();
printf("\n%d \n%d \n%d \n%d",sizeof(x),sizeof(y),sizeof(z),sizeof(w));
printf("\n%d \n%d \n%d \n%d",sizeof(char),sizeof(float),sizeof(long
char),sizeof(long float));
printf("\nsum=%d",(x=90,y=78,x+y));
getch ();
}
The output is - int =2 -sizeof (char)=1
-long int = 4 -sizeof (float)=4
-double = 8 - sizeof(longchar)=1
-longdouble = 10 -size of (long float)=8
 sum =168 b/c x+y, 90+78=168
Lab activity

 Write a C program that accepts an integer and check whether it is even or odd
using conditional operator
#include <stdio.h>
#include <conio.h>
void main ()
{
int x;
clrscr ();
printf("\n enter the number");
scanf("\n %d",&x);
(x%2==0)?
printf("Even"):
printf("Odd");
getch ();
}
The output is if you enter even number output is even or if you enter odd number out put is
odd.
Using if else statement.
#include <stdio.h>
#include <conio.h>
void main ()
{
int x;
clrscr ();
printf(" enter the number \n");
scanf("%d",&x);
if(x%2==0)
printf("Even");
else
printf("Odd");
getch ();
}
Output is the same.
 Write a C program that reads three integers and display the largest , the
smallest( use conditional operator)
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c,largest,smallest;
clrscr();
printf("enter the number");
scanf("%d%d%d",&a,&b,&c);
largest = (a > b) ? ( ( a > c ) ? a | c) | ( (b > c) ? b | c);
smallest = (a < b) ? ( ( a < c ) ? a | c) | ( (b < c) ? b | c);
printf("\n largest=%d,\n smallest=%d\n",largest,smallest);
getch ();
}
The out put is if you enter 20,40,70 the output are largest =70
Smallest = 20
OR
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c,x,y;
int largest,smallest;
clrscr();
printf("enter the number");
scanf("%d%d%d",&a,&b,&c);
x=(a>b)?a|b;
largest = ( x > c ) ? x | c) ;
y=(a<c)? a|c;
smallest = (y < b) ?y|b;
printf("\n largest=%d,\n smallest=%d\n",largest,smallest);
getch ();
}
The out put is if you enter 100,50,90
Largest = 100
Smallest = 50

OR by using if else statement


#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c,largest,smallest;
clrscr();
printf("enter the number");
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
largest=a;
else if(b>a&&b>c)
largest=b;
else
largest=c;
if(a<b&&a<c)
smallest=a;
else if(b<a&&b<c)
smallest=b;
else
smallest=c;
printf("\n largest=%d,\n smallest=%d\n",largest,smallest);
getch ();
}
The output is if you enter 40, 30, 43
Largest =43
Smallest = 30
 Write a program to check the size in bytes of all C data types . Use the sizeof
operator.
#include <stdio.h>
#include <conio.h>
void main()
{
Char a;
Int b;
Longint c;
Float x;
Long float y;
Double z;
Long double w;
Clrscr();
Printf(“\n%d \n%d \n%d \n%d \n%d \n%d\n%d”,sizeof(a) ,sizeof(b)
,sizeof(z) ,sizeof(c) ,sizeof(x) ,sizeof(y) ,sizeof(w)
Getch();
}
The output is size of size of (a) = 1 – char
Size of (b) = 2- int
Size of (c) = 4 – long int
Size of (x) = 4 – float
Size of (y) = 8 – long float
Size of (z) = 8 – double
Size of (w) = 10 – long double
Lab 5
Decision making (if , if else and switch… case structure)
Lab activity exercise
1. Write a program which takes three sides a, b and c of a triangle input and
calculates its area if these conditions are satisfied: a+b>c, b+c>a, a+c>b
(Hint: area= s(s-a)(s-b)(s-c), where s=(a+b+c)/2)

#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c,s,Area;
clrscr ();
printf("Enter a sides of triangle \n");
scanf("%f %f %f",&a,&b,&c);
s=(a+b+c)/2;
if(a+b>c && b+c>a && a+b>b)
Area=s*(s-a)*(s-b)*(s-c);
printf("Area=%f",Area);
getch ();
}
The out put is if you enter the side of triangles are 6,5,4 out puts are 98.437500 b/c s =
(a+b+c)/2
S = (6+5+4)/2
S = 15/2 = 7.5
There for substitutes it Area = s x (s – a) x (s – b) x (s – c )
= 7.5 x (7.5 – 6) x (7.5 – 5) x (7.5 – 4)
= 98.437500
2. Write a program that inputs an integer – determine if it is even or odd

#include <stdio.h>
#include <conio.h>
void main ()
{
int x;
clrscr ();
printf(" enter the number \n");
scanf("%d",&x);
if(x%2==0)
printf("Even");
else
printf("Odd");
getch ();
}
The out put is if you enter even number it is even , or odd
OR
#include <stdio.h>
#include <conio.h>
void main ()
{
int x;
clrscr ();
printf("Enter the number\n");
scanf(" %d",&x);
(x%2==0)?
printf("Even"):
printf("Odd");
getch ();
}
The output is the same.
3. Write a program which takes a character input and checks whether it is vowel or
consonant

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char c;
clrscr();
printf ("enter a character \n");
scanf ("%d",&c);
c=getchar();
if(isalpha(c))
{
switch(toupper(c))
{
case'A':
case'E':
case'I':
case'O':
case'U': printf ("Vowel");
break;
default: printf ("Consonant");
}
}
else printf("not alphabet");
getch();
}
The out put is if you enter the vowel it is output is vowel or not constant
OR
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf ("enter a character \n");
scanf ("%c",&c);
if((c=='a')||(c=='A'))
printf("Vowel");
else if((c=='e')||(c=='E'))
printf("Vowel");
else if((c=='i')||(c=='I'))
printf("Vowel");
else if((c=='o')||(c=='O'))
printf("Vowel");
else if((c=='u')||(c=='U'))
printf("Vowel");
else printf("Consonant");
getch();
}
The out put is the same
4. Write a program that accepts the salary of an employ and display the net pay
based on the following rule

Salary Tax allowance


<=200 0 0
>200 and <=800 10% of salary 100
>800 and <=1400 20% 300
>1400 and<=2500 30% 500
>2500 35% 700

Net pay=salary+allowance-tax
#include<stdio.h>
#include<conio.h>
void main()
{
float salary,tax,allowance,netpay;
clrscr();
printf ("enter the salary \n");
scanf ("%f",&salary);
if(salary<=200)
{
tax=0;
allowance=0;
}
else if(salary>200 && salary<=800)
{
tax=salary*10/100;
allowance=100;
}
else if(salary>800 && salary<=1400)
{
tax=salary*20/100;
allowance=300;
}
else if(salary>1400 && salary<=2500)
{
tax=salary*30/100;
allowance=500;
}
else if(salary>2500)
{
tax=salary*35/100;
allowance=700;
}
netpay=salary+allowance-tax;
printf("netpay=%f",netpay);
getch();
}
The output is if you enter 2400salary output is 2180. b/c if (salary > 1400 && salary < = 2500)
Tax = salary x 30/100
Allowance = 500 and
Nat pay = salary + allowance – tax then
Tax = salary x 30/100
= 2400 x 30 / 100
Tax = 720 there for net pay = salary + akkowance – tax
= (2400+500) – 7200
2900 – 720 = 2180 net pay = 2180
5. Write a program for basic calculator that can perform addition, subtraction,
division, modulo division and multiplication (+,-,/,%,*).

#include<stdio.h>
#include<conio.h>
void main()
{
Int num1, num2;
Char choice;
Clrscr();
Printf(“enter the two number\n”);
Scanf(“%d%d”&nume1,&nume2);
Printf(“enter the operator\n”;
Scanf(“\n%c”,&choice);
Switch(choice)
{
Case’+’:
Printf(“the sum = %d \n”, num1 + num2);
Break;
Case’-‘;
Printf(“the defference = %d\n”, num1 – num2);
Break;
Case’/’:
Printf(“the division = %d \n”, num1 / num2);
Break;
Case’%‘;
Printf(“the modulo = %d\n”, num1 % num2);
Break;
Case’*’:
Printf(“the product= %d \n”, num1 x num2);
Break;
Default:
Printf(“wrong operator\n”);
Break;
}
Getch();
}
The output is if you enter 40 and 20 number and
Enter operator is ‘+’ output is 60 b/c 40 + 20 = 60
The same is true for the rest operators.
OR
#include<stdio.h>
#include<conio.h>
void main()
{
char op;
int a,b;
clrscr();
printf ("enter a and b\n");
scanf ("%d%d",&a,&b);
printf ("enter the operator");
scanf ("%s",&op);
if(op=='+')
printf("%d",a+b);
else if(op=='-')
printf("%d",a-b);
else if(op=='*')
printf("%d",a*b);
else if(op=='/')
printf("%d",a/b);
else if(op=='%')
printf("%d",a%b);
getch();
}
Lab6
Looping constructs in c language
Per lab activity
1. Revise how to find factorial of a number mathematically.
A. The factorial of 6
 6! => 6x5x4x3x2x1 = 720
B. A factorial of 5.
 5! => 5x4x3x2x1 = 120
2. Write down the out put of the following program statement
i. for (i=1; i<=10;i++)
printf(“%d \n”,i);
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for (i=1; i<=10;i++)
printf("%d \n",i);
getch();
}
Out put is :- 1,2,3,4,5,6,7,8,9,10
ii. int a = 10, b = 10;
for(inti=1;i<=a;i++)
{
a++;
b--;
printf(“a = %d,b=%d\t”,a,b);
}

#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=10,i;
clrscr();
for (i=1; i<=a;i++)
{
a++;
b--;
printf("a=%d b=%d\n",a,b);
}
getch();
}
The output is -: infinite in case of a++ & i++.a++ means
a=11,12,13,14----- and i++ means i=2,3,4,5---- then they have no
common intercept.
iii. int i=0;
while(i>10)
printf(“i is greater than 10”);
Answer
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
clrscr();
while(i>10)
printf("i is greater than 10");
getch();
}
The output is nothing display (o/p) b/c in the while statement if
the condition is false nothing should be execute.
iv. int y=0;
do{
printf(“i is greater than 10”);
}while(i>10);
Answer
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
clrscr();
do
{
printf("i is greater than 10");
}while(i>10);
getch();
}
Output is:-I is greater than 10.b/c in do while statement if the
condition is false at least one statement is execute or out put
or display.
v. for(int i=0;i<1;i--)
printf(“x”);
answer.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
clrscr();
for(i=0;i<1;i--)
{
printf("x");
}
getch();
}
Output is infinite x b/c the number that less than 1 means i—by
one is infinite.
OR
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
clrscr();
for(i=0;i<1;i++)
{
printf("x");
}
getch();
}
Output is x b/c if I++ by one means i=0, when i++ 0+1 = 1 and I =1 this is false for the
condition of i<1 b/c 1<1 is false then the 1st value of I is 0 so instead of 0 X is display or
o/p.
Lab – Activity
1. Write a program to generate a series of first 50 even number.
#include<stdio.h>
#include <conio.h>
void main()
{
int i=0;
clrscr ();
while(i<100)
{
i=i+2;
printf("\n %d",i);
}
getch ();
}
Output are:- 2,4,6,8,10,12,14-----100 b/c they are the 1 st 50 even numbers.
2. Write a program to generate the sum of the first 100 whole numbers.
#include<stdio.h>
#include <conio.h>
void main()
{
int i=0,sum=0;
clrscr ();
while(i<=100)
{
sum=sum+i;
i++;
}
printf("sum= %d\n",sum);
getch ();
}
Output: - 5050 b/c the sum of the 1st 100 whole numbers that 0,1,2,3,4----100.
3. Write a program to enter the numbers till the user wants and at the end it should display
the count of positive, negative and zeros entered.

#include<stdio.h>
#include <conio.h>
void main()
{
int a;
clrscr ();
printf("enter the number");
scanf("\n%d",&a);
if (a>0)
printf("positive number");
else if (a<0)
printf("Negative number");
else
printf("zero");
getch ();
}

Output:- if you enter a positive number out put is positive number.


If you enter a negative number out put is negative number else output is zero.
OR
#include<stdio.h>
#include<conio.h>
void main()
{
int Mark;
char Grade;
clrscr();
printf("Enter the mark of the student\n");
scanf("%d",&Mark);
if(Mark>=85)
printf("Grade=A");
else if(Mark>=75)
printf("Grade=B");
else if(Mark>=65)
printf("Grade=C");
else if(Mark>=50)
printf("Grade=D");
else printf("Grade=F");
getch();
}
Output is :- ig you enter the student mark is 86 your o/p is Grade = A.
:- if you enter the student mark is 78 your o/p is Grade = B.
4. Write a C program to find factorial of accepted integer.
#include<stdio.h>
#include <conio.h>
void main()
{
int n,fact=1;
clrscr ();
printf("enter the number");
scanf("\n%d",&n);
while (n>0)
{
fact=fact*n;
n--;
}
printf("fact=%d",fact);
getch ();
}
Output :-if you enter 3 factorialof 3 is 3! = 3x2x1 = 6
If you enter 4 4 ! = 4x3x2x1=24
By using for loop.
#include<stdio.h>
#include <conio.h>
void main()
{
int n,i,fact=1;
clrscr ();
printf("enter the number \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("fact=%d",fact);
getch ();
}
OR
#include<stdio.h>
#include <conio.h>
void main()
{
int n,i,fact=1;
clrscr ();
printf("enter the number \n");
scanf("%d",&n);
for(i=n;i>0;i--)
{
fact=fact*i;
}
printf("fact=%d",fact);
getch ();
}
The output of both of them
If you enter 4 or 5 or6 number their factorial numbers are:-
4! 4x3x2x1 = 24
5! 5x4x3x2x1 = 120
6! 6x5x4x3x2x1 = 720
5. Write a C program to find the sum of digits of accepted integer.
#include<stdio.h>
#include<conio.h>
SumOfDig(int n);
void main()
{
int n,R;
clrscr();
printf("enter the digit \n");
scanf("%d",&n);
R=SumOfDig(n);
printf("sum of digit=%d",R);

getche();
}
SumOfDig(int n)
{
int x,s=0;
while(n>0)
{
x=n%10;
s=s+x;
n=n/10;
}
return s;
}
OR
#include<stdio.h>
#include<conio.h>
void main()
{
int n,x,s=0;
clrscr();
printf("enter the digit \n");
scanf("%d",&n);
while(n>0)
{
x=n%10;
s=s+x;
n=n/10;
}
printf("sum of digit=%d",s);
getche();
}
The out puts of both of them one:-
If you enter the 48 o/p is 12 b/c the fourmula of x = n/10 and s=s+x means :-
x = n%10, x = 48/10 x =8 the value of x is remainder n=n/10 n= 48/10 n=4.8 the value of n=4 b/c
it dropped that comes after a point digit then, s = s+x, s= 4+8 s = 12
6. Write a program in C that will scan a number N and then output the sum of the powers
from 1 to N. thus, if the input is 4, the output should be 30 because 1+4+9+16=30
[1,2,3,4]
#include<stdio.h>
#include <conio.h>
void main()
{
int sum=0,n,i;
clrscr ();
printf("enter the number \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i*i;
printf("The square=%d\n",i*i);
}
printf("The sum of their square/power/=%d\n",sum);
getch ();
}
Out put is :- if you enter 5 it reads as 1- 5 or 1 up to 5 then their squares are
The square of 1 = 1
The square of 2 = 4
“ “ “ 3=9
“ “ “ 4 = 16
“ “ “ 5 = 25
The sum of their square /power/ = 55
Post lab activity
1. Write a program which Prints the following pattern up to 10 lines

0
111
22222
3333333
#include<stdio.h>
#include <conio.h>
void main(void)
{
int i,j,k=0;
clrscr();
for(i=0;i<10;i++)
{
for(j=0;j<=k;j++)
{
printf("%d",i);
}
printf("\n");
k+=2;
}
getch ();
}
Out put is
0
111
22222
3333333
-
-
9999999999999999999
It prints up to 10 line.
2. Write a program to produce the following output:

A * B 1
** 12
*** 123
**** 1234
***** 12345

#include<stdio.h>
#include<stdio.h> #include <conio.h>
#include <conio.h> void main()
void main() {
{ int i,j,row;
int i,j,row; clrscr();
clrscr(); printf("enter the number of row \n");
printf("enter the number of row \n"); scanf("%d",&row);
scanf("%d",&row); for(i=1;i<=row;i++) {
for(i=1;i<=row;i++) for(j=1;j<=i;j++) {
{ printf("%2d",j); }
for(j=1;j<=i;j++) printf("\n");
{ }
printf("*"); getch ();
} }
printf("\n");
}
getch ();
}

c.
* 1
** 22
*** 333
**** 4444
***** 55555

#include<stdio.h> #include<stdio.h>
#include <conio.h> #include <conio.h>
void main(void) void main(void)
{ {
int i,j,k,row; int i,j,k,row;
clrscr(); clrscr();
printf("enter the row \n"); printf("enter the row \n");
scanf("%d",&row); scanf("%d",&row);
for(i=1;i<=row;i++) for(i=1;i<=row;i++)
{ {
for(k=1;k<=row-i;k++) for(k=1;k<=row-i;k++)
printf(" "); printf(" ");
for(j=1;j<=i;j++) for(j=1;j<=i;j++)
{ {
printf("*"); printf("*");
} }
printf("\n"); printf("\n");
} }
} }
getch (); getch ();
} }
3. Write a program that accepts a number and check whether it is a prime number or not.
#include<stdio.h>
#include <conio.h>
void main(void)
{
int n,j,i,f=1;
clrscr();
printf("enter the number \n");
scanf("%d",&n);
printf("2\n");
for(i=3;i<n;i++){
f=1;
for(j=2;j<i;j++){
if(i%j==0) {
f=0;
break; } }
if(f==1)
printf("%d\n",i); }
getch();
}
Out put :- if you enter 20 your out puts are the prime number that found up to 20 means
2,3,5,7,11,13,17,19
If you enter 100 your o/p are a prime number that found up to 100 means
2,3,5,7,11,13,17,19----97 and so on.
OR
#include<stdio.h>
#include <conio.h>
int prime(int n);
void main(void)
{
int n,f;
clrscr();
printf("enter the number \n");
scanf("%d",&n);
f=prime(n);
if(f==0)
printf("The number is not prime",n);
else printf("The number is prime");
getch();
}
int prime(int n)
{
int f=1,i;
for(i=2;i<n;i++)
{
if(n%i==0)
{
f=0;
break;
}
}
return f;
}
Output if you enter prime number your output is prime number. If you enter not prime your
o/p is the number is not prime.
OR
#include<stdio.h>
#include <conio.h>
void main(void)
{
int n,i,k=0;
clrscr();
printf("enter the number \n");
scanf("%d",&n);for(i=2;i<n;i++)
if(n%i==0)
k++;
if(k==0)
printf("The number is prime");
else if(k>0)
printf ("The number is not prime");
getch();
}
Output if you enter prime number your output is prime number. If you enter not prime your
o/p is the number is not prime.
4. Write a program to generate multiplication table
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\n\t======================");
printf("\n\tMultiplication table");
printf("\n\t======================\n");
for(int i=1;i<=12;i++)
{
for(int j=1;j<=12;j++)
{
printf("%4d",i*j);
}
printf("\n");
}
getche();
}
Out puts are
Multiplication table
1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 36
4 8 12 16 20 24 28 32 36 40 44 48
5 10 15 20 25 30 35 40 45 50 55 60
6 12 18 24 30 36 42 48 54 60 66 72
7 14 21 28 35 42 49 56 63 70 77 84
8 16 24 32 40 48 56 64 72 80 88 96
9 18 27 36 45 54 63 72 81 90 99 108
10 20 30 40 50 60 70 80 90 100 110 120
11 22 33 44 55 66 77 88 99 110 121 132
12 24 36 48 60 72 84 96 108 120 132 144

OR
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=1;i<=10;i++)
{
for(int j=1;j<=10;j++)
{
printf("%4d",i*j);
}
printf("\n");
}
getche();
}
The out put will be the same.
Computer programming
1. Program to demonstrate sum using for loop
#include<stdio.h>
#include <conio.h>
void main()
{
int limit,i,sum=0;
clrscr();
printf("enter the limit:\n");
scanf("%d",&limit);
for(i=1;i<=limit;i++)
{
sum=sum+i;
}
printf("The sum=%d \n",sum);
getch ();
}
Output if you enter 5 your out put is 15 b/c the limited number is 5 then add it the bumber that
1-5 (1 up to 5 1+2+3+4+4=15).
2. Program to demonstrate multiplication table
#include<stdio.h>
#include <conio.h>
void main(void)
{
int i,j,col,row;
clrscr();
printf("\n enter the column and row:");
scanf("\n%d \n%d",&col,&row);
for(i=1;i<=row;i++) {
for(j=1;j<=col;j++) {
printf("%5d",i*j); }
printf("\n"); }
getch ();
}
Output if you enter column = 4 and row = 4your output is :-
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
3. program to print the following pattern using for loop.
*
* *
* * *
* * * *
* * * * *
#include<stdio.h>
#include <conio.h>
void main(void)
{
int i,j,row;
clrscr();
printf("enter the row \n");
scanf("%d",&row);
for(i=1;i<=row;i++) {
for(j=1;j<=i;j++) {
printf("*"); }
printf("\n"); }
getch ();
}
Output :- if you enter row is 5 your output is the above stars that on no3
4. program to print the following pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<stdio.h>
#include <conio.h>
void main(void)
{
int i,j,row;
clrscr();
printf("enter the row \n");
scanf("%d",&row);
for(i=1;i<=row;i++) {
for(j=1;j<=i;j++) {
printf("%d",j); }
printf("\n"); }
getch ();
}
Output :- if you enter row is 5 your output is the above stars that on no4
5. Program to print the following pattern.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<stdio.h>
#include <conio.h>
void main(void)
{
int i,j,k,row;
clrscr();
printf("enter the row \n");
scanf("%d",&row);
for(i=1;i<=row;i++)
{
for(k=1;k<=row-i;k++)
printf(" ");
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch ();
}
Output :- if you enter row is 5 your output is the above number that on no 5.
6. Program to print the following pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include<stdio.h>
#include <conio.h>
void main(void)
{
int i,j,k,row;
clrscr();
printf("\n enter the row:");
scanf("%d",&row);
for(i=1;i<=row;i++)
{
for(k=1;k<=row-i;k++)
printf(" ");
for(j=1;j<=i;j++)
{
printf("%2d",j);
}
printf("\n");
}
getch ();
}
Output :- if you enter row is 5 your output is the above number that on no 5.
Lab 7
Functions
Lab Activity
1. Write a program that reads two strings and perform the following operations
a. Display the longest string.

#include <stdio.h>
#include <conio.h>
#include <string.h>
Void main (void)
{
char s1 [9]={"decfer"};
char s2[4]={"Dec"};
char s3[20];
int l1,l2,longest;
clrscr();
l1=strlen(s1);
l2=strlen(s2);
printf("the first string is %s\n",s1);
printf("the length of first string is %d\n",l1);
printf("the second string is %s\n",s2);
printf("the length of second string is %d\n",l2);
if(l1>l2)
printf("the longest string");
else
printf("the shortest string");
getche();
}
Output
-the first string = decter
-the length of first string = 6
-the second string = dec
-the length of second string = 3
* the longest stringest . b/c it compares the length of their strings and prints the longest string
b/c 6>3
OR
#include<stdio.h>
#include <conio.h>
#include <string.h>
void main(void)
{
char s1[9]={"decfer"};
char s2[4]={"Dec"};
char s3[20];
int l1,l2,longest;
clrscr();
l1=strlen(s1);
l2=strlen(s2);
if(l1>l2)
printf("the longest string");
else if(l2>l1)
printf("the longest string");
getche();
}
Output is the longest string.
b. swap the two strings
#include<stdio.h>
#include<conio.h>
void main()
{
char *s1,*s2,*temp;
s1="Abebe";
s2="Kebede";
printf("before swap s1=%s and s2= %s\n",s1,s2);
temp=s1;
s1=s2;
s2=temp;
printf("after swap s1=%s and s2= %s\n",s1,s2);
getch(); }
outputs are:-
- before swap s1 = abebe and s2 = kebede
- after swap s1 = kebede and s2 = abebe
c. Concatenate them to a third string

Output: - s3 = dec engineering


d. Check if they are the same.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(void)
{
char s1[10]={"Defense"};
char s2[10]={"colleg"};
char s3[20];
int l1,l2,longest;
clrscr();
l1=strlen(s1);
l2=strlen(s2);
if(l1==l2)
printf("they are the same");
else
printf("they are not the same");
getche();
}
Output is :- they are the same b/c the length their strings are equal
Defense = = College -> 7=7
But
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(void)
{
char s1[10]={"Defense"};
char s2[10]={"colleg"};
char s3[20];
int l1,l2;
clrscr();
l1=strlen(s1);
l2=strlen(s2);
if(l1==l2)
printf("they are the same");
else
printf("they are not the same");
getche();
}
Output is: - they are not the same b/c the length of their strings are not equal
Defense > college -> Defense = 7 7 colleg = 6 then 7>6
Compare two strings.
2. Write a program that accepts a floating point number and display its floor and ceil.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a=45.78;
clrscr ();
printf(" ceil=%.2f",ceil(a));
printf(" floor=%.2f",floor(a));
getch();
}
Output are :- cell = 46.00 -> rounded after
Floor = 45.00 -> dropped after
3. Write a program that reads a character and check whether it is a digit , an alphabet or any
other character.

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char c;
clrscr();
printf("enter the character\n");
c=getchar();
printf("check=%d\n",isalpha(c));
getch();
}
Output:- if you enter the character is Alem or kebed--- check = 8 it means true b/c any none
zero number is true.
If you enter 10 or 9 or 8--- check =0 it means false b/c they are not a character.

Lab -8
Function
Pre lab activity
1. Write the output of the following program

#include<stdio.h>
#include<conio.h>

void manipulate(int x, int *y, int z)


{
x=*y+z ;
*y=z+10 ;
z=90 ;
printf(« x=%d y=%d z=%d\n »,x,*y,z) ;
}

void main()
{
int a=9,b=7,c=8 ;
printf(« a=%d b=%d c=%d\n »,a,.b,c);
manipulate(a,&b,c);
printf(« a=%d b=%d c=%d\n »,a,.b,c);
getch();
}
Output :- a=9 b=7 c=8
X =15 y = 18 z = 90
a=9 b = 18 c=8
lab activity
1. Write a program using a function that accepts two integers and display their sum.
//no giving no returning
#include<stdio.h>
#include<conio.h>
void sum(void); //prototype
int x,y; // globla varibles
void main ()
{
clrscr ();
printf("\n enter x,y");
scanf("%d%d",&x,&y);
sum ();
}
void sum(void) // function definition
{
int z; // local variable
z=x+y;
printf("\n sum=%d",z);
getch ();
}
Out put :- if you enter x and y are 40 & 50 your out put is 90 b/c 40 + 50 = 90.
2. Write a program using function that accepts two integers arguments and exchange their
values.

#include<stdio.h>
#include<conio.h>
void swap(int x, int y);
void main()
{
int x=12,y=20;
swap(x,y);
clrscr ();
printf("\nmain:nafter swap \nx=%d \ny=%d",x,y);
}
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("\nswap:after swap \nx=%d \ny=%d",x,y);
getch ();
}
Output :- Before swap x = 12 y = 20
After swap x = 20 y = 12
3. Write a program using function to accept a string and change its content to uppercase
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *c;
char *r;
clrscr();
printf("enter the string\n");
gets(c);
r=strupr(c);
printf("the upper case string is:%s\n",r);
getch();
}
Output :- if you enter computer in lower case string your out put is COMPUTER in upper case
string.
- If you enter a in the lower case strings yoiur o/p is A in upper case string.
4. Write a program using a recursive function DigitSUM() that accepts a number and
returns the sum of its digits .
(eg. If number is 234, function should return 9)
#include<stdio.h>
#include<conio.h>
Int digitsum(int x)
{
Int sum = 0
If (x<10)
Return x;
Else
Sum =x%10+(digitsum(x/10));
Return sum;
}
Void main()
Int n;
Clrscr();
Printf(“enter the digit \n”;
Scanf(“%d”,&n);
Printf(“digit sum =%d”,digit sum);
Getch();
}
Output :- if you enter 2678 your o/p is 23 b/c it adds 2+6+7+8 = 23
:- if you enter 436 your o/p is 13 b/c it adds 4+3+6 = 13
oR
#include<stdio.h>
#include<conio.h>
Void main ()
{
Int n,x,s=0;
Clrscr();printf(“enter the digit \n”);
Scanf(“%d”,&n);
While(n>0)
{
X=n%10;
S=s+x;
N=n/10;
}
Printf(“sum of digit=%d”,s);
Getch();
}
Output:- if you enter 347 your o/p is 14 b/c it added 3+4+7=14.
Lab – 9
Arrays in c /one dimensional)
Per lab activity
1. Write a program that takes input for array int a[5] and print the values to the screen.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={10,12,14,16,18};
clrscr();
for(int i=0;i<5;i++)
{
printf("%d\t",a[i]);
}
getch();
}
Output:- 10 12 14 16 18 b/c it display the declared number of the above
2. Write a program that takes input for array int a[5] and array int b[5] and exchanges their
values.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={10,12,14,16,18};
int b[5]={11,13,15,17,19};
int temp;
clrscr();
for(int i=0;i<5;i++)
{
printf("\n before exchange their values a[i]= %d and b[i]= %d",a[i],b[i]);
temp=a[i];
a[i]=b[i];
b[i]=temp;
printf("\n after exchange their values a[i]= %d and b[i]= %d",a[i],b[i]);
}
getch();
}
Output :- before exchange their values a[i]=10 and b[i]=11
:- after exchange their values a[i]=11 and b[i]=10
:-before exchange their values a[i]=12 and b[i]=13
:- after exchange their values a[i]=13 and b[i]=12
:- before exchange their values a[i]=14 and b[i]=15
:- after exchange their values a[i]=15 and b[i]=14
:-before exchange their values a[i]=16 and b[i]=17
:- after exchange their values a[i]=17 and b[i]=16
:-before exchange their values a[i]=18 and b[i]=19
:- after exchange their values a[i]=19 and b[i]=18
3. Write a program that takes 10 integers as input and prints the largest integer and its
location in the array.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
int a[10]={20,22,24,26,28,30,32,34,36,38};
int i,a,largest;
clrscr();
for(i=0;i<10;i++)
largest=a[0];
for(i=1;i<10;i++)
if(largest<a[i])
{
largest=a[i];
a=i;
}
printf("address=a[%d]\nlargest=%d\n",a,largest);
getch();
}
Output is :- location (address)= a[9]
:- largest 38
It means the location (address) of 38 is a[9].
OR
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
int a[10]
int i,a,largest;
clrscr();
printf("enter the array element\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
largest=a[0];
for(i=1;i<10;i++)
if(largest<a[i])
{
largest=a[i];
a=i;
}
printf("address=a[%d]\nlargest=%d\n",a,largest);
getch();
}
Output are :- if you enter the array elements are 20,22,24,26,28,30,32,34,36,38
Your output are :- location = a[9]
:- largest = 38
It means the location (address) of the largest 38 is a[9].
Their location (address) are
A[0] = 20 a[5] = 30
A[1] = 22 a[6] = 32
A[2] = 24 a[7] = 34
A[3] = 26 a[8] = 36
A[4] =28 a[9] =38

4. Write a program that adds up two 4x4 arrays and stores the sum in third array.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
int a[4],b[4],sum[4];
int i;
clrscr();
printf("enter the array element a\n");
for(i=0;i<4;i++)
scanf("%d",&a[i]);
printf("enter the array element b\n");
for(i=0;i<4;i++)
scanf("%d",&b[i]);
for(i=0;i<4;i++)
{
sum[i]=a[i]+b[i];
printf("%3d",sum[i]);
}
getch();
}
Outputs are;-
If you enter the array elements of a are 20,23,31,40and
If you enter the array elements of b are 70,27,32,180
Your outputs are 90 , 50 , 63 , 220 b/cthe sum of two array a[i] + b[i]orderly 20+70 = 90 ,
23+27=50 , 31+32=63 and 40+180=220.
LAB ACTIVITY.

1. Write a program which takes names of five countries as input and prints them in
alphabetical order.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,n;
char str[20][20];
char temp[20];
clrscr();
puts("how many countries do want to enter? \n");
scanf("%d",&n);
puts("enter the name of these countries\n");
for(i=0;i<=n;i++)
gets(str[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("The sorted string\n");
for(i=0;i<=n;i++)
puts(str[i]);
getch();
}

Output:- if you enter the number of countries are 5


Enter the name of those countries are:-1 keniya 2. Eygpt 3. Sudan

4. Ethiopia 5. Somalia.

Finally display sorted strings are

1. Ethiopia 2. Eygpt 3. Keniya 4. Somalia 5. Sudan.

2. Write a program that reads two strings and compare for equality. (do not use any built in function
strcmp()).

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(void)
{
char s1[10]={"Kebede"};
char s2[10]={"Getacher"};
char s3[20];
clrscr();
printf("%d",strcmp(s1,s2));
printf("%s",s3);
getche();
}
Output is:- 4 b/c it compares the ASCII numerical code of kebede 1 st letter k=75 and the ASCII numerical
code of getacher 1st letter G=71 then :- form K-G => 75 -71 = 4.

3. Write a program which takes a string as input and counts total number of vowels in the string.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
int count=0;
int i,j;
char *x;
clrscr();
printf("enter the string\n");
scanf("%s",x);
for(i=0;x[i]!='\0';i++)
{
if(x[i]=='a'|| x[i]=='e'|| x[i]=='i'|| x[i]=='o'|| x[i]=='u')
count=count+1;
}
printf("all the total counts of vowel is %d\n",count);
getch ();
}
Outputs :-
If you enter the strings are a b c o d I u.
Your outputs are all the total counts of vowel is 4 b/c in the strings a b c o d I u the vowels are a,o,I,u =4
- if you enter the strings are alemayehu.
* Your o/p are all the total counts of vowel is 5 b/c in the strings alemayehu the vowels are a,e,a,e,u = 5
- If you enter the strings are a e I o u
* Your o/p is all the total counts of vowel is 5 b/c all of the strings a,e,I,o,u are vowels.
LAB – 10
POINTER
PER LAB ACTIVITY
B. int a;
int b;
int* p,*q;
p = &a;
*p = 4;
*q=&b
*q=*p+10;
p = &b;
*p = 3;
printf(“%d %d %d %d\n”,a,b,*p,*q);
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;
int *p,*q;
clrscr();
p = &a;
*p = 4;
q=&b;
*q=*p+10;
p = &b;
*p = 3;
printf("a=%d\nb=%d\n*p=%d\n*q=%d\n",a,b,*p,*q);
getch ();
}
Output is a=4 - *p=3
b=3 - *q=3
c.int a,b,*p,**q;
a=90;
p=&a;
q=&(&a);
printf(“%d %d %d %u %u\n”,*p,a,*(&a),p,q) ;
ans
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*p,**q;
clrscr();
a=90;
p=&a;
q=&p;
printf("*p=%d\na=%d\n*(&a)=%d\np=%u\nq=%u\n",*p,a,*(&a),p,q);
getch();
}
Output is :- *p=90 - *(&a)=90
a=90 -p=65476
- q=65474
 K “p” 65476 “ K “q”,65474 ¢Uú¨<}\ u^c< ’c< ¾Á²<ƒ” Addres e׆¨<::

variable adresse
a 5000
p 3000
q 2900

d. long int a,*p=&a ;//&a=900


int i;
for(i=0;i<10;i++)
{
Printf(“%u \n”,p);
P++;
}
Ans
#include<stdio.h>
#include<conio.h>
void main()
{
long int a,*p=&a;//&a=900
int i;
clrscr();
for(i=0;i<10;i++)
{
printf("%u \n",p);
p++;
}
getch();
}
Output is
- 65490 65506
- 65494 - 66510
- 65498 - 65514
- 65502 - 65518
- 65522
- 65526
- There are 10 computer address displayed in by 4 byte 4/ce b/cour
data type is long int then the long int data type size is 4 bytes.
E.
char c=’Y’;
char *ptr =&c;
printf(“%c %c\n”,c,*ptr);
c=’P’;
printf(“%c %c\n”,c,*ptr);
*ptr=’1’;
printf(“%c %c\n”,c,*ptr);
#include<stdio.h>
#include<conio.h>
void main()
{
char c='y';
char *ptr=&c;
clrscr();
printf("%c%c\n",c,*ptr);
c='p';
printf("%c%c\n",c,*ptr);
*ptr='1';
printf("%c%c\n",c,*ptr);
getch();
}

Output:- * y y

*p p

*11

f. Int a[10]={987};
int *p,i;
p=a;
for(i=0;i<10;i++)
{
Printf(“%d\n”,*p);
P++;
}
Printf(“%d “,p-a);
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10]={9,8,7};
int i,*p;
clrscr();
p=a;
for(i=0;i<10;i++)
{
printf("%d\n",*p);
p++;
}
printf("%d",p-a);
getch();
}
Output:-
9 *0 *0
8 *0 *0
7 *0 *10
0 *0
Note-> the 7 zeros are the remains that becomes after 9,8,7 elements
and zeros make i<10 is true then print zeros.10 is the result of p-a.
Lab – activity
2. Write a program that reads 10 elements of the arry and prints the
same using pointer to the array.

LAB-11
STRUCTURE
Pre Lab activity
1. Program to initialize structure
variables,assigning values and accessing members of
tructures
#include<stdio.h>
#include<conio.h>
struct sample //definition of a structure
{
int x;
float y;
}; //structure definition should end with semi colon
void main()
{
struct sample m(90,98.8); //initialization of structure variable
clrscr();
printf(“x=%d y=%f\n”,m.x,m.y);//accessing members of a structure
m.x=67; //assigning values to structure members
m.y=10;
printf(“x=%d y=%f\n”,m.x,m.y);
getch();
}
Answer
#include<stdio.h>
#include<conio.h>
struct sample //definition of a structure
{
int x;
float y;
}; //structure definition should end with semi colon
void main()
{
struct sample m;
clrscr();
m.x=90;
m.y=98.8;
printf("x=%d\ny=%0.2f\n",m.x,m.y);//accessing members of a
structure
m.x=67; //assigning values to structure members
m.y=10;
printf("x=%d\ny=%0.2f\n",m.x,m.y);
getch();
}
Output:-

2. Write a program that reads details of a student and display the values to the screen
using a structure. The structure should have the items name, idno, sex, age, class_year
and nationality as members.

#include<stdio.h>
#include<conio.h>
struct student
{
char name[20],idno[20];
char sex;
int age,classYear;
char nationality[20];
}s;
void main()
{
printf(“enter name,idno,nationality,sex,age,classyear of a
student\n”);
scanf(“%s%s%s”,s.name,s.idno,s.nationality);
s.sex=getche();
scanf(“%d%d”,&s.age,&s.classYear);
printf(“name=%s\nidno=%s nationality=%s\n”,
s.name,s.idno,s.nationality);
printf(“sex=%c\nage=%d\nclassyear=%d\n”,s.sex,s.age,s.classYear);
getch();
}
Answer
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
char idno[20];
char nationality[20];
char sex;
int age,classyear;
}
s;
void main()
{
clrscr() ;
printf("enter name\nidno\nnationality\nsex\nage\nclassyear of a
student\n");
scanf("%s%s%s%s",&s.name,&s.idno,&s.nationality,&s.sex);
//s.sex=getche();
scanf("%d%d",&s.age,&s.classyear);
printf("name=%s\n idno=%s\n nationality=%s\n",
s.name,s.idno,s.nationality);
printf("sex=%c\n age=%d\n classyear=
%d\n",s.sex,s.age,s.classyear);
getch();
}
OR
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
char idno[20];
char nationality[20];
char sex;
int age,classyear;
}
s;
void main()
{
clrscr() ;
printf("enter name\nidno\nnationality\nsex\nage\nclassyear of a
student\n");
scanf("%s%s%s%s",&s.name,&s.idno,&s.nationality,&s.sex);
//s.sex=getche();
scanf("%d%d",&s.age,&s.classyear);
printf("name=%s\n idno=%s\n nationality=%s\n",
s.name,s.idno,s.nationality);
printf("sex=%c\n age=%d\n classyear=
%d\n",s.sex,s.age,s.classyear);
getch();
}
The output of both of them are
If you enter
Kebede M
102 28
Ethiopian 2006
Then your put puts are:-
Name = kebede
Idno = 102
Nationality = thiopian
Sex = M
Age = 28
Class year =2006

3. Program to demonstrate a structure as member of another structure

#include<stdio.h>
#include<conio.h>
struct date{
int day;
int month;
int year;
};

struct sudent
{
char name[10];
long int rollno;
struct date dob;
};
struct student a;
void main()
{
a.name[]=”sample”;
a.rollno=9000;
a.dob.day=7;
d.dob.month=9;
d.dob.year=2012;
printf(“ %s \n%ld\n”,a.name,a.rollno);
printf(“DOB=%d-%d-%d\n”,a.day,a.month,a.year);
getch();
}
Answer
#include<stdio.h>
#include<conio.h>
struct date
{
int day;
int month;
int year;
};
struct student
{
char *name;
long int rollno;
struct date dob;
};
struct student a;
void main()
{
clrscr();
a.name="Kebede";
a.rollno=9000;
a.dob.day=7;
a.dob.month=9;
a.dob.year=2012;
printf("a.name= %s\na.rollno=%d\n",a.name,a.rollno);
printf("DOB=%d/%d/%d",a.dob.day,a.dob.month,a.dob.year);
getch();
}
The Outputs are:-
a.name = kebede
a.rollno = 9000
Dob = 7/9/2012

You might also like