0% found this document useful (0 votes)
102 views8 pages

Computer Programming Basics 2065

The document discusses fundamentals of computer programming and provides solutions to programming problems involving: 1) Drawing a flowchart and writing an algorithm to find the largest of three numbers 2) Calculating values of variable 'a' based on given mathematical expressions 3) Writing a program to calculate monthly interest charged for installments on a cassette player 4) Writing a program using a for loop to compute the sum of squares of given numbers 5) Writing a program to obtain the product of two matrices 6) Writing a function to perform addition, subtraction, multiplication and division of complex numbers 7) Writing a program to read a line and delete all occurrences of the word "that" from it.

Uploaded by

Bishal Chapagain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views8 pages

Computer Programming Basics 2065

The document discusses fundamentals of computer programming and provides solutions to programming problems involving: 1) Drawing a flowchart and writing an algorithm to find the largest of three numbers 2) Calculating values of variable 'a' based on given mathematical expressions 3) Writing a program to calculate monthly interest charged for installments on a cassette player 4) Writing a program using a for loop to compute the sum of squares of given numbers 5) Writing a program to obtain the product of two matrices 6) Writing a function to perform addition, subtraction, multiplication and division of complex numbers 7) Writing a program to read a line and delete all occurrences of the word "that" from it.

Uploaded by

Bishal Chapagain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Subject: Fundamental of Computer Programming Year: 2065

1. Draw the flow chart for finding largest of three numbers and write an algorithm and
explain it.
Ans:
Algorithm
Step 1: Start
Step 2: Declare variables a, b, c
Step 3: Read values of a, b, c
Step 4: if a > b
If a > c
Print a is the greatest number
Else
Print c is the greatest number
Else
If b > c
Print b is the greatest number
Else
Print c is the greatest number
Step 5: Stop

start

Declare variables a,b,c

Read a,b,c

Is
a>b
?

Is Is
b>c a>c
? ?

a l
Print c Print c Print c

e p
T N
End
S I
C
Source: www.csitnepal.com
2. Find the value of “a” in each of the following statements.
int i=3 , j=4, k=8;
float a =4.5, b = 6.5, c = 3.5;
(a) a = b –i/k+ c/k
(b) a = (b-k)/j + (j+c)/k
(c) a=c-((i+j)/(k+i))*b
(d) a=c –i+j/k+i*b
(e) a=c+j%2+b
(f) a= (b+1)%(c+1)

Ans:
A) a = b-i/k+c/k
= 6.5-3/8+3.5/8
= 6.5-0+0.4375
= 6.9375
B) a = (b-k)/j+(j+c)/k
= (6.5-8)/4+(4+3.5)/8
= -0.375+0.9375
= 0.5625
C) a = c-((i+j)/(k+i))*b
= 3.5-((3+4)/(8+3))*6.5
= 3.5-0*6.5
= 3.5
D) a = c-i+j/k+i*b
= 3.5-3+4/8+3 *6.5
= 3.5-3+0+19.5
= 0.5 + 19.5
= 20
E) a = c+j%2+b
= 3.5+4%2+6.5
= 3.5+0+6.5
= 10
F) a = (b+1)%(c+1)
= (6.5+1)%(3.5+1)
= 7.5%4.5
= Not valid
3. Write a program for the interest charged in installments for following case. A
cassette player costs Rs. 2000. A shopkeeper sells it for Rs. 100 down payment and Rs.
100 for 21 more months. What is the monthly interest Charged?

#include<stdio.h>
a l
#include<conio.h>
e p
void main()
{
T N
S I
C
Source: www.csitnepal.com
float c_price,t_price,t_interest,m_interest; //cost price, total price, total interest,
monthly interest
c_price=2000;
t_price=100+(100*21);//down payment Rs. 100 and Rs. 100 for 21 installments
t_interest=(t_price-c_price)/c_price*100;
m_interest=t_interest/22;//payment is for 22 months
printf("\nThe monthly interest charged is %.2f",m_interest); //result is 0.45% per
month
getch();
}

4. Write a program that uses a “for” loop to compute and prints the sum of a given numbers
of squares.
Ans:

#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, sum=0;
printf(“Enter the number:”);
scanf(“%d”,&n);
for(i=1;i<n;i++)
{
sum=i*i+sum;
}
printf(“\n Sum=%d”,sum);
getch();
}

5. Write a program to obtain the product of the following matrices and explain it;
Ans:

#include<stdio.h>
#include<conio.h>
void main
{
int a[3][3]={{3,5,7},{2,-3,4,{4,5,2}};
int b[3][2]={{7,6},{6,-5},{4,3}};
int i,j,k,c[3][2];
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=0;
a l
for(k=0;k<3;k++)
e p
N
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}

S I T
C
Source: www.csitnepal.com
}
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“%d”,c[i][j]);
}
printf(“\n”);
}
getch();
}

6. Write a function to add, subtract, multiply and divide two complex numbers(x+iy) and
(c+id).
Ans:

#include<stdio.h>
#include<conio.h>
void add(int,int,int,int);
void sub(int,int,int,int);
void mul(int,int,int,int);
void div(int,int,int,int);

