Final Lab Report
Final Lab Report
INSTITUTE OF ENGINEERING
PULCHWOK CAMPUS
Subject: C-Programming
Date: 2078/05/20
2) Define Compiler.
Compiler, computer software that translates (compiles) source code written in a high -level
language (e.g., C++) into a set of machine-language instructions that can be understood by a
digital computer's CPU. A compiler takes the entire program in one go. i.e. it compiles the
whole program at a single time. Also, it generates an intermediate machine code.
3) Define Interpreter.
An interpreter is a program that executes instructions written in a high-level
language. Interpreters enable other programs to run on a computer or server. They process
program code at run time, checking the code for errors line by line.
4) Getting used to in C.
For getting used to in c, we must have clear understanding of variables, operators, operands
and compiler.
A variable is a symbol or letter, such as "x" or "y," that represents a value. The variable
types in c are:-
integer (int) : The most natural size of integer for the machine.
Real number(float) : A single-precision floating point value.
double: A double-precision floating point value.
char: Typically a single octet(one byte). It is an integer type.
Long integer(Long int): Used for writing Long Integer.
Unsigned integer(unsigned int): Used for taking modulus of a number.
Void: Represents the absence of type.
Lab Report: 1
Q. NO. 1)
Source Code:
#include<stdio.h>
#include<conio.h>
void main(){
printf("This is my first c program");
getch();
}
Q. NO. 2)
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int s,a,b,c=20; /*VARIABLE DECLARATION*/
printf ("Enter value of a"); /*TO DISPLAY MESSAGE ON SCREEN*/
scanf ("%d",&a); /*TO GIVE VALUE OF A*/
printf ("Enter value of b"); /*TO DISPLAY MESSAGE ON
SCREEN*/ scanf ("%d",&b); /*TO GIVE VALUE OF B*/
s=a+b+c; /*PROCESSING*/
printf ("sum=%d",s); /*TO DISPLAY VALUE STORED AS S*/
getch(); /*TO MAKE PROGRAM WAIT UNTIL USER ENTERS ANY CHARACTER*/
}
Output: The output was the sum of the numbers entered by the user.
Q. NO. 3)
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int s,a,b;
float p;
printf("address of s is %x",&s);
printf("\n address of a is %x",&a);
printf("\n\n occupied number of bytes by variable s is %d",sizeof(s));
printf("\n\n\n Size of a is %d",sizeof(1.5));
printf ("\n\n\n\n Size of float data type is %d",sizeof(float));
getch();
}
Output:
address of s is 61ff1c
address of a is 61ff18
Size of a is 8
Source Code:
/*WAP TO CALCULATE CIRCUMFERENCE AND AREA OF THE CIRCLE OF RADIUS r*/
#include<stdio.h>
#include<conio.h>
void main()
{
float r,pi,p,a;
pi=3.14159;
printf("Enter value of Radius");
scanf("%f",&r);
p=2*pi*r;
a=pi*r*r;
printf ("The Circumference of circle=%f and the area of the circle=%f",p,a);
getch();
}
Output: The area and circumference of the circle was calculated based on the radius of
circle given by the user.
Q. NO. 5)
Source Code:
/*WAP TO CALCULATE VOLUME OF SPHERE OF RADIUS r*/
#include<stdio.h>
#include<conio.h>
void main()
{
float r,pi,v;
pi=3.14159;
printf("Enter value of Radius");
scanf("%f",&r);
v=(4/3)*pi*r*r*r;
printf ("The volume of the sphere=%f",v);
getch();
}
Output: The Volume of the sphere was calculated based on the radius of circle given
by the user.
Q. NO. 6)
Source Code:
/*WAP TO CALCULATE SIMPLE INTREST*/
#include<stdio.h>
#include<conio.h>
void main(){
float p,t,r,si;
printf("Enter value of Principal, Time And Rate");
scanf("%f%f%f",&p,&t,&r);
si=(p*t*r)/100;
printf ("simple intrest=%f",si);
getch();
}
Output: Simple intrest was calculated based on the values of principal, rate and time
given by user.
Q. NO. 7)
Source Code:
/*WAP TO READ VALUES OF x AND y FROM USER AND EVALUATE THE EXPRESSION v=x2+y2-
100/x*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x,y,v;
printf("Enter values of x and y");
scanf("%f%f",&x,&y);
v=pow(x,2)+pow(y,2)-100/x;
printf ("\n The value of v=%f",v);
getch();
}
Output: The value of v was evaluated based on the values of x and y given by the user.
Q. NO. 8)
Source Code:
/*WAP TO EVALUATE AVERAGE OF FOUR NUMBERS*/
#include<stdio.h>
#include<conio.h>
void main(){
int a,b,c,d;
float avg;
printf("Enter values of four numbers");
scanf("%d%d%d%d",&a,&b,&c,&d);
avg=(a+b+c+d)/4;
printf ("the average of four numbers=%f",avg);
getch();
}
Output: The average value of four numbers was obtained based on the numbers given
by user.
Q. NO. 9)
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
float l,b,h,v;
printf("Enter value of length,breadth and height");
scanf("%f%f%f",&l,&b,&h);
v=l*b*h;
printf ("The volume of the cuboid=%f",v);
getch();
}
Source Code:
/*WAP TO READ PRICES OF TWO PENS AND 5 COPIES AOF SAME TYPE AND CALCULATE THE A
MOUNT AFTER 10% DISCOUNT*/
#include<stdio.h>
#include<conio.h>
void main()
{
int pen,copy,total;
float dp;
printf("Enter unit price of pen and copy");
scanf("%d%d",&pen,©);
total=(2*pen)+(5*copy);
dp=0.9*total;
printf("After discounting 10% from rs %d , You have to pay rs %f",total,dp);
getch();
}
Q. NO. 11)
Source Code:
Source Code:
/*WAP TO READ USER’S NAME AND AGE AND TO DISPLAY IT*/
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char first_name[40];
char last_name[40];
printf("Enter your first name, last name and age");
scanf("%s%s%d",&first_name,&last_name,&age);
printf("Hello %s %s , Your age is %d",first_name,last_name,age);
getch();
}
Q. NO. 13)
Source Code:
/*WAP TO DISPLAY CHARACTER ENTERED BY USER*/
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
printf("Enter a character");
scanf("%c",&a);
printf("The character entered by you is %c",a);
getch();
}
Source Code:
/*WAP THAT CONVERTS SECONDS TO MINUTES*/
#include<stdio.h>
#include<conio.h>
void main()
{
int sec,t;
int min;
printf("Enter seconds");
scanf("%d",&sec);
t=sec;
sec=sec%60;
min=(t-sec)/60;
printf ("%d is equiavelnt to %d minutes %d seconds",t,min,sec);
getch();
}
Q. NO. 2)
Source Code:
/*WAP TO ILLUSTRATE PREFIX INCREAMENT OPERATOR*/
#include<stdio.h>
#include<conio.h>
void main()
{
int x=5,v;
v=++x * ++x + ++x;
printf ("the value of v is %d",v);
getch();
}
Output: 57
Q. NO. 3)
Source Code:
Source Code:
/*WAP TO DETERMINE GREATEST AMONG THREE NUMBERS*/
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr();
x=30000,y=20000;
z=x+y;
printf ("sum=%d",z);
getch();
}
Output: 50000
When x=-30000 and y=-20000, Output: -50000
Q. NO. 5)
Source Code:
/*WAP TO RUN AND OBSERVE THE RESULT*/
#include<stdio.h>
#include<conio.h>
void main()
{
float a; char b; long int c; unsigned int e;
printf("Enter value of a");
scanf("%f",&a);
printf("Enter value of b");
scanf(" %c",&b); /*Note: put space before %c because when we hit enter, the compiler treats enter as an charact
er input.*/
printf("Enter value of c and e");
scanf("%ld%u",&c,&e);
printf ("value of a=%f\nvalue of b=%c\nvalue of c=%ld\nvalue of e=%u",a,b,c,e);
getch();
}
Source Code:
/*WAP TO CONVERT CENTIGRADE MEASURE TO
FAHRENHEIT*/ #include<stdio.h>
#include<conio.h>
void main()
{
float c,f;
printf("Enter value in centigrade");
scanf("%f",&c);
printf ("%f degree centigrade is equivalent to %f Farenheit",c,(1.8*c)+32);
getch();
}
Source Code:
/*WAP TO CALCULATE EQUIVALENT RESISTANCE WHEN THEY ARE CONNECTED TO SERIES A
ND PARALLEL*/
#include<stdio.h>
#include<conio.h>
void main()
{
unsigned int r1,r2,rs;
float rp;
printf("Enter values of resistances");
scanf("%u%u",&r1,&r2);
rs=r1+r2;
rp=float((r1*r2)/(r1+r2));
printf ("Equivant resistance when connected in series=%u\nEquivant resistance when connected in parallel=
%f",rs,rp);
getch();
}
Output: The corresponding equivalent resistances when connected to series and parallel.
Q. NO. 8)
Source Code:
/*WAP TO CALCULATE mid point of given two co-ordinates*/
#include<stdio.h>
#include<conio.h>
void main()
{
float x1,x2,y1,y2,xm,ym;
printf("Enter first point");
scanf("%f%f",&x1,&y1);
printf("Enter second point");
scanf("%f%f",&x2,&y2);
xm=(x1+x2)/2;
ym=(y1+y2)/2;
printf ("The Co-ordinate of mid point is= (%f,%f)",xm,ym);
getch();
}
Source Code:
/*WAP TO CALCULATE Ratio of boys:Girls*/
#include<stdio.h>
#include<conio.h>
void main()
{
int b,g,v=1;
printf("Enter no. of boys and girls");
scanf("%d%d",&b,&g);
while (v<=1000){
++v;
{
if (b%v==0 && g%v==0)
{
b=b/v;g=g/v;
v=1;
}
}
printf ("The ratio of Boys:girls=%d:%d",b,g);
getch();
}
Q. NO. 10a)
Source Code:
/*WAP TO EVALUATE VALUE OF SOME EXPRESSIONS*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x,y,S;
printf("Enter Enter values of x and y");
scanf("%f%f",&x,&y);
S=pow(x,5)+(0.2*x*y)+pow(y,7)
printf ("The value of S is=%f",S);
getch();
}
Output: value of S baed on user’s input.
Q. NO. 10b)
Source Code:
/*WAP TO EVALUATE VALUE OF SOME EXPRESSIONS*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,x,y,p,q,c,f;
printf("Enter Enter values of a, b, x, y, p, q, c");
scanf("%f%f%f%f%f%f%f",&a,&b,&x,&y,&c,&p,&q);
f=pow((a+b),(2*x+y)*(p-q))+c-100
printf ("The value of f is=%f",f);
getch();
}
Source Code:
/*WAP TO EVALUATE VALUE OF SOME EXPRESSIONS*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int A,B;
float r;
printf("Enter Enter values of A and B");
scanf("%d%d",&A,&B);
r=(float)A/B;
printf ("The value of r is=%f",r);
getch();
}
Q. NO. 10d)
Source Code:
/*WAP TO EVALUATE VALUE OF SOME EXPRESSIONS*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float r,u,x,v,y,p,u,q,z;
printf("Enter Enter values of u,x,v,y,p,u,q");
scanf("%f%f%f%f%f%f%f",&u,&x,&v,&y,&p,&u,&q);
z=((p*p)/(3*pow(u,2.5)))-(q/(2*v))
r=pow((u/x)+(v/y),5)/pow(z,3.5)
printf ("The value of r is=%f",r);
getch();
}
Source Code:
/*WAP TO SWAP TWO NUMBERS*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int t=0;
printf ("Enber values of a and b");
scanf("%d%d",&a,&b);
t=a;
a=b;
b=t;
printf("The values of a and b after swapping=%d and %d",a,b);
getch();
}
Conclusion
Hence, Through Lab report, I got to clear my confusions about variable types, and
pre-defined functions and debug my mistakes. And from now, I am more confident on
writing and Understand C -Program.
Lab Report-3
Theory
Character I/O: It refers to the taking of character from the user and displaying it to the
screen. Character here refers to a single alphabet or symbol. In-bult functions like getch(),
getche(), getchar(), putchar(), scanf(), printf(),etc can be used for this.
String I/O: It refers to the taking of strings from the user and displaying it to the screen.
String refers to a group of characters. In-bult functions like gets(),puts(),scanf(),printf(),etc
can be used for this.
Format Specification: Format specification are the keywords that define the format of the
input or output data items. Format specification consists of the following elements
Q. NO. 1)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
char a;
printf("Enter a character");
a=getchar();
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
char a;
char b[30];
printf("Enter a character");
scanf(" %c",&a);
printf("Enter a string");
scanf("%s",b);
printf("The entered character is:%c",a);
printf("The entered string is:%s",b);
return 0;
}
Output: Displays a character and the first word from the string entered by the user.
Q. NO. 3)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
char a[30];
printf("Enter a string");
gets(a);
printf("The entered string is:");
puts(a);
return 0;
}
Output: Displays the first word from the string entered by the user.
Q. NO. 4)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int a=12345;
printf("\ncase 1:%d",a);
printf("\ncase 2:%i",a);
printf("\ncase 3:%15d",a);
printf("\ncase 4:%-15d",a);
printf("\ncase 5:%015d",a);
printf("\ncase 6:-+15d",a);
printf("\ncase 7:%3d",a);
}
Output:
case 1:12345
case 2:12345
case 3: 12345
case 4:12345
case 5:000000000012345
case 6:-+15d
case 7:12345
Q. NO. 4)
Source Code:
#include<stdio.h>
#include<conio.h>
float main(){
float n=123.9876;
printf("\ncase 1:%f",n);
printf("\ncase 2:%e",n);
printf("\ncase 3:%g",n);
printf("\ncase 4:%15.4f",n);
printf("\ncase 5:%015.4e",n);
printf("\ncase 6:%8f",n);
printf("\ncase 7:%2.2f",n);
}
Output:
case 1:123.987602
case 2:1.239876e+002
case 3:123.988
case 4: 123.9876
case 5:00001.2399e+002
case 6:123.987602
case 7:123.99
Q. NO. 5)
Source Code:
#include<stdio.h>
#include<conio.h>
float main(){
float n=123.9876;
printf("\ncase 1:%f",n);
printf("\ncase 2:%e",n);
printf("\ncase 3:%g",n);
printf("\ncase 4:%15.4f",n);
printf("\ncase 5:%015.4e",n);
printf("\ncase 6:%8f",n);
printf("\ncase 7:%2.2f",n);
}
Output:
case 1:123.987602
case 2:1.239876e+002
case 3:123.988
case 4: 123.9876
case 5:00001.2399e+002
case 6:123.987602
case 7:123.99
Q. NO. 6)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
char ch="a";
printf("\ncase 1:%c",ch);
printf("\ncase 2:%10c",ch);
printf("\ncase 3:%-10c",ch);
return 0;
}
Output:
case 1:d
case 2: d
case 3:d
Q. NO. 7)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
char ch[20]="I Love Baglung.";
printf("\ncase 1:%s",ch);
printf("\ncase 2:%18s",ch);
printf("\ncase 3:%-18s",ch);
printf("\ncase 4:%18.8s",ch);
printf("\ncase 5:%-18.9s",ch);
printf("\ncase 6:%5s",ch);
printf("\ncase 7:%10s",ch);
return 0;
}
Output:
case 1:I Love Baglung.
case 2: I Love Baglung.
case 3:I Love Baglung.
case 4: I Love B
case 5:I Love Ba
case 6:I Love Baglung.
case 7:I Love Baglung.
Q. NO. 8)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int n=12345;
float m=123.9876;
char ch='a';
char str[20]="I love Baglung";
printf("n=%7dm=%12.5fch=%-2cstr=%16s",n,m,ch,str);
return 0;
}
Output:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int a,b;
printf("Enter an integer:");
scanf("%d",&a);
printf("The read and stored value of a=%d",a);
printf("Enter an integer:");
scanf("%3d",&b);
printf("The read and stored value of b=%d",b);
return 0;
}
Output: Prints value of a and b based on user’s input.
Q. NO. 10)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
char str[50];
printf("ENter a string");
scanf("%10c",&str);
printf("Read string is: %s",str);
return 0;
}
Output:
Input: Helloooooooooooooooooooo
Output: Helloooooo
Q. NO. 11)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
char str[70];
printf("How old are you:");
scanf("%[a-z0-9]",str);
printf("Read string is: %s",str);
return 0;
}
Output:
Q. NO. 12)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
char str[70];
printf("Enter a string");
scanf("%[^M]",str);
printf("Read string is: %s",str);
return 0;
}
Output:
Analysis and Discussion
Here, during the lab report, i understood about formatted input and output. How a
string can be stored in a variable. Also, we learned about the alignment of the string.
Conclusion
Hence, Through Lab report, I got to clear my confusions about formatted input and
output and debug my mistakes. And from now, I am more confident on writing and
Understand C -Program.
Lab Report-4
Background Theory:
Simple if statement: It is a decision making statement in c which performs a set of
instruction only when the test expression is satisfied.
Syntax: if (test expression)
{
code ;
code ;
}
Nested if statement: If there is an if statement within an if statement than that is known as
nested if statement.
Syntax: if (test expression)
{
if (test expression)
{
code ;
}
}
if….else statement: This is similar to simple if statement with the only difference being that
if the test expression is not satisfied the control is automatically shifted to else statement
block.
Syntax: if (test expression)
{
code ;
code ;
}
else
{
code ;
code ;
}
Nested if….else statement: Like nested if statement, if there is if…..else statement within
if…..else statement than that is known as nested if…..else statement.
Syntax: if (test expression)
{
code ;
}
else
if (test expression)
{
code ;
code ;
}
else
{
code ;
code ;
}
}
Switch statement: A switch statement is a type of selection control mechanism used
to allow the value of a variable or expression to change the control flow of program.
Syntax: switch(a) (a is a numerical or character type variable)
{
case 1:
code;
break;
case 2:
code;
break;
case n:
code;
break;
default:
code:
}
Q. NO. 1)
Algorithm:
1. Start
2. Read User’s age.
3. Is age>65?
Yes: You will get seniority allowance.
No: You will not get seniority allowance.
4. Stop.
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int age;
printf("Enter your age");
scanf("%d",&age);
if (age>65)
{
printf("seniority is allowed");
}
else{
printf("seniority is not allowed");
}
return 0;
}
Output:
Q. NO. 2)
Algorithm:
1. Start
2. Enter an integer.
3. Is the number >0?
Yes: Display Positive number.
No: Check whether the no. is negative or zero.
4. Is the number <0?
Yes: Display Negative number.
No: Display Zero.
5. Stop
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int num;
printf("Enter a number");
scanf("%d",&num);
if (num>0)
{
printf("Positive number");
}
if (num<0)
{
printf("Negative number");
}
else{
printf("Zero");
}
return 0;
}
Output:
Q. NO. 3)
1. Algorithm: Start
2. Read sides of the triangle a, b & c.
3. Is a+b>c And is b+c>a And c+a>b?
Yes:
a+b+ c
s=
2
Display Area=√ s ( s−a)(s−b)( s−c )
No:
Display the triangle doesn’t exist.
4. Stop
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
float a,b,c,s,area;
printf("Enter three sides of the triangle");
scanf("%f%f%f",&a,&b,&c);
if (a+b>c && b+c>a && c+a>b)
{
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\n\nArea of the triangle is: %f",area);
}
else{
printf("The triangle doesn't exist.");
}
return 0;
}
Output:
Q. NO. 4)
Algorithm:
1. Start
2. Enter a letter (ch).
3. Is ch≥ ' a’ And ch≤‘z’?
Yes: Display Lowercase
No: Check if it is an Uppercase.
4. Is ch≥ ' A’ And ch≤ ‘Z’?
Yes: Display Uppercase
No: Display Invalid Character. Enter alphabet letter only.
5. Stop.
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
void main(){
char c;
int ch=c;
printf("ENter a character\n");
ch=getchar();
if (ch>=65 && ch<=90)
{
printf("Uppercase");
}
if (ch>=97 && ch=<122)
{
printf("Lower case");
}
getch();
}
Output:
Q. NO. 5)
Algorithm:
1. Start
2. Enter a number n.
3. Is n mod 2=0?
Yes: Check if n>100?
o Yes: Display the number is even and greater than zero.
o No: Display the number is even but not greater than zero.
No: Check if n mod 11=0?
o Yes: Check if n mod 7≠ 0?
Yes: Display the given number is odd. It is divisible by 11 but not by 7.
No: Display The given number is odd . It is divisible by both 11 & 7.
o No: Display The given number is odd but not divisible by 11.
4. Stop
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
unsigned int num;
printf("Enter a number");
scanf("%u",&num);
if(num%2==0){
if(num>100){
printf("The number is Even and greater than 100");
}
else{
printf("The number is even but not greater than 100");}
}
else{
if(num%11==0 && num%7!=0){
printf("The number is odd as well as divisible by 11 but not by 7");
}
else{
printf("The number is simply Odd.");
}
}
return 0;
}
Output:
Q. NO. 6)
Algorithm:
6) Algorithm:
1. Start
2. Enter values of A,B and C.
3. D=B2-4AC
4. Check if D>0?
−b+ √ D −b− √ D
Yes: Display the unequal and distinct Roots are: x1¿ And x2¿
2a 2a
No: Check if D<0?
Yes: D=|D|
−b+i √ D −b−i √ D
Display the imaginary roots are Roots are: x1¿ And x2¿
2a 2a
−b
No: Display The real and equal roots are: x¿
2a
5. Stop
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
float a,b,c,d,x1,x2;
printf("ENter the values of a,b and c corresponding to the standard quadratic equa
tion");
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 real and unequal roots are %f and %f",x1,x2);
}
else if(d<0){
d=-d;
x1=(-b/2*a);
x2=(sqrt(d)/(2*a));
printf("The imaginary and unequal roots are %f+i%f and %f-i%f",x1,x2,x1,x2);
}
else{
x1=(-b/(2*a));
printf("The real and equal roots are %f and %f",x1,x1);
}
return 0;
}
Output:
Q. NO. 7)
Algorithm:
1. Start
2. Enter value of x.
3. Check if x≤ 0?
Yes: Display 0.
No: Goto step 4.
4. Check if 0<x≤ 10?
Yes: Display x(x-10)(x-15)
No: Goto step 5.
5. Check if Is 10<x≤ 15?
Yes: Display (x-10)(x-20)(x-30)
No: Goto step 6.
6. Check if 15<x≤ 20?
Yes: Display (x-15)(x-20)(x-30)
No: Goto step 7.
7. Check if 30<x≤ 20?
Yes: Display (x-20)(x-30)(x-40)
No: Display 0.
8. Stop
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
float x;
printf ("ENter the value of x");
scanf("%f",&x);
if (x<=0)
{
printf("The value of x is: %f",0);
}
else if (x>0 && x<=10)
{
printf("The value of x is: %f",x*(x-10)*(x-15));
}
else if (x>10 && x<=15)
{
printf("The value of x is: %f",(x-10)*(x-15)*(x-20));
}
else if (x>15 && x<=20)
{
printf("The value of x is: %f",(x-15)*(x-20)*(x-30));
}
else if (x>20 && x<=30)
{
printf("The value of x is: %f",(x-20)*(x-30)*(x-40));
}
else{
printf("The value of x is: %f",0);
}
return 0;
}
Output:
Q. NO. 8)
Algorithm:
1. Start
2. Enter the number of the day.
3. Check if day=1?
Yes: Display Sunday.
No: Goto step 4.
4. Check if day=2?
Yes: Display Monday.
No: Goto step 5.
5. Check if day=3?
Yes: Display Tuesday.
No: Goto step 6.
6. Check if day=4?
Yes: Display Wednesday.
No: Goto step 7.
7. Check if day=5?
Yes: Display Display Thursday.
No: Goto step 8.
8. Check if day=6?
Yes: Display Display Friday.
No: Goto step 9.
9. Check if day=7?
Yes: Display Display Saturday.
No: Display enter number ranging form 1 to 7.
Goto step 2
10. Stop.
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int day;
printf("Enter the corresponding number day");
scanf("%d",&day);
switch (day)
{
case 1: printf("The day is Sunday");
break;
case 2: printf("The day is Monday");
break;
case 3: printf("The day is Tuesday");
break;
case 4: printf("The day is Wednesday");
break;
case 5: printf("The day is Thrusday");
break;
case 6: printf("The day is Friday");
break;
case 7: printf("The day is Saturday");
break;
default: printf("ENter the number ranging from 1-7");
break;
}
return 0;
}
Output:
Q. NO. 9)
Algorithm:
1. Start
2. Enter two numbers a and b.
3. Enter the operation (op) you want to perform.
4. Is op=“+”?
Yes: Display a+b.
No: Goto step 5.
5. Is op=“-”?
Yes: Display a-b.
No: Goto step 6.
6. Is op= =“*”?
Yes: Display a*b.
No: Goto step 7.
7. Is op=“/”?
Yes: Display a/b.
No: Goto step 8.
8. Is op=“%”?
Yes: Display a%b.
No: Goto step 9.
9. Stop.
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
float a,b;
int op;
printf("Enter two numbers");
scanf("%f%f",&a,&b);
printf("Which operation do you want to perform?\n");
printf("Hint: Press 1 for addition \n");
printf(" Press 2 for subtraction \n");
printf(" Press 3 for multiplication \n");
printf(" Press 4 for division \n");
scanf("%d",&op);
switch (op){
case 1: {printf("The addition of given number is: %f",a+b);
break;
}
case 2: {printf("The subtraction of given number is: %f",a-b);
break;
}
case 3: {printf("The multiplication of given number is: %f",a*b);
break;
}
case 4: {printf("The division of given number is: %f",a/b);
break;
}
default: {
printf("Enter valid operation");
break;
}
}
return 0;
}
Output:
Algorithm:
1. Start
2. N=N+1
3. Is N<=100?
Yes: Enter a integer value and store in x.
Is x mod 9=0 And x mod 6≠ 0 ?And x>1 And x<100?
Yes: m=m+1
sum=sum+x
No: Goto step 3.
No: Ask user if he wants to enter more input.
Yes: N=N-2
No: Is m=0?
Yes: Display No such number was entered which is divisible by 9 but not by 6 and
which lies between 1 to 100.
No: average=sum/m
Display sum and average.
4. Stop
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int num=0,count=0,sum=0;
float avg;
char ch;
int main(){
int a;
Y:
if (num<=100)
{
num=num+1;
X:
printf("Enter the number");
scanf("%d",&a);
if (a%9==0 && a%6!=0 && a>1 && a<100) {sum=sum+num;count=count+1;}
goto Y;
}
else{
printf("Do you want to enter more number? y--->Yes n---->no");
ch=getch();
if (ch=='y')
{
goto X;
}
else if(ch=='n'){
if (count!=0)
{
avg=(float)(sum/count);
printf("There were found %d numbers whose sum are %d and average is %f",count,sum,
avg);
}
else{
printf("No such numbers were found!");
}
}
}
return 0;
}
Output:
Analysis and discussion
Questions were moderate with some involving higher level of thinking. Mostly the
question dealt with simple if condition and some of them were very logical.
Conclusion
In conclusion, lab 4made me clear of the concept of if…else like how we can use to
calculate different series and solve different mathematical problems with it. Moreover
it also made me clear how multiple lines of code, if suitable, can be shortened down
with the concept of if/else.
Lab Report-5
Theory
Looping in C : looping refers to doing a certain sets of instruction repetitively until a certain
criteria is met.While, Do..While,For statement are the satements that do looping in c.
for Loop: For loop is the repition of codes using for statement where a variable is initialized a
certain value and an expression is tested for each while the value is incremented.
Syntax:for(initialization expression;test expression;update expression)
{
code;
}
while loop: It is also repition of code using while statement where each time a certain
expression is tested at the beginning of the loop and if the expression is true than only the loops
gets executed.
Syntax: while(test expression)
{
code;
}
Do…..While loop: It is similar to while loop but the only difference is that the expression is
tested at the end of the loop.It is done using Do…while statement.
Syntax: do
{
code;
} while(test expression);
break Statement: Break statement in c is used to exit out of the loop.Whenever break;
statement is encountered in the loop it immediately causes the control to leave the looping.
Syntax: break;
continue Statement: continue statement in c is used to take the control to the beginning of the
loop.
Syntax: continue;
goto statement: This is used to take the control to a specified line.
Syntax: goto label1;
code;
code;
label1:
code;
Q. NO. 1a)
Algorithm:
Step 1: Start.
Step 2: Declare variables i and n
Step 2: Read n
Step 3: i=0;
Step 4: Is I < n ?
Yes:goto Step 5
No: goto Step10
Step 5: Display i+1
Step 6: i=i+1
Step 7: goto step 4
Step 8: Stop
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int i;
system("cls");
for ( i = 0; i <=255; i++)
{
printf("%d=%c\t",i,i);
}
return 0;
}
Output:
Q. NO. 1b)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int a=10;
system("cls");
printf("%d",a);
a=a+50;
printf("%d",a);
return 0;
}
Q. NO. 2)
Algorithm:
Step 1: Start.
Step 2: Declare variables i and n
Step 3: Read n
Step 4: i=0;
Step 5: Is i < n ?
Yes:Go to Step 6
No: Go to Step 9
Step 6: Display i+1 and n-i
Step 7: i=i+1
Step 8: Go to step 5
Step 9: Stop
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int i;
unsigned int n;
printf("ENter the value of n");
scanf("%u",&n);
printf("1 to %u:\t",n);
for ( i = 1; i <= n; i++)
{
printf("%d\t",i);
}
printf("\n%u to 1:\t",n);
for ( i = n; i >= 1; i--)
{
printf("%d\t",i);
}
return 0;
}
Output:
Q. NO. 3)
Algorithm:
Step 1: Start.
Step 2: Declare variables i , n and sum
Step 3: Read n
Step 4: i=0 and sum=0;
Step 5: Is i < n ?
Yes: Go to Step 6
No: Go to Step 10
Step 6: Is i MOD 2=0?
Yes: Go to Step 7
No: Go to Step 8
Step 7: sum=sum+i
Step 8: i=i+1
Step 9: Go to step 5
Step 10: Display sum
Step 11: Stop
Flowchart:
Source Code:
) #include<stdio.h>
#include<conio.h>
int main(){
unsigned int n,sum=0,i;
printf("ENter the value of n");
scanf("%u",&n);
for (i = 1; i <=n; i++)
{
if (i%2==0)
{
sum+=i;
}
}
printf("Sum of even numbers from 1 to %u=%u",n,sum);
return 0;
}
Output:
Q. NO. 4)
Algorithm:
Step 1: Start.
Step 2: Declare variables i , n , sum and fact
Step 3: Read n
Step 4: i=0 and fact=1;
Step 5: Is n MOD i =0 ?
Yes: Go to Step 9
No: Go to Step 7
Step 6: sum= ( n*(n+1) )/2
Step 7: Display sum
Step 8: Go to Step 14
Step 9: Is i < n ?
Yes:Go to Step 10
No: Go to Step 13
Flowchart:
Source Code:
))) #include<stdio.h>
#include<conio.h>
int main(){
int n,sum=0,i,product=1;
printf("ENter the value of n");
scanf("%d",&n);
for (i = 1; i <=n; i++)
{
if (n%2==0)
{
product*=i;
}
else{
sum+=i;
}
}
if (sum!=0)
{
printf("Sum of natural numbers from 1 to %d is: %d",n,sum);
}
else{
printf("Product of natural numbers from 1 to %d is: %d",n,product);
}
return 0;
}
Output:
Q. NO. 5)
Algorithm:
Step 1: Start.
Step 2: Declare variables i and n and fact
Step 3: Read n
Step 4: i=0 and fact=1;
Step 5: Is n – integer (n)=0 ?
Yes: Go to Step 8
No: Go to Step 6
Step 6: Display "The factorial of the given number cannot be calculated."
Step 7: Go to Step 12
Step 8: Is i < n ?
Yes:Go to Step 9
No: Go to Step 12
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
float product=1,num;
float i;
printf ("ENter the number whose factorial is to be calculated.");
scanf("%f",&num);
if ((int)(num)!=num || num<0 )
{
printf ("Factorial cannot be calculated");
}
else{
for (i = 1;i <= num;i++)
{
product*=i;
}
printf("The factorial of %f=%f",num,product);
}
return 0;
}
Output:
ENter the number whose factorial is to be calculated.6
Q. NO. 6)
Algorithm:
Step 1: Start.
Step 2: Declare variables i , n , x , num and den
Step 3: Read n and x
Step 4: i=0 , den=1 , num=1
Step 5: Is i < n ?
Yes:Go to Step 6
No: Go to Step 9
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
float i;
unsigned int n;
float x,num=1,den=1;
printf("Enter the valude of x and n.");
scanf("%f",&x);
scanf("%u",&n);
for ( i = 1; i <= n; i++)
{
num*=x;
den*=(float)i;
}
printf("The value for the expression (%f^%u)/(%u!)=(%f)",x,n,n,(num/den));
return 0;
}
Output:
Enter the valude of x and n.5
Algorithm:
Step 1: Start.
Step 2: Declare variables i , n , x , prev and term
Step 3: Read n
Step 4: i=0 , prev=1 , term=2
Step 5: Display 0
Step 6: Is i < n ?
Yes:Go to Step 7
No: Go to Step 11
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int current=1,previous=0,next;
int n,i; // stands for nth terrm 1,1,2,3,5,8,13,21,34,....
printf("Which term of fibonacci series do you want?");
scanf("%d",&n);
for ( i = 2; i <=n; i++)
{
next=previous+current;
previous=current;
current=next;
}
printf("The %dth term of fibonacci series = %d ",n,current);
return 0;
}
Output:
Which term of fibonacci series do you want?6
Q. NO. 8)
Algorithm:
Step 1: Start.
Step 2: Declare variables n , r and sum
Step 3: Read n
Step 4: i=0 and sum=0
Step 5: r= a MOD 10
Step 6: sum=sum+r
Step 7: n=n/10
Step 8: Is n=0 ?
Yes:Go to Step 9
No: Go to Step 5
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
unsigned int num,i,rem,sum=0,x;
printf("Enter the number");
scanf("%u",&num);
x=num;
while(rem!=0){
rem=x%10;
sum+=rem;
x/=10;
}
printf("The sum of the digits of the number %u = %u",num,sum);
return 0;
}
Output:
Enter the number 5526
Q. NO. 9a)
Algorithm:
To check palindrome
Step 1: Start.
Step 2: Declare variables n , a , r and sum
Step 3: Read n
Step 4: i=0 , sum=0 and a=n
Step 5: r= a MOD 10
Step 6: sum=sum+r
Step 7: n=n/10
Step 8: Is n=0 ?
Yes:Go to Step 9
No: Go to Step 5
Step 9: is sum=a ?
Yes: Go to Step 10
No: Go to Step 12
Step 10: Display a "is a palindrome"
Step 11: Go to Step 13
Step 12: Display a "is not a palindrome"
Step 13: Stop
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int num,i,x,rem,rev=0;
printf("Enter a number");
scanf("%d",&num);
x=num;
while (x!=0)
{
rem=x%10;
rev=rev*10+rem;
x=x/10;
}
if (rev==num)
{
printf("The given number is a palindrome number.");
}
else{
printf("The given number is not a palindrome number.");
}
return 0;
}
Output:
Enter a number55255
Q. NO. 9b)
Algorithm:
Step 1: Start
Step 2: Declare variables n , a , l , i , r , p , j , sum
Step 3: Read n
Step 4: a=n , l=0 and sum=0
Step 5: a=a/10
Step 6: l=l+1
Step 7: Is a=0 ?
Yes: Go to step 8
No: Go to step 5
Step 8: a=n
Step 9: Is i< l ?
Yes: Go to Step
No: Go to Step
Step 10: r=a MOD 10
Step 11: a=a/10
Step 12: p=1 and j=0
Step 13: is j<l ?
Yes: Go to Step 14
No: Go to Step 17
Step 14: p=p*r
Step 15: j=j+1
Step 16: Go to Step 13
Step 17: sum=sum+r
Step 18: i=i+1
Step 19: Go to Step 9
Step 20: Is sum=n ?
Yes: Go to step 21
No: Go to Step 23
Step 21: Display "The number is an armstrong number"
Step 22: Go to Step 24
Step 23: Display "The number is not an armstrong number"
Step 24: Stop
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int num,i,x,rem,sum=0;
printf("Enter a number");
scanf("%d",&num);
x=num;
while (x!=0)
{
rem=x%10;
sum+=(rem*rem*rem);
x=x/10;
}
if (sum==num)
{
printf("The given number is an armstrong number.");
}
else{
printf("The given number is not an armstrong number.");
}
return 0;
}
Output:
Enter a number153
Q. NO. 9c)
Algorithm:
To check prime
Step 1: Start
Step 2: Declare variables i and n
Step 3: Read n
Step 4: i=2
Step 5: is i <n ?
Yes: Go to Step 6
No: Go to Step 11
Step 6: Is n MOD i=0?
Yes: Go to Step 7
No: Go to Step 9
Step 7: Display n "is not a prime number"
Step 8: Go to Step 12
Step 9: i=i+1
Step 10: Go to step 5
Step 11: Display n "is a prime number"
Step 12: Stop
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int num,i,count=0;
printf("Enter a number");
scanf("%d",&num);
for (i = 2; i <=(num/2); i++)
{
if (num%i==0)
{
count++;
}
}
if (count==0)
{
printf("%d is a prime number",num);
}
else{
printf("%d is not a prime number",num);
}
return 0;
}
Output:
Enter a number53
53 is a prime number
Q. NO. 9d)
Algorithm:
Twin Prime
Step 1: Start
Step 2: Declare variables i , n , count1 , count2 , count3
Step 3: Read n
Step 4: i=2 ,count=0 , coun2 =0 and count3=0
Step 5: Is i <(n+2)/2 ?
Yes: Go to Step 6
No: Go to Step 14
Step 6: Is n MOD i=0?
Yes: Go to Step 7
No: Go to Step 8
Step 7: count1=1
Step 8: Is (n-2) MOD i=0
Yes: Go to Step 9
No: Go to Step 10
Step 9: count2=1
Step 10: Is (n+2) MOD i=0
Yes: Go to Step 11
No: Go to Step 12
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int num1,num2,i,count1=0,count2=0;
X:
printf("\nEnter two prime numbers");
scanf("%d%d",&num1,&num2);
for (i = 2; i <=(num1/2); i++)
{
if (num1%i==0)
{
count1++;
}
}
for (i = 2; i <=(num2/2); i++)
{
if (num2%i==0)
{
count2++;
}
}
if (count1==0 && count2==0)
{
if (num1-num2==2 || num2-num1==2)
{
printf("The given numbers are twin prime numbers.");
}
else{
printf("The given numbers are not twin prime numbers.");
}
}
else{
printf("Enter both prime numbers.");
goto X;
}
return 0;
}
Output:
Enter two prime numbers 5
3
The given numbers are twin prime numbers.
Q. NO. 10)
Algorithm:
Step 1: Start.
Step 2: Declare variables i and n
Step 3: Read n
Step 4: i=1;
Step 5: Is i ≤ 10 ?
Yes:Go to Step 6
No: Go to Step 9
Step 6: Display n " * " i " = " n*i
Step 7: i=i+1
Step 8: Go to step 5
Step 9: Stop
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int num,i;
printf("Enter a number");
scanf("%d",&num);
for ( i = 1; i <= 10; i++)
{
printf("%d X %d = %d\n",num,i,num*i);
}
return 0;
}
Output:
Enter a number7
7X1=7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70
Lab Report-6
Background Theory:
Nested for loop: It is the condition of having for loop inside a for loop.
Syntax:
for (int i = 0 ; i <count1 ; i++)
{
for (int j =0 ; j<=count2 ; j++){
/* code */
}
}
Nested while loop: It is the condition of having while loop inside a while loop.
Syntax:
while (test expression 1)
{
while (test expression 2){
/* code */
}
}
Syntax:
do
{
do{
/* code */
}while (test expression 2);
} while (test expression 1);
While: It is a loop which keeps on executing until and unless the test expression
becomes false.
Break: It is a statement in C programming. Whenever Break statement is
executed, the loop in which break statement was executed gets terminated.
Eg: #include<stdio.h>
#include<conio.h>
int main(){
int a=0;
while(1){
printf("*");
if (a==10)
{
break;
}
a++;
}
return 0;
}
In the above program, when the value of a becomes 10, the loop breaks and
the program finally gets terminated.
The output of the above program is simply odd number from 1 to 19. Initially
when i=2 and n=2(initialized), as i=n, if condition becomes true and the
statement inside if condition gets executed. n becomes 4 and continue
statement gets executed. That means without executing printf statement, it
simply redirectes to label X.
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int m,n,i;
printf("Enter values of m and n.");
scanf("%d%d",&m,&n);
for ( i = 1; i <=n; i++)
{
printf("%d X %d = %d\n",m,i,m*i);
}
return 0;
}
Output:
Enter values of m and n.5
5X1=5
5 X 2 = 10
5 X 3 = 15
Q. NO. 2)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int row=8,column=8,i,j;
for(i=1;i<=row;i++){
for ( j = 1; j <=column; j++)
if ((i+j)%2==0)
{
printf("\xdb"); //Used for printing white space
}
else{
printf(" ");
}
printf("\n");
}
return 0;
}
Output:
Q. NO. 3)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int n1,n2,temp,i,j,count1=0,count2=0,arm1=0,arm2=0,rem1=0,rem2=0,sum1=0,sum2=0;
printf("Enter two numbers");
scanf("%d%d",&n1,&n2);
//Swap
if (n1>n2)
{
temp=n2;
n2=n1;
n1=temp;
}
int x1=n1,x2=n2;
for ( i = 1; i <=n1; i++)
{
if (n1%i==0)
{
count1++;
}
}
for ( i = 1; i <=n2; i++)
{
if (n2%i==0)
{
count2++;
}
}
while(x1!=0){
rem1=x1%10;
sum1+=(rem1*rem1*rem1);
x1/=10;
}
if (sum1==n1)
{
arm1=1;
}
while(x2!=0){
rem2=x2%10;
sum2+=(rem2*rem2*rem2);
x2/=10;
}
if (sum2==n2)
{
arm2=1;
}
if (count1<=2)
{
printf("%d is a prime number\n",n1);
}
if (count2<=2)
{
printf("%d is a prime number\n",n2);
}
if (arm1==1)
{
printf("%d is an armstrong number\n",n1);
}
if (arm2==1)
{
printf("%d is an armstrong number\n",n2);
}
printf("%d is divisible by %d numbers\n",n1,count1);
printf("%d is divisible by %d numbers\n",n2,count2);
return 0;
}
Output:
Enter two numbers5
6
5 is a prime number
5 is divisible by 2 numbers
6 is divisible by 4 numbers
Q. NO. 4)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
unsigned int num,sum=0;
do
{
printf("Enter the number\n");
scanf("%u",&num);
sum+=num;
} while (num);
printf("The sum of the numbers is %d\n",sum);
return 0;
}
Output:
65
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int n,i;
printf("Upto which term do you want to print?\n");
scanf("%d",&n);
for ( i = 1; i <=n; i++)
{
if (i<n)
{
printf("%d,",i);
}
else{
printf("%d",i);
}
}
return 0;
}
Output:
Upto which term do you want to print?
1,2,3,4,5,6,7,8
Q. NO. 5b)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int n,i;
printf("Upto which term do you want to print?\n");
scanf("%d",&n);
for ( i = 1; i <=n; i++)
{
if (i<n)
{
printf("%d,",2*i);
}
else{
printf("%d",2*i);
}
}
return 0;
}
Output:
2,4,6,8,10,12,14,16,18
Q. NO. 5c)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){ //to print 1,2,5,10,17,26,....,n //Either (n^2+1) OR (previous nu
mber+nth odd number)
int n,i,odd=-1;
int previous_number=2,sum;
printf("Upto which term do you want to print?\n");
scanf("%d",&n);
for ( i = 1; i <=n; i++)
{
sum=previous_number+odd;
if (i<n)
{
printf("%d,",sum);
}
else{
printf("%d",sum);
}
odd+=2;
previous_number=sum;
}
return 0;
}
Alternative:
#include<stdio.h>
#include<conio.h>
int main(){ //to print 1,2,5,10,17,26,....,n //Either (n^2+1) OR (previous nu
mber+nth odd number)
int n,i;
printf("Upto which term do you want to print?\n");
scanf("%d",&n);
for ( i = 0; i <=n; i++)
{
if (i<n)
{
printf("%d,",(i*i+1));
}
else{
printf("%d",(i*i+1));
}
}
return 0;
}
Output:
Upto which term do you want to print?
1,2,5,10,17,26,37,50,65,82
Q. NO. 5d)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
float n,i;
float x;
printf("Upto which term do you want to print?\n");
scanf("%f",&n);
for ( i = 1; i <=n; i++)
{
x=((i*i)+((i+1)*(i+1)))/(i+1);
if (i<n)
{
printf("%f,",x);
}
else{
printf("%f",x);
}
}
return 0;
}
Output:
2.500000,4.333333,6.250000,8.200000,10.166667,12.142858,14.125000,16.111111,18.100000
Q. NO. 5e)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
float n,i;
printf("Upto which term do you want to print?\n");
scanf("%f",&n);
for ( i = 1; i <=(2*n-1); i+=2)
{
if (i<n)
{
printf("%f,",1/i);
}
else{
printf("%f",1/i);
}
}
return 0;
}
Output:
Upto which term do you want to print?
9
1.000000,0.333333,0.200000,0.142857,0.1111110.0909090.0769230.0666670.058824
Q. NO. 6a)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int n,i,sum=0;
printf("Upto which term do you want to do sum of?");
scanf("%d",&n);
for (i = 2; i <=2*n; i+=2)
{
sum+=i;
}
printf("Sum of the %d terms=%d",n,sum);
return 0;
}
Output:
Upto which term do you want to do sum of?8
Q. NO. 6b)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
float sign=1,num=1,den=1,n,term=0,sum=0,i,j,factorial=1;
printf("Upto which term do you want of sum of?");
scanf("%f",&n);
for (i = 1; i <=n; i++)
{
for(j=1;j<=(i-1);j++){
factorial*=j;
}
den=factorial;
factorial=1;
term=(sign)*(num/den);
sum+=term;
sign*=-1;
}
printf("sum=%f",sum);
return 0;
}
Output:
Upto which term do you want of sum of?9
sum=0.367882
Q. NO. 6c)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
float sign=1,num=1,den=1,n,term=0,sum=0,i,j,factorial=1,x;
printf("Enter the value of x");
scanf("%f",&x);
printf("Upto which term do you want of sum of?");
scanf("%f",&n);
for (i = 0; i <=(n-1); i++)
{
for(j=1;j<=2*i;j++){
factorial*=j;
num*=x;
}
den=factorial;
factorial=1; //Factorial Reset
term=(sign)*(num/den);
sum+=term;
num=1; //Numerator Reset
sign*=-1;
}
printf("sum=%f",sum);
return 0;
}
Output:
Enter the value of x5
Upto which term do you want of sum of?9
sum=0.284220
Q. NO. 7)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
float num=1,den=1,n,term=0,sum=0,i,j,factorial=1,x;
printf("Enter the value of x");
scanf("%f",&x);
printf("Upto which term do you want of sum of?");
scanf("%f",&n);
for (i = 0; i <=(n-1); i++)
{
for(j=1;j<=i;j++){
factorial*=j;
num*=x;
}
den=factorial;
factorial=1; //Factorial Reset
term=(num/den);
sum+=term;
num=1; //Numerator Reset
}
printf("sum=%f",sum);
return 0;
}
Output:
Enter the value of x6
sum=341.799988
Q. NO. 8a)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int n,i,j;
printf("Enter the value of n");
scanf("%d",&n);
for ( i =1; i <=n; i++)
{
for ( j = 1; j<=i; j++)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}
Output:
Enter the value of n5
12
123
1234
12345
Q. NO. 8b)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int n,i,j;
printf("Enter the value of n");
scanf("%d",&n);
for (i=n; i>=1; i--)
{
for ( j = i; j>=1; j--)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
Output:
Enter the value of n6
654321
54321
4321
321
21
Q. NO. 8c)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int n,i,j;
char str[10]={'N','E','P','A','L'};
n=5;
for (i=0; i<n; i++)
{
printf("%d ",i+1);
for ( j = 0; j<=i; j++)
{
printf("%c ",str[i]);
}
printf("\n");
}
return 0;
}
Output:
1N
2EE
3PPP
4AAAA
5LLLLL
Q. NO. 8d)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int n,i,j;
char alphabet='A';
n=5;
for (i=1; i<=n; i++)
{
for ( j = 1; j<=i; j++)
{
printf("%c ",alphabet);
}
alphabet++;
printf("\n");
}
return 0;
}
Output:
A
BB
CCC
DDDD
EEEEE
Q. NO. 8e)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int n,i,j,x;
char star='*';
char hash='#';
n=5;
x=n;
for (i=1; i<=n; i++)
{
for ( j = 1; j<=5; j++)
{
if (j>=x)
{
printf("%c ",star);
}
else{
printf("%c ",hash);
}
}
x--;
printf("\n");
}
return 0;
}
Output:
####*
###**
##***
#****
*****
Lab 5 and 6
Analysis and discussion
Questions were moderate with some involving higher level of thinking. Mostly the
question dealt with loops and nested loops. Lab 5 mainly consisted of math problems
while lab 6 consisted of printing different series and sum common thing being concept
of looping.
Conclusion
In conclusion, lab 5 and 6 made me clear of the concept of looping like how we can
use to calculate different series and solve different mathematical problems with it.
Moreover it also made me clear how multiple lines of code, if suitable, can be
shortened down with the concept of loop.
Lab Report-7
Theory
Function: A function is a self-contained program that carries out a specific and well defined
task.
Advantages of using function: The man advantages of using function are:
1. It avoids repetition of codes.
2. It makes it easy to modify and debug program.
3. It makes it easier to understand the program.
Function Declaration: Declaration of the function and its type at the beginning of the
program is known as function declaration.
Syntax: Return_type function_name (argument type1,argument type 2);
Categories of function:There are two categories of function which are:-
Library Function:These are the function that have already been written, compiled and
place in libraries.printf(), scanf(), getch(), are the examples of library function.
User defined function: These are the function which the user defines in his/her
program.
Function call: A function remains inactive unless a call is made to the fuction and this is
known as function call.
Function call can be further divided to two types which are:-
Call by value: Calling a function by passing the arguments is known as call by value.
Syntax: function_name(arg 1 , arg 2); (if it’s a return type function than the value which it
returns can be stored in a variable)
Call by reference: Calling the function by passing the address of the arguments is known as
call by reference.
Syntax: function_name( &arg 1 , &arg 2); (if it’s a return type function than the value
which it returns can be stored in a variable)
Types of user defined function: User defined function can further be divide to:-
Non-Return Type: This type of function do not return anything when they are called.
Syntax: void Function_name()
{
code;
}
Return Type: This type of function returns based on the return type declared
whenever its call is made.
Syntax: return type function_name(arg 1,arg 2,arg 3)
{
code;
return(return expression);
}
Q. NO. 1)
Source Code:
#include<stdio.h>
#include<conio.h>
float add(int,float);
int main(){
int n1;
float n2;
float sum;
printf("Enter the value of integer number");
scanf("%d",&n1);
printf("Enter the value of float number");
scanf("%f",&n2);
sum=add(n1,n2);
printf("Enter the Sum of the numbers=%f",sum);
return 0;
}
float add(int x,float y){
return (float)(x)+y;
}
Output:
Q. NO. 2)
Source Code:
#include<stdio.h>
#include<Conio.h>
void SumOfDigits(int);
int main(){
int n;
printf("Enter the number.\n");
scanf("%d",&n);
SumOfDigits(n);
return 0;
}
void SumOfDigits(int x){
int y,rem=0,sum=0;
y=x;
while (y!=0)
{
rem=y%10;
sum+=rem;
y/=10;
}
printf("The sum of the digits of the number %d = %d",x,sum);
}
Output:
Enter the number.
5526
Source Code:
#include<stdio.h>
#include<conio.h>
unsigned int fact(unsigned int);
int main(){
unsigned int n;
printf("Enter the number.\n");
scanf("%u",&n);
printf("The factorial of %u = %u",n,fact(n));
return 0;
}
unsigned int fact(unsigned int x){
if(x==1 || x==0){
return 1;
}
else{
return x*fact(x-1);
}
}
Output:
Enter the number.
Q. NO. 4)
Source Code:
#include<stdio.h>
#include<conio.h>
void check_prime(int);
int main(){
int n;
printf("Enter the value of n");
scanf("%d",&n);
check_prime(n);
return 0;
}
void check_prime(int x){
int i,count=0;
for ( i = 1; i <=x; i++)
{
if (x%i==0)
count++;
}
if (count>2)
{
printf("%d is a composite number.",x);
}
else if(count==1){
printf("%d is neither prime nor Composite numder",x);
}
else if (count<=2){
printf("%d is a prime number.",x);
}
}
Output:
Enter the value of n5
5 is a prime number.
Q. NO. 5)
Source Code:
#include<stdio.h>
#include<conio.h>
void menu();
void sum_of_two_numbers();
void sum_of_digits();
void factorial();
void check_prime();
int main(){
int choice;
menu();
X:
scanf("%d",&choice);
switch(choice){
case 1: {sum_of_two_numbers();break;}
case 2: {sum_of_digits();break;}
case 3: {factorial();break;}
case 4: {check_prime();break;}
default: {printf("Enter valid choice");goto X;break;}
}
return 0;
}
void menu(){
printf("Enter your choices from below:-\n");
printf("1. Sum of two numbers.\n2.Sum of digits\n3.Factorial of an integer\
n4.Check whether a number is prime or not.\n");
}
void sum_of_two_numbers()
{
int n1;
int n2;
int sum;
printf("Enter the value of first number");
scanf("%d",&n1);
printf("Enter the value of second number");
scanf("%d",&n2);
sum=n1+n2;
printf("Enter the Sum of the numbers=%d",sum);
}
void sum_of_digits(){
int n;
printf("Enter the number.\n");
scanf("%d",&n);
int y,rem=0,sum=0;
y=n;
while (y!=0)
{
rem=y%10;
sum+=rem;
y/=10;
}
printf("The sum of the digits of the number %d = %d",n,sum);
}
void factorial(){
int n,x,fact=1;
printf("Enter the number.\n");
scanf("%d",&n);
x=n;
if (n==0);
else{
while(x!=0){
fact*=x;
x--;
}
}
printf("The Factorial of %d = %d",n,fact);
}
void check_prime(){
int x;
printf("Enter the value of n");
scanf("%d",&x);
int i,count=0;
for ( i = 1; i <=x; i++)
{
if (x%i==0)
count++;
}
if (count>2)
{
printf("%d is a composite number.",x);
}
else if(count==1){
printf("%d is neither prime nor Composite numder",x);
}
else if (count<=2){
printf("%d is a prime number.",x);
}
}
Output:
Enter your choices from below:-
2.Sum of digits
3.Factorial of an integer
5526
Q. NO. 6)
Source Code:
#include<stdio.h>
#include<conio.h>
void countDigits(int,int*,int*);
int main(){
int count_odd=0,count_even=0, num;
int *p1,*p2;
p1=&count_odd;
p2=&count_even;
printf("Enter a number.\n");
scanf("%d",&num);
countDigits(num,p1,p2);
printf("%d contains %d even digits and %d odd digits.",num,count_even,count_odd);
return 0;
}
void countDigits(int x,int *ptr_odd, int *ptr_even){
int rem;
while(x!=0){
rem=x%10;
if (rem%2==0)
{
*ptr_even+=1;
}
else{*ptr_odd+=1;}
x/=10;
}
}
Output:
Enter a number.
3672
Q. NO. 7)
Source Code:
#include<stdio.h>
#include<conio.h>
int findLowest(int,int,int);
int findHighest(int,int,int);
int main(){
int num1,num2,num3;
int high,low;
printf("Enter three numbers. ");
scanf("%d%d%d",&num1,&num2,&num3);
high=findHighest(num1,num2,num3);
low=findLowest(num1,num2,num3);
printf("Highest value= %d\n",high);
printf("Lowest value= %d",low);
return 0;
}
int findHighest(int a,int b,int c){
if (a>b)
{
if(a>c)
{
return a;
}
else{
return c;
}
}
else{
if (b>c)
{
return b;
}
else{
return c;
}
}
}
int findLowest(int a,int b,int c){
if (a<b)
{
if(a<c)
{
return a;
}
else{
return c;
}
}
else{
if (b<c)
{
return b;
}
else{
return c;
}
}
}
Output:
Enter three numbers. 5
Highest value= 7
Lowest value= 4
Q. NO. 8a)
Source Code:
#include<stdio.h>
#include<conio.h>
int fact(int);
int main(){
float num;
int n;
X:
printf("Enter a number: ");
scanf("%f",&num);
if (num!=(int)num)
{
printf("Factorial of decimal number cannot be calculated. Please enter integ
er number.\n");
goto X;
}
if(num<0){
printf("Factorial of negative number is undefined. Please enter a positive i
nteger value.\n");
goto X;
}
n=(int)num;
printf("Factorial of %d = %d",n,fact(n));
return 0;
}
int fact(int n){
if (n==1 || n==0)
{
return 1;
}
else{
return n*fact(n-1);
}
}
Output:
Enter a number: 6
Factorial of 6 = 720
Q. NO. 8b)
Source Code:
#include<stdio.h>
#include<conio.h>
float power(float,float);
void main(){
float x,n,z;
printf("Enter value of x and n\n");
scanf("%f%f",&x,&n);
z=power(x,n);
printf("The value of %f^%f=%.3f",x,n,z);
getch();
}
float power(float a,float b){
if(b==1){
return a;
}
else if(b==0){
return 1;
}
else{
return a*power(a,b-1);
}
}
Output:
Enter value of x and n
3
Q. NO. 8c)
Source Code:
#include<stdio.h>
#include<conio.h>
int h=1;
int hcf(int,int);
void main(){
int num1,num2,HCF;
printf("Enter the numbers whose HCF is to be calculated.\n");
scanf("%d%d",&num1,&num2);
HCF=hcf(num1,num2);
printf("The HCF of %d and %d=%d",num1,num2,HCF);
getch();
}
int hcf(int a,int b){
int high,i;
if (a>b)
{
high=a;
}
else if (a<b){
high=b;
}
else{
return a;
}
for ( i = 2; i <=high; i++)
{
if (a%i==0 && b%i==0)
{
h*=i;
return hcf(a/i,b/i);
}
}
return h;
}
Output:
Enter the numbers whose HCF is to be calculated.
48
96
Q. NO. 8d)
Source Code:
#include<stdio.h>
#include<conio.h>
int sum(int);
int main(){
int n;
printf("Enter the value of n upto which you want the sum of.");
scanf("%d",&n);
printf("The sum of natural numbers from 1 to %d= %d",n,sum(n));
return 0;
}
int sum(int n){
if (n==1)
{
return 1;
}
else{
return n+sum(n-1);
}
}
Output:
Enter the value of n upto which you want the sum of.6
Q. NO. 9)
Source Code:
#include<stdio.h>
#include<conio.h>
int sum(int);
int main(){
int n;
printf("Enter the value of n upto which you want the sum of.");
scanf("%d",&n);
printf("The sum of numbers upto to %d= %d",n,sum(n));
return 0;
}
int sum(int n){
int sign=-1;
int i;
for ( i = 1; i <=n; i++)
{
sign*=-1;
}
if (n==1)
{
return 1;
}
else{
return ((sign)*n*n)+sum(n-1);
}
}
Output:
Enter the value of n upto which you want the sum of.7
Q. NO. 10)
Source Code:
#include<stdio.h>
#include<conio.h>
float power(float,int);
int factorial(int);
int main(){
float x,sum=0;
int n,sign,i;
sign=-1;
printf("Enter value of x\n");
scanf("%f",&x);
printf("Enter value of n\n");
scanf("%d",&n);
for ( i = 1; i <=((2*n)-1); i+=2)
{
sign*=-1;
sum+=(sign)*(power(x,i))/factorial(i);
}
printf("The value of sin%.2f=%.2f",x,sum);
return 0;
}
float power(float x,int n){
if (n==1)
{
return x;
}
else{
return x*(power(x,n-1));
}
}
int factorial(int n){
if (n==1)
{
return 1;
}
else{
return n*(factorial(n-1));
}
}
Output:
Enter value of x
Enter value of n
Conclusion
In conclusion, lab 7 made me clear of the concept of function like how we can use to
calculate different series and solve different mathematical problems with it. Moreover
it also made me clear how multiple lines of code, if suitable, can be shortened down
with the concept of function. Also this lab made me clear about recursive functions,
return type and non-return type functions.
Lab Report-8
Theory
Advantages of an array: Using an array have a lot of advantages.It enables us to store a large
data under a single variable name and the elements of an array is also easy to access.
Disadvantages of an array: The main disadvantage of using an array is that on;y data of similar
data type can be stored in an array.
One dimensional array: One dimensional array is the collection of data item in one row only.
Two dimensional array: Two dimensional array is the collection of data items in multiple rows
and columns.
1-D Array:
Q. NO. 1)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int i,num[6]={4,5,3,2,15};
for ( i = 0; i < 6; i++)
{
printf("%d ",num[i]);
}
return 0;
}
Output:
4 5 3 2 15 0
Q. NO. 2)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int i,num[6];
printf("Enter the members of an array");
for ( i = 0; i < 6; i++)
{
scanf("%d",&num[i]);
}
for ( i = 0; i < 6; i++)
{
printf("%d ",num[i]);
}
return 0;
}
Output:
Enter the members of an array3
345678
Q. NO. 3)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int i,num[5],sum=0;
printf("Enter the members of an array");
for ( i = 0; i < 5; i++)
{
scanf("%d",&num[i]);
if (num[i]%10==0 && num[i]%15!=0)
{
sum+=num[i];
}
}
printf("Sum= %d",sum);
return 0;
}
Output:
Enter the members of an array3
Sum= 0
Q. NO. 4)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int i,n;
printf("Enter the size of an array\n");
scanf("%d",&n);
int num1[n],num2[n],sum[n];
printf("Enter the members of first array\n");
for ( i = 0; i < n; i++)
{
scanf("%d",&num1[i]);
}
printf("Enter the members of second array\n");
for ( i = 0; i < n; i++)
{
scanf("%d",&num2[i]);
}
for ( i = 0; i < n; i++)
{
sum[i]=num1[i]+num2[i];
}
printf("A new array after adding corresponding elements looks like:\n");
for ( i = 0; i < n; i++)
{
printf("%d ",sum[i]);
}
return 0;
}
Output:
Enter the size of an array
2 4 6
Q. NO. 5)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int i,num[5],highest=0,lowest=0;
printf("Enter the members of an array\n");
for ( i = 0; i < 5; i++)
{
scanf("%d",&num[i]);
}
for ( i = 0; i < 5; i++)
{
if (num[i]>highest)
{
highest=num[i];
}
}
lowest=highest;
for ( i = 0; i < 5; i++)
{
if (num[i]<lowest)
{
lowest=num[i];
}
}
printf("Highest number among array element is %d and lowest element among array el
ement is %d\n",highest,lowest);
return 0;
}
Output:
Enter the members of an array
Highest number among array element is 4 and lowest element among array element is 1
Q. NO. 6)
Source Code:
#include<stdio.h>
#include<conio.h>
void sort_asc(int[],int);
void sort_dsc(int[],int);
int main(){
int i,n,choice;
printf("Enter the size of an array\n");
scanf("%d",&n);
int num[n];
printf("Enter the members of an array\n");
for ( i = 0; i < n; i++)
{
scanf("%d",&num[i]);
}
printf("Before sorting: ");
for ( i = 0; i < n; i++)
{
printf("%d ",num[i]);
}
X:
printf("In which order do you want to sort?\n1. Ascending order\n2.Descending order
");
scanf("%d",&choice);
switch(choice){
case 1: {
sort_asc(num,n);
printf("After sorting in ascending order: ");
for ( i = 0; i < n; i++)
{
printf("%d ",num[i]);
}
break;
}
case 2: {
sort_dsc(num,n);
printf("After sorting in ascending order: ");
for ( i = 0; i < n; i++)
{
printf("%d ",num[i]);
}
break;
}
default: {printf("Enter valid choices");goto X; break;}
}
return 0;
}
void sort_asc(int nu[], int n){
int temp=0;
for (int i = 0; i <n; i++)
{
for(int j = i+1;j<=n;j++){
if (nu[i]>nu[j])
{
temp=nu[i];
nu[i]=nu[j];
nu[j]=temp;
}
}
}
}
void sort_dsc(int nu[], int n){
int temp=0;
for (int i = 0; i <n; i++)
{
for(int j = i+1;j<=n;j++){
if (nu[i]<nu[j])
{
temp=nu[i];
nu[i]=nu[j];
nu[j]=temp;
}
}
}
}
Output:
Enter the size of an array
1. Ascending order
2.Descending order
3
1. Ascending order
2.Descending order2
Q. NO. 7)
Source Code:
#include<stdio.h>
#include<conio.h>
void sort_asc(int[],int);
void sort_dsc(int[],int);
int main(){
int i,n,choice;
printf("Enter the size of an array\n");
scanf("%d",&n);
int num[n],num_pow[n];
printf("Enter the members of an array\n");
for ( i = 0; i < n; i++)
{
scanf("%d",&num[i]);
}
for ( i = 0; i < n; i++)
{
num_pow[i]=(num[i])*(num[i])*(num[i]);
}
printf("\nBefore raising power: ");
for ( i = 0; i < n; i++)
{
printf("%d ",num[i]);
}
printf("\nAfter raising power: ");
for ( i = 0; i < n; i++)
{
printf("%d ",num_pow[i]);
}
return 0;
}
Output:
Enter the size of an array
Q. NO. 8)
Source Code:
#include<stdio.h>
#include<conio.h>
unsigned int arm(unsigned int[],unsigned int);
int main(){
unsigned int n;
printf("Enter the size of the array: ");
scanf("%u",&n);
int i;
unsigned int num[n],count=0;
printf("Enter the members of an array");
for ( i = 0; i < n; i++)
{
scanf("%u",&num[i]);
}
count=arm(num,n);
printf("There were %u Armstrong numbers among %u members.",count,n);
return 0;
}
unsigned int arm(unsigned int num[],unsigned int n)
{
int count=0;
for ( int i = 0; i < n; i++)
{
int x,rem,sum=0;
x=num[i];
while (x!=0)
{
rem=x%10;
sum+=(rem*rem*rem);
x/=10;
}
if (num[i]==sum)
{
count++;
}
}
return count;
}
Output:
21
34
Q. NO. 9a)
Source Code:
#include<stdio.h>
#include<conio.h>
void sort_asc(float[],int);
int main(){
float median;
int i,n,x;
printf("Enter the size of an array\n");
scanf("%d",&n);
float num[n];
printf("Enter the members of an array\n");
for ( i = 0; i < n; i++)
{
scanf("%f",&num[i]);
}
sort_asc(num,n); //sorting the array
if (n%2==1)
{
x=(n+1)/2;
median=num[x];
}
else
{
x=n/2;
median=(num[x]+num[x+1])/2;
}
printf("median=%f",median);
return 0;
}
void sort_asc(float nu[], int n){
int temp=0;
for (int i = 0; i <n; i++)
{
for(int j = i+1;j<=n;j++){
if (nu[i]>nu[j])
{
temp=nu[i];
nu[i]=nu[j];
nu[j]=temp;
}
}
}
}
Output:
12
13
14
median=13.000000
Q. NO. 9b)
Source Code:
#include<stdio.h>
#include<conio.h>
float highest(float[],int);
float lowest(float[],int);
int main(){
float range,high,low;
int i,n;
printf("Enter the size of an array\n");
scanf("%d",&n);
float num[n];
printf("Enter the members of an array\n");
for ( i = 0; i < n; i++)
{
scanf("%f",&num[i]);
}
high=highest(num,n);
//printf("%f\n",high);
low=lowest(num,n);
//printf("%f\n",low);
range=high-low;
printf("Range=%f",range);
return 0;
}
float highest(float nu[], int n){
float high=0;
for (int i = 0; i <n; i++)
{
if(nu[i]>high){
high=nu[i];
}
}
return high;
}
float lowest(float nu[], int n){
float low;
for (int i = 0; i <n; i++)
{
if(nu[i]<low){
low=nu[i];
}
}
return low;
}
Output:
Enter the size of an array
Range=3.000000
Q. NO. 9c)
Source Code:
#include<stdio.h>
#include<conio.h>
float sum_x_sqr(float[],int);
float sum_x(float[],int);
int main(){
int i,n;
printf("Enter the size of an array\n");
scanf("%d",&n);
float num[n],variance;
printf("Enter the members of an array\n");
for ( i = 0; i < n; i++)
{
scanf("%f",&num[i]);
}
float sumxsqr=sum_x_sqr(num,n),sumx=sum_x(num,n);
variance=(sumxsqr/(float)n)-((sumx/(float)n)*(sumx/(float)n));
printf("Variance=%f",variance);
return 0;
}
float sum_x_sqr(float nu[], int n){
float sum=0;
for (int i = 0; i <n; i++)
{
sum+=nu[i]*nu[i];
}
return sum;
}
float sum_x(float nu[], int n){
float sum=0;
for (int i = 0; i <n; i++)
{
sum+=nu[i];
}
return sum;
}
Output:
5
4
Variance=4.666668
Q. NO. 9d)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float sum_x_sqr(float[],int);
float sum_x(float[],int);
int main(){
int i,n;
printf("Enter the size of an array\n");
scanf("%d",&n);
float num[n],variance,sd;
printf("Enter the members of an array\n");
for ( i = 0; i < n; i++)
{
scanf("%f",&num[i]);
}
float sumxsqr=sum_x_sqr(num,n),sumx=sum_x(num,n);
variance=(sumxsqr/(float)n)-((sumx/(float)n)*(sumx/(float)n));
sd=sqrt(variance);
printf("Standard Deviation=%f",sd);
return 0;
}
float sum_x_sqr(float nu[], int n){
float sum=0;
for (int i = 0; i <n; i++)
{
sum+=nu[i]*nu[i];
}
return sum;
}
float sum_x(float nu[], int n){
float sum=0;
for (int i = 0; i <n; i++)
{
sum+=nu[i];
}
return sum;
}
Output:
Enter the size of an array
Standard Deviation=1.247220
Q. NO. 10)
Source Code:
#include<stdio.h>
#include<conio.h>
float highest(float[],int,int *);
float lowest(float[],int,int *,float);
int main(){
float sum,diff,high,low;
int i,n=5,h=0,l=0;
//printf("Enter the size of an array\n");
//scanf("%d",&n);
float num[n];
printf("Enter the members of an array\n");
for ( i = 0; i < n; i++)
{
scanf("%f",&num[i]);
}
high=highest(num,n,&h);
printf("Highest element of an array: %f\n",high);
if (h==1)
{
printf("It was: %d %s element\n",h,"st");
}
else if (h==2)
{
printf("It was: %d %s element\n",h,"nd");
}
else if (h==3)
{
printf("It was: %d %s element\n",h,"rd");
}
else
{
printf("It was: %d %s element\n",h,"th");
}
low=lowest(num,n,&l,high);
printf("Lowest element of an array: %f\n",low);
if (l==1)
{
printf("It was: %d %s element\n",l,"st");
}
else if (l==2)
{
printf("It was: %d %s element\n",l,"nd");
}
else if (l==3)
{
printf("It was: %d %s element\n",l,"rd");
}
else
{
printf("It was: %d %s element\n",l,"th");
}
sum=high+low;
printf("Sum of the highest and lowest member of the array: %f\n",sum);
diff=high-low;
printf("Difference between the highest and lowest member of the array: %f\n",diff);
return 0;
}
float highest(float nu[], int n,int *ptr_h){
float high=0;
for (int i = 0; i <n; i++)
{
if(nu[i]>high){
high=nu[i];
*ptr_h=i+1;
}
}
return high;
}
float lowest(float nu[], int n,int *ptr_l,float high){
float low=high;
for (int i = 0; i <n; i++)
{
if(nu[i]<low){
low=nu[i];
*ptr_l=i+1;
}
}
return low;
}
Output:
Enter the members of an array
24
12
36
15
It was: 4 th element
It was: 1 st element
Difference between the highest and lowest member of the array: 32.000000
2-D Array:
Q. NO. 1)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int i,j,num[2][2]={{4,5},{6,7}};
for ( i = 0; i < 2; i++)
{
for ( j = 0; j < 2; j++)
{
printf("%d ",num[i][j]);
}
}
return 0;
}
Output:
Q. NO. 2)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int i,j,num[3][3];
printf("ENter the members of the array");
for ( i = 0; i < 3; i++)
{
for ( j = 0; j < 3; j++)
{
scanf("%d",&num[i][j]);
}
}
for ( i = 0; i < 3; i++)
{
for ( j = 0; j < 3; j++)
{
printf("%d ",num[i][j]);
}
}
return 0;
}
Output:
Q. NO. 3)
Source Code:
//divisible by 7 but not by 5.
#include<stdio.h>
#include<conio.h>
int main(){
int num[3][3],sum=0;
printf("Enter the members of the aray");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d",&num[i][j]);
if (num[i][j]%7==0 && num[i][j]%5!=0)
{
sum+=num[i][j];
}
}
}
printf("Sum of those numbers who were divisible by 7 but not by 5 is: %d",sum)
;
return 0;
}
Output:
3
4
55
Q. NO. 4)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int i,j,m,n;
printf("Enter the row of the array\n");
scanf("%d",&m);
printf("Enter the column of the array\n");
scanf("%d",&n);
int num1[m][n],num2[m][n],sum[m][n];
printf("Enter the members of first array\n");
for ( i = 0; i < n; i++)
{
for( j = 0; j <n;j++){
scanf("%d",&num1[i][j]);
}
}
printf("Enter the members of second array\n");
for ( i = 0; i < n; i++)
{
for( j = 0; j <n;j++){
scanf("%d",&num2[i][j]);
}
}
for ( i = 0; i < n; i++)
{
for( j = 0; j <n;j++){
sum[i][j]=num1[i][j]+num2[i][j];
}
}
printf("A new array after adding corresponding elements looks like:\n");
for ( i = 0; i < n; i++)
{
for( j = 0; j <n;j++){
printf("%d ",sum[i][j]);
}
printf("\n");
}
return 0;
}
Output:
7
8
3 6 8
10 6 9
14 17 13
Q. NO. 5)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int i,j,m,n;
printf("Enter the row of the array\n");
scanf("%d",&m);
printf("Enter the column of the array\n");
scanf("%d",&n);
int num[m][n];
printf("Enter the members of an array\n");
for ( i = 0; i < n; i++)
{
for( j = 0; j <n;j++){
scanf("%d",&num[i][j]);
}
}
int highest=num[0][0] , lowest=num[0][0];
for ( i = 0; i < n; i++)
{
for( j = 0; j <n;j++){
if (num[i][j]>highest)
{
highest = num[i][j];
}
if (num[i][j]<lowest)
{
lowest = num[i][j];
}
}
}
printf("The highest element of the array is: %d, the lowest is: %d",highest,lowest
);
return 0;
}
Output:
Q. NO. 6)
Source Code:
#include<stdio.h>
#include<conio.h>
int sum(int,int [][100]);
int main(){
//We know diagonal elements are defined only for square matrices
int i,j,add,n;
printf("ENter the dimension of the square matrix");
scanf("%d",&n);
int num[n][n];
for ( i = 0; i < n; i++)
{
for ( j =0;j<n; j++)
{
scanf("%d",&num[i][j]);
}
}
add=sum(n,num);
printf("The sum of the diagonal elements is %d",add);
return 0;
}
int sum(int n,int mat[n][n]){
int add=0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++){
if (i==j)
{
add+=mat[i][j];
}
}
}
return add;
Output:
9
The sum of the diagonal elements is15
Q. NO. 7)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
int i,j,m,n;
printf("Enter the row of the array\n");
scanf("%d",&m);
printf("Enter the column of the array\n");
scanf("%d",&n);
int num[m][n];
long int power[m][n];
printf("Enter the members of an array\n");
for ( i = 0; i < n; i++)
{
for( j = 0; j <n;j++){
scanf("%d",&num[i][j]);
}
}
for ( i = 0; i < n; i++)
{
for( j = 0; j <n;j++){
power[m][n]=(long int)pow(num[m][n],5);
}
}
printf("After raising the power, the elemenrs are:\n");
for ( i = 0; i < n; i++)
{
for( j = 0; j <n;j++){
printf("%ld ",power[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter the row of the array
1 32
1 32
Q. NO. 8)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
int i,j,n=4;
int num[n][n];
for ( i = 0; i < n; i++)
{
for ( j = 0; j < n; j++)
{
num[i][j]=pow(3,-(i+j));
}
}
for ( i = 0; i < n; i++)
{
for ( j = 0; j < n; j++)
{
printf("%d",num[i][j]);
}
}
return 0;
}
Output:
Q. NO. 9)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
int i,j,m,n;
printf("Enter the row of the array\n");
scanf("%d",&m);
printf("Enter the column of the array\n");
scanf("%d",&n);
int num[m][n];
printf("Enter the members of an array\n");
for ( i = 0; i < m; i++)
{
for( j = 0; j <n;j++){
scanf("%d",&num[i][j]);
}
}
int sum[m]; //Coz the size of the matrix will be equL TO no. of rows. After addin
g one rows, we will get a member for the array.
int add=0;
for ( i = 0; i < m; i++)
{
for( j = 0; j <n;j++){
add+=num[i][j];
}
sum[i]=add;
add=0;
}
printf("The array after adding the rows: \n");
for ( i = 0; i < m; i++)
{
printf("%d ",sum[i]);
}
return 0;
}
Output:
5
6
6 15 24
Q. NO. 10)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int row1,col1,row2,col2;
float num1[100][100],num2[100][100];
float product[100][100];
void readMatrix();
void processMatrix();
void writeMatrix();
int main(){
readMatrix();
processMatrix();
writeMatrix();
return 0;
}
void readMatrix(){
X:
printf("Enter the row of first matrix");
scanf("%d",&row1);
printf("Enter the column of the first matrix");
scanf("%d",&row2);
printf("Enter the row of second matrix");
scanf("%d",&col1);
printf("Enter the column of the second matrix");
scanf("%d",&col2);
if(col1!=row2){
printf("Enter matrix in such a way that no. of column of of first matrix will be
equal to no. of row of second matrix");
goto X;
}
printf("Enter the elements of the first matrix:");
for (int i = 0; i < row1; i++)
{
for (int j =0;j<col1;j++){
scanf("%f",&num1[i][j]);
}
}
printf("Enter the elements of the second matrix:");
for (int i = 0; i < row2; i++)
{
for (int j =0;j<col2;j++){
scanf("%f",&num2[i][j]);
}
}
}
void processMatrix(){
int i,j,k;
float sum=0;
for ( i = 0; i < row1; i++)
{
for ( j = 0; j < col2; j++)
{
for ( k = 0; k < col1; k++)
{
sum+=(num1[i][k])*(num2[k][j]);
}
product[i][j]=sum;
sum=0;
}
}
}
void writeMatrix(){
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
printf("%.2f ",product[i][j]);
}
printf("\n");
}
}
Output:
Enter the row of first matrix1 3
9
30.00 36.00 42.00
Conclusion
In conclusion, lab 8 made me clear of the concept of arrays, how similar data types can
be stored in arrays. Also, I passed arrays elements to function and made some
calculations.
Lab Report-9
Background Theory:
What is a pointer?
A pointer is a variable which is used for storing address of a variable.
Why is it useful?
Pointers are useful for accessing memory locations. Pointers provide an efficient
way for accessing the elements of an array structure. Pointers are also useful in
changing the value of a local variable from another function using the concept of
pass by reference.
Pointer return type: In C, We can create a fuction which returns the variable of
pointer form.
Q. NO. 1a)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int a,b;
printf("The address of a=%u\n",&a);
printf("The address ofb=%u",&b);
return 0;
}
Output:
Q. NO. 1b)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
int *p,*q; //Declaring of pointer variable
int a,b; //Declaring ordinary variable
p=&a; //Using refrencing operator to initialize pointer variable p.
q=&a; //Using refrencing operator to initialize pointer variable q.
printf("The address of a=%u\n",&a);
printf("The address ofb=%u\n",&b);
printf("Value of p=%u\n",q);
printf("Value of q=%u\n",p);
printf("Enter the value of a and b");
scanf("%d%d",&a,&b);
printf("The value pointed by p is %d\n",*p);
printf("The value pointed by q is %d\n",*q);
printf("a+b=%d",a+b);
printf("*p+*q=%d",*p+*q); // *p+*q --> pointer expression
return 0;
}
Output:
Q. NO. 2)
Source Code:
#include<stdio.h>
#include<conio.h>
void larger(int *, int *,int *);
int main(){
int num1,num2; //Declaring ordinary variable
printf("ENter two number\n");
scanf("%d%d",&num1,&num2);
int c;
larger(&num1,&num2,&c);
if (c==1)
{
printf("%d is larger",num1);
}
else if (c==2)
{
printf("%d is larger",num2);
}
else{
printf("Both are equal");
}
return 0;
}
void larger(int *p,int *q,int *r){
if (*p>*q)
{
*r=1;
}
else if(*q>*p){
*r=2;
}
else{
*r=0;
}
}
Output:
ENter two number
5 is larger
Q. NO. 3)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
float marks[5];
int i;
printf("%d\n",marks);
printf("The address of array elements:\n");
for ( i = 0; i < 5; i++)
{
printf("The address of the element %d is %u\n",i,&marks[i]);
}
printf("\n");
printf("\n");
for ( i = 0; i < 5; i++)
{
printf("The address of the element %d is %u\n",i,(marks+i));
}
return 0;
}
Output:
The address of array elements:
Conclusion
Hence, Through Lab report, I got to clear my confusions regarding pointer variable
and debug my mistakes. And from now, I am more confident on writing and
Understand C -Program.
Lab Report-10
Background Theory:
Introduction to string:
In C programming, a string is a sequence/array of characters terminated with a
null character \0 . For example: char c[] = "c string"; When the compiler
encounters a sequence of characters enclosed in the double quotation marks, it
appends a null character \0 at the end by default.
String handeling function: These functions are defined under the header file
string.h .These functions are designed for strings.
1) strlen(): This function basically counts the length of the string.
Eg: If char ch[50]= “Kathmandu” and int len=stelen(ch) then the value of len
will be 9.
2) strcat(): This function helps us to concatenate two strings.
Syntax: strcat(destination string,source string)
Eg:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello ", str2[] = "everyone";
strcat(str1, str2);
puts(str1);
puts(str2);
return 0;
}
Output:
3) strcpy(): This function helps to copy the string.
Syntax: strcpy(destination string, source string)
Eg:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello ", str2[] = "everyone";
strcpy(str1, str2);
puts(str1);
puts(str2);
return 0;
}
Output:
1 if the first non-matching character in str1 is greater (in ASCII) than that
of str2 .
-1 if the first non-matching character in str1 is greater (in ASCII) than that
of str2 .
Two dimensional array in string: In c programming, we can actually create
two dimensional array of string. This will help us storing words in different
places. Application of it is we can extract word by word from a sentence and
perform the required task.
Pointer to string: We can create a pointer variable that can point to a string.
Actually, when we try to point towards a string, it basically points to the base
address of the string instead of pointing towards the whole string. But we can
actually retrieve all the characters of the string using the concept of pointer
arithematic.
Q. NO. 1)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
int i;
char name1[]="pokhara city";
char name2[]={'K','a','t','h','m','a','n','d','u',' ','c','i','t','y','\0'};
for ( i = 0; i < strlen(name1); i++)
{
printf("%c",name1[i]);
}
printf("\n");
for ( i = 0; i < strlen(name2); i++)
{
printf("%c",name2[i]);
}
return 0;
}
Output:
pokhara city
Kathmandu city
Q. NO. 2)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
int i,j=0;
char input[100],temp[100];
printf("Enter the string.");
scanf("%s",&input);
for ( i = strlen(input)-1; i>=0; i--)
{
temp[j]=input[i];
j++;
}
if (strcmp(input,temp)==0)
{
printf("The string is palindrome.");
}
else{
printf("The string is not palindrome");
}
return 0;
}
Output:
Enter the string.liril
Q. NO. 3)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int str_count(char *ptr);
int main(){
char input[100];
printf("ENter the string");
scanf("%s",input);
int length = str_count(input);
printf("The length of the string is %d",length);
return 0;
}
int str_count(char *ptr){
int i=0;
while (*ptr!='\0')
{
ptr++;
i++;
}
return i;
}
Output:
ENter the string Hello
Q. NO. 4)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int length[100];
int i=0,j=0,k=0,l=0,max_position,max=0,min_position,min,count_vowel=0,count_consonan
t=0;
char str2[100][100];
void largest(char []);
int main(){
char str[100];
printf("Enter the string");
gets(str);
largest(str);
printf("%s is the largest word in the string '%s'.",str2[max_position-1],str);
printf("This word holds %d position in the string.",max_position);
printf("There were %d vowels and %d consonants in the word
%s.",count_vowel,count_consonant, str2[max_position-1],);
return 0;
}
void largest(char str[]){
for ( i = 0; i < strlen(str); i++ )
{
if (str[i]==' ')
{
j=0;
k++;
}
else{
str2[k][j]=str[i];
j++;
}
}
for ( l = 0; l <= k; l++)
{
length[l]=strlen(str2[l]);
}
for ( l = 0; l <= k; l++)
{
if (length[l]>max)
{
max=length[l];
max_position=l+1;
}
}
for ( i = 0; i < strlen(str2[max_position-1]); i++ )
{
switch (str2[max_position-1][i])
{
case 'a': {count_vowel++;break;}
case 'e': {count_vowel++;break;}
case 'i': {count_vowel++;break;}
case 'o': {count_vowel++;break;}
case 'u': {count_vowel++;break;}
case 'A': {count_vowel++;break;}
case 'E': {count_vowel++;break;}
case 'I': {count_vowel++;break;}
case 'O': {count_vowel++;break;}
case 'U': {count_vowel++;break;}
default:{count_consonant++;break;}
}
}
}
Output:
Enter the string hello my name is javed ansari
ansari is the largest word in the string 'hello my name is javed ansari'.This word holds 6 position in the
string.There were 3 vowels and 3 consonants in the word ansari.
Q. NO. 5)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int x=0;
char rev(char [],int);
int main(){
char str[100];
printf("Enter the string");
gets(str);
int length = strlen(str);
rev(str,length);
return 0;
}
char rev(char str[],int len){
char temp;
temp = str[x];
str[x]=str[len-1];
str[len-1]=temp;
if (x<(len/2))
{
++x;
rev(str,--len);
}
else{
printf("%s",str);
}
}
Output:
Enter the string hello peter
retep olleh
Q. NO. 6)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
int len1,len2;
char str1[100],str2[100];
printf("Enter the string 1\n");
gets(str1);
printf("Enter the string 2\n");
gets(str2);
char str3[100],str4[100],str5[100];
len1=strlen(str1);
len2=strlen(str2);
printf("The length of the string 1 is %d\n",len1);
printf("The length of the string 2 is %d\n",len2);
strcat(str1,str2);
printf("After concatenating the string,str1 becomes '%s'\n",str1);
strcpy(str1,str2);
printf("After using strcpy the string,str1 becomes '%s'\n",str1);
printf("%d",strcmp(str1,str2)); //Note: strcmp(str1,str2) returns
// +1 if ascii value of str1>str2
// -1 if ascii value of str1<str2
// 0 if ascii value of str1=str2
}
Output:
Q. NO. 7)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
char st[100];
char st1[100];
int track=1;
printf("ENter the string ");
scanf("%s",st);
int n=strlen(st);
int initial=((n/2)+1);
for (int i = 0; i < n; i++)
{
track++;
if (i<n-1)
{
st1[i]=st[initial];
if (track%2==0)
{
initial-=track;
}
else{
initial+=track;
}
}
else{
st1[i]=st[(n/2)];
}
}
for (int i = 0; i < n; i++)
{
printf("%c",st1[i]);
}
return 0;
}
Output:
ENter the string NEPAL
AELNP
Q. NO. 8)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int i=0,j=0,k=0;
int main(){
int count=0;
char ch;
char str[100];
printf("ENter the string\n");
gets(str);
int len=strlen(str);
int asc;
printf("ENter the character whose frequency is to be determined.\n");
ch=getch(); //A------
>65
asc=ch; //
a------>97
for ( i = 0; i < len; i++)
{
int character=str[i];
if (asc==character)
{
count++;
}
}
printf("the frequency of the character %c = %d",ch,count);
return 0;
}
Output:
Enter the string.
Hello my name is javed ansari and i live in pepsicola
ENter the character whose frequency is to be determined. i
the frequency of the character i = 6
Q. NO. 9)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int i=0,j=0,k=0;
int main(){
int count[1000];
for ( i = 0; i < 1000; i++)
{
count[i]=0;
}
char str[100];
printf("ENter the string");
gets(str);
int len=strlen(str);
int asc; //
A------>65
//
a------>97
for ( i = 0; i < len; i++)
{
asc=str[i];
for ( j = 65; j <=90; j++)
{
if (asc==j)
{
count[j]++;
}
}
for ( j = 97; j <=122; j++)
{
if (asc==j)
{
count[j]++;
}
}
}
for ( j = 65; j <=90; j++)
{
if (count[j]!=0)
{
printf("%c count=%d\t",j,count[j]);
}
}
for ( j = 97; j <=122; j++)
{
if (count[j]!=0)
{
printf("%c count=%d\t",j,count[j]);
}
}
return 0;
}
Output:
ENter the string Hello my name is javed ansari and i live in pepsicola. i am currently studying in pulchowk
campus of engineering.
H count=1 a count=8 c count=4 d count=3 e count=9 f count=1 g count=3 h count=1
i count=11 j count=1 k count=1 l count=6 m count=4n count=10 o count=4 p count=4
r count=4 s count=5 t count=2 u count=4 v count=2 w count=1 y count=3
Q. NO. 10)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void change_letter(char [],int,int);
int main(){
char str[100];
printf("ENter the string\n");
gets(str);
int len=strlen(str);
int diff='a'-'A';
//A------>65
//a------>97
change_letter(str,len,diff);
return 0;
}
void change_letter(char str[],int length,int difference){
int asc;
for (int i = 0; i < length; i++)
{
asc=str[i];
if (asc>=97)
{
str[i]=asc-difference;
}
else{
str[i]=asc+difference;
}
}
printf("%s",str);
}
Output:
Q. NO. 11)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void sort(char [10][100]);
int main(){
char str[10][100];
printf("ENter the names\n");
for (int i = 0; i < 10; i++)
{
gets(str[i]);
}
sort(str);
printf("****************************************************************************
**************************************\n");
for (int i = 0; i < 10; i++)
{
puts(str[i]);
}
return 0;
}
void sort(char str[10][100]){
char temp[100];
for (int i = 0; i < 10; i++)
{
for (int j =i+1;j<10;j++){
if (strcmp(str[i],str[j])==1)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
}
}
Output:
ENter the names
Rohan
Karan
Ashish
Javed
Khagendra
Mandeep
Babin
Nishan
Saurav
Anoz
*****************************************************************************************
*************************
Anoz
Ashish
Babin
Javed
Karan
Khagendra
Mandeep
Nishan
Rohan
Saurav
Q. NO. 12)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
char str[10][100];
printf("Who is the prime minister of Nepal?\n");
printf("1. Sher Bahadur Deuba\n");
printf("2. KP Sharma Oli\n");
printf("3. Bidhya Devi Bhandari\n");
printf("4. Madhav Prashad Ghimire\n");
int ans;
int wrong_ans=0;
X:
if (wrong_ans==3)
{
printf("The correct answer of this question is option 2. i.e. KP Sharma Oli.\
n");
goto Y;
}
scanf("%d",&ans);
switch(ans){
case 1:{if (wrong_ans<2)
{
printf("Try again!\n");
}
wrong_ans++;goto X;break;}
case 2:{printf("Good\n");break;}
case 3:{if (wrong_ans<2){
printf("Try again!\n");
}
wrong_ans++;goto X;break;}
case 4:{if (wrong_ans<2){
printf("Try again!\n");}
wrong_ans++;goto X;break;}
default:{printf("Enter valid choice.\n");goto X;break;}
}
Y:
return 0;
}
Output:
Who is the prime minister of Nepal?
1. Sher Bahadur Deuba
2. KP Sharma Oli
3. Bidhya Devi Bhandari
4. Madhav Prashad Ghimire
4
Try again!
1
Try again!
2
Good
Analysis and Discussion
Here, during the lab report, i understood about string, how string is stored in the
variable, how string pre-defined function like strcmp, strlen works. Also I learned how
to sort strings in different orders.
Conclusion
Hence, Through Lab report, I got to clear my confusions regarding string and
debugged my mistakes. And from now, I am more confident on writing and
Understand C -Program.
Lab-report 11
Declaration of structure template:
struct structure_name {
DataType member1_name;
DataType member2_name;
DataType member3_name;
…
};
Structure pointer operator( ->): C provides the structure pointer operator -> to
access members of a structure via a pointer. It is typed on the keyboard as minus
sign followed by a greater than sign. If a pointer variable is assigned the address
of a structure, then a member of the structure can be accessed by:
Structure within structure: Like nested loop and nested if, we can have nested
structure. Here, a member of structure can be itself a structure. Eg: If we want to
store records of student, we can create another structure date of birth which
consists of day, month and year. We can access them using . operator twice . The
first dot operator will access the member of first layer structure, the second dot
operator will access the member of second layer and so on.
Eg:
Struct dob{
int day;
int month;
int year;
};
struct record{
char name[50];
int roll;
char sec;
struct dob date;
};
This was just the initialization part. How to access them? Let’s see.
struct student s1;
s1.name= “Ram”;
s1.roll=12;
s1.sec= ‘A’;
s1.date.day=1;
s1.date.month=10;
s1.date.year=1999;
In this way, we can access them.
Passing structures by value: This method sends the copy of the value rather
than actual variable. In this method, any change to structure members within the
function are not reflected in the original structure (in the calling function).
Passing structures by reference: This method sends the address of the
structure. In this method, any change to structure members within the function
are reflected in the original structure (in the calling function).
Q. NO. 1)
Source Code:
#include<stdio.h>
#include<conio.h>
struct employee{
char name[20];
char address[40];
long int telephone_number;
float salary;
};
int main(){
struct employee e[10000];
int x;
for (int i = 0; i < 10000; i++)
{
char ch;
printf("Enter name of employee %d:\t",i+1);
scanf("%s",e[i].name);
printf("Enter address of employee %d:\t",i+1);
scanf("%s",e[i].address);
printf("Enter telephone number of employee %d\t",i+1);
scanf("%ld",&e[i].telephone_number);
printf("Enter salary of employee %d\t",i+1);
scanf("%f",&e[i].salary);
printf("Do you want to enter more details?\t");
ch=getch();
printf("\n");
if (ch=='n' || ch=='N')
{
x=i;
i=10000;
}
}
for (int i = 0; i <=x; i++)
{
printf("Name of employee %d:\t%s\n",i+1,e[i].name);
printf("Address of employee %d:\t%s\n",i+1,e[i].address);
printf("Telephone number of employee %d:\t%ld\n",i+1,e[i].telephone_number);
printf("Salary of employee %d:\t%f\n",i+1,e[i].salary);
}
return 0;
}
Output:
Q. NO. 2)
Source Code:
#include<stdio.h>
#include<conio.h>
struct employee{
char name[40];
char telephone_number[40];
int salary;
};
int main(){
struct employee e[10];
for (int i = 0; i < 10; i++)
{
printf("Enter name of employee %d:\t",i+1);
gets(e[i].name);
printf("Enter telephone number of employee %d\t",i+1);
scanf("%s",e[i].telephone_number);
printf("Enter salary of employee %d\t",i+1);
scanf("%d",&e[i].salary);
}
int high,low;
int high_salary=e[0].salary,low_salary=e[0].salary;
int sum=e[0].salary;
for (int i = 1; i < 10; i++)
{
if (e[i].salary>high_salary)
{
high_salary=e[i].salary;
high=i;
}
if (e[i].salary<low_salary)
{
low_salary=e[i].salary;
low=i;
}
sum+=e[i].salary;
}
float avg=((float)sum)/10;
printf("Detail of the person whose salary is highest\n");
printf("Name: %s\n",e[high].name);
printf("Telephone number: %s\n",e[high].telephone_number);
printf("Salary: %d\n\n",e[high].salary);
printf("Detail of the person whose salary is lowest\n");
printf("Name: %s\n",e[low].name);
printf("Telephone number: %s\n",e[low].telephone_number);
printf("Salary: %d\n\n",e[low].salary);
printf("The average salary of the employees is %f",avg);
return 0;
}
Output:
Q. NO. 3)
Source Code:
#include<stdio.h>
#include<conio.h>
struct date{
int dd;
int mm;
int yyyy;
};
struct person{
char name[20];
char address[40];
char telephone_no[40];
struct date dob;
};
int main(){
struct person p[10];
for (int i = 0; i < 10; i++)
{
printf("Enter name of employee %d:\t",i+1);
scanf("%s",p[i].name);
printf("Enter address of employee %d:\t",i+1);
scanf("%s",p[i].address);
printf("Enter salary of employee %d:\t",i+1);
scanf("%s",p[i].telephone_no);
printf("Enter date of birth of employee %d in format (dd/mm/yyyy):\t",i+1);
scanf("%d",&p[i].dob.dd);
scanf("%d",&p[i].dob.mm);
scanf("%d",&p[i].dob.yyyy);
}
for (int i = 0; i < 10; i++)
{
printf("The name of employee %d:\t%s\n",i+1,p[i].name);
printf("The address of employee %d:\t%s\n",i+1,p[i].address);
printf("The salary of employee %d:\t%s\n",i+1,p[i].telephone_no);
printf("The date of birth of employee %d in format (dd/mm/yyyy):\t%d/%d/%d\
n",i+1,p[i].dob.dd,p[i].dob.mm,p[i].dob.yyyy);
}
return 0;
}
Output:
Source Code:
#include<stdio.h>
#include<conio.h>
struct TIME{
int hour;
int minutes;
int seconds;
};
void display(struct TIME,struct TIME) ;
int main(){
struct TIME startTime,stopTime;
printf("Enter start time.\n");
printf("Enter Hour");
scanf("%d",&startTime.hour);
printf("Enter minutes");
scanf("%d",&startTime.minutes);
printf("Enter seconds");
scanf("%d",&startTime.seconds);
printf("Enter stop time.\n");
printf("Enter Hour");
scanf("%d",&stopTime.hour);
printf("Enter minutes");
scanf("%d",&stopTime.minutes);
printf("Enter seconds");
scanf("%d",&stopTime.seconds);
display(startTime,stopTime);
return 0;
}
void display(struct TIME startTime, struct TIME stopTime) {
int sumhour=(startTime.hour)+(stopTime.hour);
int summinutes=(startTime.minutes)+(stopTime.minutes);
if (summinutes>=60)
{
sumhour+=(summinutes/60);
summinutes=summinutes%60;
}
int sumseconds=(startTime.seconds)+(stopTime.seconds);
if (sumseconds>=60)
{
summinutes+=(sumseconds/60);
sumseconds=sumseconds%60;
}
int diffhour=(stopTime.hour)-(startTime.hour);
if (stopTime.minutes<startTime.minutes)
{
diffhour--;
stopTime.minutes+=60;
}
int diffminutes=(stopTime.minutes)-(startTime.minutes);
if (stopTime.seconds<startTime.seconds)
{
diffminutes--;
stopTime.seconds+=60;
}
int diffseconds=(stopTime.seconds)-(startTime.seconds);
printf("Sum of the start time is %d:%d:%d\n",sumhour,summinutes,sumseconds);
printf("Difference between the start time and the stop time is %d:%d:%d\
n",diffhour,diffminutes,diffseconds);
}
Output:
Q. NO. 5)
Source Code:
#include<stdio.h>
#include<conio.h>
struct memory{
int KB;
int B;
int b;
};
void calculate(struct memory,struct memory,struct memory *,struct memory *);
int main(){
struct memory initial,final;
struct memory sum,diff;
struct memory *sumptr,*diffptr;
sumptr=∑
diffptr=&diff;
printf("Enter first memory.\n");
printf("Enter KiloBytes:\t");
scanf("%d",&initial.KB);
printf("Enter Bytes:\t");
scanf("%d",&initial.B);
printf("Enter Bits:\t");
scanf("%d",&initial.b);
printf("Enter second memory.\n");
printf("Enter KiloBytes:\t");
scanf("%d",&final.KB);
printf("Enter Bytes:\t");
scanf("%d",&final.B);
printf("Enter Bits:\t");
scanf("%d",&final.b);
calculate(initial,final,sumptr,diffptr);
printf("The sum of the memories= %d KB %dBytes and %d Bites\n",sum.KB,sum.B,sum.b);
printf("The difference of the memories= %d KB %dBytes and %d Bites\
n",sum.KB,sum.B,sum.b);
return 0;
}
void calculate(struct memory initial, struct memory final,struct memory *sumptr,stru
ct memory *diffptr) {
sumptr->KB=(initial.KB)+(final.KB);
sumptr->B=(initial.B)+(final.B);
if ((sumptr->B)>=1024)
{
(sumptr->KB)+=(sumptr->B)/1024;
(sumptr->B)=(sumptr->B)%1024;
}
sumptr->b=(initial.b)+(final.b);
if ((sumptr->b)>=8)
{
(sumptr->B)+=(sumptr->b)/8;
(sumptr->b)=(sumptr->b)%8;
}
if ((final.KB)>(initial.KB))
{
diffptr->KB=(final.KB)-(initial.KB);
if((final.B)<(initial.B)){
(diffptr->KB)--;
final.B+=1024;
}
diffptr->B==(final.B)-(initial.B);
if((final.b)<(initial.b)){
(diffptr->B)--;
final.b+=8;
}
diffptr->b=(final.b)-(initial.b);
}
else if ((initial.KB)>(final.KB))
{
diffptr->KB=(initial.KB)-(final.KB);
if((initial.B)<(final.B)){
(diffptr->KB)--;
initial.B+=1024;
}
diffptr->B=(initial.B)-(final.B);
if((initial.b)<(final.b)){
(diffptr->B)--;
initial.b+=8;
}
diffptr->b=(initial.b)-(final.b);
}
else
{
diffptr->KB=(final.KB)-(initial.KB);
if((final.B)<(initial.B)){
diffptr->B=(initial.B)-(final.B);
if((initial.b)<(final.b)){
(diffptr->B)--;
initial.b+=8;
}
diffptr->b=(initial.b)-(final.b);
}
else if((final.B)>(initial.B)){
diffptr->B=(final.B)-(initial.B);
if((final.b)<(initial.b)){
(diffptr->B)--;
final.b+=8;
}
diffptr->b=(final.b)-(initial.b);
}
else{
diffptr->B=(final.B)-(initial.B);
if((final.b)>(initial.b)){
diffptr->b=(final.b)-(initial.b);
}
else{
diffptr->b=(initial.b)-(final.b);
}
}
}
}
Output:
Conclusion
Hence, Through Lab report, I got to clear my confusions regarding structureand
debugged my mistakes. And from now, I am more confident on writing and
Understand C -Program.
Lab-report 12
Background theory:
What is a file?
A file is an object on a computer that stores data, information, settings, or
commands used with a computer program. On a computer there are three types of
files, application files, data files, and system files.
Types of files in C: There are 2 kinds of files in which data can be stored in 2
ways either in characters coded in their ASCII character set or in binary format.
They are
1. Text Files.
2. Binary Files
File operations in C:
1.w(write):
This mode opens new file on the disk for writing. If the file exist,disk for writing. If the
file exist, then it will be over written without then it will be over written without any
confirmation.
SYNTAX:
fp=fopen("data.txt","w");
"data.txt" is filename
"w" is writemode.
2. r (read)
This mode opens an preexisting file for reading.If the file doesn’t Exist then the
compiler returns a NULL to the file pointer
SYNTAX:
fp=fopen("data.txt","r");
fp=fopen("data.txt","w+");
4.a(append)
This mode opens a preexisting file for appending the data.
SYNTAX:
fp=fopen("data.txt","a");
5.a+(append+read)
the end of the file.
SYNTAX:
fp=fopen("data.txt","a+");
What is buffer?
A section of Random Access Memory (RAM) reserved for temporary storage of
data waiting to be directed to a device.
Q. NO. 1)
Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
FILE *fp;
fp = fopen("leap_year.txt","w");
if (fp==NULL)
{
printf("Error!");
}
else{
printf("Enter the year");
int year;
scanf("%d",&year);
if (year%4==0)
{
if (year%100!=0 || year%400==0)
{
fprintf(fp,"%d is a leap year.",year);
}
}
}
fclose(fp);
return 0;
}
Output:
Q. NO. 2)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
FILE *fp;
fp = fopen("new.txt","w");
if (fp==NULL)
{
printf("Error!");
}
else{
while(1){
char ch[100];
printf("Enter the word:\t");
gets(ch);
if((strcmp(ch, "no"))==0 ||(strcmp(ch, "NO"))==0){
break;
}
int check=0;
for (int i = 0; i < strlen(ch); i++)
{
switch(ch[i]){
case 'A':{check=1; break;}
case 'E':{check=1; break;}
case 'I':{check=1; break;}
case 'O':{check=1; break;}
case 'U':{check=1; break;}
case 'a':{check=1; break;}
case 'e':{check=1; break;}
case 'i':{check=1; break;}
case 'o':{check=1; break;}
case 'u':{check=1; break;}
}
}
if (check==0)
{
fprintf(fp,"%s\t",ch);
}
}
}
fclose(fp);
return 0;
}
Output:
Enter the word lifa
lsd
konka
ny
my
vivo
oppo
samsung
Q. NO. 3)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct student{
int roll;
char name[100];
char address[100];
char phone_number[100];
}info;
int main(){
FILE *fp;
fp = fopen("student_record.txt","w");
if (fp==NULL)
{
printf("Error!");
}
else{
info stu[100];
int count=0;
while(1){
printf("Enter the roll no. of the student %d:\t",count+1);
scanf("%d",&stu[count].roll);
fprintf(fp,"Roll no. of student %d:\t%d\n",count+1,stu[count].roll);
printf("Enter the name of the student %d:\t",count+1);
gets(stu[count].name);
fprintf(fp,"Name of student %d:\t%s\n",count+1,stu[count].name);
printf("Enter the address of the student %d:\t",count+1);
gets(stu[count].address);
fprintf(fp,"Address of student %d:\t%s\n",count+1,stu[count].address);
printf("Enter the Phone number of the student %d:\t",count+1);
gets(stu[count].phone_number);
fprintf(fp,"Phone number of student %d:\t%s\
n",count+1,stu[count].phone_number);
fprintf(fp,"****************************************************************
****************************************");
count++;
char ch[100];
printf("Do you want to enter more records?(Yes/No)");
gets(ch);
if((strcmp(ch, "no"))==0 ||(strcmp(ch, "NO"))==0||(strcmp(ch, "No"))==0){
break;
}
}
}
fclose(fp);
FILE *fpt;
fpt=fopen("student_record.txt","r") ;
char ch=fgetc(fpt);
while(ch!=EOF){
printf("%c",ch);
ch=fgetc(fpt);
}
return 0;
}
Output:
Q. NO. 4)
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct student{
char name[100];
int roll;
char address[100];
char telephone_number[100];
int score;
}info;
int main(){
FILE *fp;
fp = fopen("first.txt","w");
if (fp==NULL)
{
printf("Error!");
}
else{
info stu[100];
int count=0;
while(1){
printf("Enter the name of the student %d:\t",count+1);
scanf("%s",stu[count].name);
fprintf(fp,"Name of student %d:\t%s\n",count+1,stu[count].name);
printf("Enter the roll no. of the student %d:\t",count+1);
scanf("%d",&stu[count].roll);
fprintf(fp,"Roll no. of student %d:\t%d\n",count+1,stu[count].roll);
printf("Enter the address of the student %d:\t",count+1);
scanf("%s",stu[count].address);
fprintf(fp,"Address of student %d:\t%s\n",count+1,stu[count].address);
printf("Enter the telephone number of the student %d:\t",count+1);
scanf("%s",stu[count].telephone_number);
fprintf(fp,"telephone number of student %d:\t%s\
n",count+1,stu[count].telephone_number);
printf("Enter score of the student %d:\t",count+1);
scanf("%d",&stu[count].score);
fprintf(fp,"Score of student %d:\t%d\n",count+1,stu[count].score);
fprintf(fp,"*****************************************************************
***************************************");
count++;
char ch;
printf("Do you want to enter more records?(Y/N)");
ch=getchar();
if(ch=='n' || ch=='N'){
break;
}
}
}
fclose(fp);
FILE *fptr_read;
FILE *fptr_write;
fptr_read=fopen("first.txt","r") ;
fptr_write=fopen("second.txt","w");
char ch=fgetc(fptr_read);
while(ch!=EOF){
fprintf(fptr_write,"%c",ch);
ch=fgetc(fptr_read);
}
fclose(fptr_write);
fclose(fptr_read);
FILE *read;
read=fopen("second.txt","r") ;
char c=fgetc(read);
while(c!=EOF){
printf("%c",c);
}
return 0;
}
Output:
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct student{
char name[100];
int roll;
char address[100];
char telephone_number[100];
int score;
}info;
int main(){
FILE *fp;
fp = fopen("first.txt","wb");
if (fp==NULL)
{
printf("Error!");
}
else{
info stu[100];
int count=0;
int ch;
while(ch!=0){
printf("Enter the name of the student %d:\t",count+1);
scanf("%s",stu[count].name);
fprintf(fp,"Name of student %d:\t%s\n",count+1,stu[count].name);
printf("Enter the roll no. of the student %d:\t",count+1);
scanf("%d",&stu[count].roll);
fprintf(fp,"Roll no. of student %d:\t%d\n",count+1,stu[count].roll);
printf("Enter the address of the student %d:\t",count+1);
scanf("%s",stu[count].address);
fprintf(fp,"Address of student %d:\t%s\n",count+1,stu[count].address);
printf("Enter the telephone number of the student %d:\t",count+1);
scanf("%s",stu[count].telephone_number);
fprintf(fp,"telephone number of student %d:\t%s\
n",count+1,stu[count].telephone_number);
printf("Enter score of the student %d:\t",count+1);
scanf("%d",&stu[count].score);
fprintf(fp,"Score of student %d:\t%d\n",count+1,stu[count].score);
fprintf(fp,"*****************************************************************
***************************************");
count++;
printf("Do you want to enter more records?(1/0)");
scanf("%d",&ch);
}
}
fclose(fp);
FILE *fptr_read;
FILE *fptr_write;
fptr_read=fopen("first.txt","rb") ;
fptr_write=fopen("second.txt","wb");
char ch=fgetc(fptr_read);
while(ch!=EOF){
fprintf(fptr_write,"%c",ch);
ch=fgetc(fptr_read);
}
fclose(fptr_write);
fclose(fptr_read);
FILE *read;
read=fopen("second.txt","rb") ;
char c=fgetc(read);
while(c!=EOF){
printf("%c",c);
}
return 0;
}
Conclusion
Hence, Through Lab report, I got to clear my confusions regarding file handeling and
debugged my mistakes. And from now, I am more confident on writing and
Understand C -Program.