void main()
{
Int x,y,c,d,I;
printf(“Enter real and imaginary part1:”);
scanf(“%d%d”,&x,&y);
printf(“Enter real and imaginary part2:”);
scanf(“%d%d”,&c,&d);
add(x,y,c,d);
sub(x,y,c,d);
mul(x,y,c,d);
div(x,y,c,d);
getch();
}
void add(int r1,int ip1, int r2,int ip2)
{
int realsum,imgsum;
realsum=r1+r2;
imgsum=ip1+ip2;
printf(“The sum of complex numbers=%d+i(%d)\n”,realsum,imgsum);
}
void sub(int r1,int ip1, int r2,int ip2)
{
a l
int realsub,imgsub;
realsub=r1-r2;
e p
imgsub=ip1-ip2;

T N
printf(“The subtraction of complex numbers=%d+i(%d)\n”,realsub,imgsub);
}

S I
C
Source: www.csitnepal.com
void mul(int r1,int ip1, int r2,int ip2)
{
int realmul,imgmul;
realmul=r1*r2;
imgmul=ip1*ip2;
imgmul1=r1*ip2;
imgmul2=r2*ip1;
printf(“The multiplication of complex numbers=
%d+i(%d+%d)%d\n”,realmul,imgmul1,imgmul2,imgmul);
}

void add(int r1,int ip1, int r2,int ip2)


{
int realsum,imgsum;
realsum=r1+r2;
imgsum=ip1+ip2;
printf(“The sum of complex numbers=%d+i(%d)\n”,realsum,imgsum);
}

7. Write a program which will read a line and delete from it all occurrences of the
word “that”.
Ans:

#include<stdio.h>
#include<conio.h>
void main()
{
char str[]=”I that love c that programming.”;
int i;
for(i=0;str[i]!=’\0′;i++)
{

if(str[i]==116&&str[i+1]==104&&str[i+2]==97&&str[i+3]==116&&str[i+4]==32)
{
for(;str[i+5]!=’\0′;i++)
str[i]=str[i+5];
str[i]=’\0′;
i=0;
}
}
printf(“%s”,str);
getch();
}

a l
e p
8. What is a pointer and explain its applications? Write a program that uses pointers to copy
an array of double.
Ans:
T N
S I
C
Source: www.csitnepal.com
A pointer is a variable, which contains the address of another variable in memory. We can
have a pointer to any variable type pointers are said to “point to “the variable whose
reference they store.
#include<stdio.h>
#include<conio.h>
double *copy(double a[],int n);
void main()
{
double a[10],*b;
int i;
for(i=0;i<10;i++)
{
scanf("%lf",&a[i]);
}
b=copy(a,10);
for(i=0;i<10;i++)
{
printf("\n%lf",b[i]);
}
getch();
}
double *copy(double a[], int n)
{
double *p;
int i;
for(i=0;i<10;i++)
{
p[i]=a[i];
}
return p;
}

9. Define a structure of employee having data members name, address, age, and salary. Take
data for n employee in an array dynamically and find the average salary.
Ans:

#include <stdio.h>
#include<conio.h>
#include<alloc.h>
struct employee
{
char name[50];
a l
char add[50];
int age;
e p
int salary;

T N
};

S I
C
Source: www.csitnepal.com
void main()
{
struct employee *p;
int i, n;
float avg,sum=0;
printf("Enter the number of employee:");
scanf("%d", &n);
p=(struct employee *) calloc (n,sizeof(struct employee));
if(p==NULL)
exit();
for(i=0;i<n;i++)
{
fflush(stdin);
printf("Enter name:");
gets((p+1)->name);
printf("Enter address:");
gets((p+i)->add);
printf("Enter age:");
scanf("%d",&(p+i)->age);
fflush(stdin);
printf("Enter salary:");
scanf("%d",&(p+i)->salary);
}
for(i=0;i<n;i++)
{
sum=sum+(p+i)->salary;
}
avg=sum/n;
printf("Average salary=%f",avg);
getch();
}
10. Given a text file, create another text file deleting the following words “three”,
“bad”, and “time”.
OR
Why do you require graphical function? Explain the basic graphical function with
suitable program.
Ans:

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char str;
fp=fopen("old.txt","r+");
while(fscanf(fp,"%s",str)!=EOF)
a l
{

e
if(strcmp(str,"three")==0||strcmp(str,"bad")==0||strcmp(str,"time")==0) p
{
printf("");
T N
}
S I
C
Source: www.csitnepal.com
else
printf("%s ",str);
}
fclose(fp);
getch();
}

OR

Graphical function in c is used to implement various graphical symbols and shapes. There are
various types of graphics function used. Such as
a. Line(x1,y1,x2,y2)

This function is to draw line from (x1,y1) to (x2,y2)

b. Circle(x,y,r)

This function is to draw circle of radius r and center (x,y)

c. arc(int x, int y, int stangle, int endangle, int radius);

arc function is used to draw an arc with center (x,y) and stangle specifies starting
angle, endangle specifies the end angle and last parameter specifies the radius of
the arc. arc function can also be used to draw a circle but for that starting angle and
end angle should be 0 and 360 respectively.

a l
e p
T N
S I
C
Source: www.csitnepal.com

You might also like