Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views21 pages

3 Submission

Uploaded by

dihanjuhud21
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)
5 views21 pages

3 Submission

Uploaded by

dihanjuhud21
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/ 21

Objective:

1. To be proficient in using Operators and Expressions in C language


2. To learn how to use different kinds of Operators
3. To be familiar with the use of Operators in different kinds of problem
4. To solve different types of problems seamlessly using Operators in C

• Problem-1: Write a program to convert a given number of days into months and days.

Flowchart :
Start Calculate

𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷 = 𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷 − (𝑀𝑀𝑀𝑀𝑀𝑀𝑀𝑀ℎ𝑠𝑠 × 30)

Take number of
days as input Print Months
and Days

Calculate
𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷 End
𝑀𝑀𝑀𝑀𝑀𝑀𝑀𝑀ℎ𝑠𝑠 =
30

Code:
#include<stdio.h>
int main(){
printf("Name: Amit Kumar Dey\nID: 2102033\n\n");
int months,days;
printf("Enter Days\n");
scanf("%d",&days);
months = days/30;
days = days%30;
printf("Months=%d\nDays= %d",months,days);
return 0;
}

1|Page
Output:

• Problem-2: Write a program to display sequence of squares of numbers.


Flowchart :

Start

Initialize a=2

No
A<1000

Yes
End

Print a

Initialize
𝑎𝑎 = 𝑎𝑎2

Code:
#define N 10000
#define A 2
#include<stdio.h>
int main(){

2|Page
printf("Name: Amit Kumar Dey\nID: 2102033\n\n");
int a;
a=A;
while(a<N){
printf("%d\n",a);
a*=a;
}
return 0;
}
Output:

3|Page
• Problem-3: Write a program to employ different kinds of operator.
Flowchart :

𝑎𝑎
Start Print 𝑏𝑏 remainder
a�er calcula�ng

Initialize
Print 𝑎𝑎 = 𝑎𝑎 × 𝑏𝑏
𝑎𝑎 = 15 , 𝑏𝑏 = 10
a�er

Increment a by 1
No
c>d Print 0

Calculate Yes

𝑐𝑐 = 𝑎𝑎 − 𝑏𝑏 Print 1

Print a, b, c

Calculate No
c<d Print 0
𝑑𝑑 = 𝑏𝑏 + 𝑎𝑎
Yes

Increment b by 1 Print 1

Print a, b, d

Print a/b a�er End


calcula�ng

4|Page
Code:
#include<stdio.h>
int main(){
printf("Name: Amit Kumar Dey\nID: 2102033\n\n");
int a,b,c,d;
a = 15;
b = 10;
c = ++a - b;
printf("a = %d b = %d c = %d\n",a,b,c);
d = b++ + a;
printf("a = %d b = %d d = %d\n",a,b,d);
printf("a/b = %d\n",a/b);
printf("a%%b = %d\n",a%b);
printf("a *= b = %d\n",a*=b);
printf("%d\n",(c>d)?1:0);
printf("%d\n",(c<d)?1:0);
return 0;
}
Output:

5|Page
1
• Problem-4: Write a program to evaluate the equation ∑𝑛𝑛
𝑖𝑖=1 𝑖𝑖

Flowchart :

Start

Initialize
𝑠𝑠𝑠𝑠𝑠𝑠 = 0

Initialize
𝑛𝑛 = 1

No
N<10 End

Yes

Calculate
1
𝑠𝑠𝑠𝑠𝑠𝑠 = 𝑠𝑠𝑠𝑠𝑠𝑠 +
𝑛𝑛

Print n and
sum

Increment n by 1

6|Page
Code:
#include<stdio.h>
int main(){
printf("Name: Amit Kumar Dey\nID: 2102033\n\n");
float sum=0;
for(int n=1;n<=10;++n){
sum = sum + 1/ (float)n;
printf("%2d %6.4f\n",n,sum);
}
return 0;
}
Output:

7|Page
• Problem-5: Write a program to find maximum between three numbers entered by the
user.
Flowchart :

Start

Take a, b, c three
number as input

𝑎𝑎 ≥ 𝑏𝑏 No 𝑏𝑏 ≥ 𝑎𝑎 No Print maximum
𝑎𝑎𝑎𝑎𝑎𝑎 𝑎𝑎 ≥ 𝑐𝑐 𝑎𝑎𝑎𝑎𝑎𝑎 𝑏𝑏 ≥ 𝑐𝑐 number is b

Yes Yes

Print maximum Print maximum


number is a number is b

End

8|Page
Code:
#include<stdio.h>
int main(){
printf("Name: Amit Kumar Dey\nID: 2102033\n\n");
int a,b,c;
printf("Enter three numbers:\n");
scanf("%d%d%d",&a,&b,&c);
if(a>=b && a>=c){
printf("Maximum Number: %d",a);
} else if(b>=a && b>=c){
printf("Maximum Number: %d",b);
} else {
printf("Maximum Number: %d",c);
}
return 0;
}
Output:

9|Page
• Problem-6: Write a program to check whether a number is negative, positive or zero.
Flowchart :

Start

Take a number a
as input

No No
𝑎𝑎 > 0 𝑎𝑎 < 0 Print Zero

Yes Yes

Print Posi�ve Print Nega�ve

End
Code:
#include<stdio.h>
int main(){
printf("Name: Amit Kumar Dey\nID: 2102033\n\n");
int a;
printf("Enter one number:\n");
scanf("%d",&a);
if(a>0){

10 | P a g e
printf("Positive");
} else if(a<0){
printf("Negative");
} else{
printf("Zero");
}
return 0;
}
Output:

• Problem-7: Write a program to print the size of various data types.


Flowchart :

Start Print the


size of float

Print the
size of char Print the size
of double

Print the
size of int End

11 | P a g e
Code:
#include<stdio.h>
int main(){
printf("Name: Amit Kumar Dey\nID: 2102033\n\n");
printf("Size of char is : %d\n",sizeof(char));
printf("Size of int is : %d\n",sizeof(int));
printf("Size of float is : %d\n",sizeof(float));
printf("Size of double is : %d\n",sizeof(double));
return 0;
}
Output:

12 | P a g e
• Problem-8: write a program to read three values from keyboard and print out the largest
of them without using if statement.
Flowchart :
Start

Take n1, n2, n3


three number as
input

𝑛𝑛1 ≥ 𝑛𝑛2 No
Ini�alize 𝑚𝑚𝑚𝑚𝑚𝑚 = 𝑚𝑚𝑚𝑚𝑚𝑚
𝑎𝑎𝑎𝑎𝑎𝑎 𝑛𝑛1 ≥ 𝑛𝑛3

Yes

Ini�alize 𝑚𝑚𝑚𝑚𝑚𝑚 = 𝑛𝑛1

𝑛𝑛2 ≥ 𝑛𝑛1 No
Ini�alize 𝑚𝑚𝑚𝑚𝑚𝑚 = 𝑚𝑚𝑚𝑚𝑚𝑚
𝑎𝑎𝑎𝑎𝑎𝑎 𝑛𝑛2 ≥ 𝑛𝑛3

Yes
𝑛𝑛2 ≥ 𝑛𝑛1
Ini�alize 𝑚𝑚𝑚𝑚𝑚𝑚 = 𝑚𝑚𝑚𝑚𝑚𝑚
𝑎𝑎𝑎𝑎𝑎𝑎 𝑛𝑛2 ≥ 𝑛𝑛3
Ini�alize 𝑚𝑚𝑚𝑚𝑚𝑚 = 𝑛𝑛2

Ini�alize 𝑚𝑚𝑚𝑚𝑚𝑚 = 𝑛𝑛2

End

13 | P a g e
Code:
#include<stdio.h>
int main(){
printf("Name: Amit Kumar Dey\nID: 2102033\n\n");
int n1,n2,n3,max;
printf("Enter three numbers: ");
scanf("%d%d%d",&n1,&n2,&n3);
max = n1>=n2 && n1>=n3?n1:max;
max = n2>=n1 && n2>=n3?n2:max;
max = n3>=n2 && n3>=n1?n3:max;
printf("The largest number is: %d",max);
return 0;
}
Output:

14 | P a g e
• Problem-9: Write a program to read two integer values m and n and to decide and print
whether m is a multiple of n.
Flowchart :

Start

Take two number


m and n as input

Remainder No Print m is not


𝑛𝑛
of 𝑚𝑚 = 0 a mul�ple of n

Yes

Print m is a
mul�ple of n

End

Code:
#include<stdio.h>
int main(){
printf("Name: Amit Kumar Dey\nID: 2102033\n\n");
int m,n;
printf("Enter m and n: ");
scanf("%d%d",&m,&n);
if(n%m==0){
15 | P a g e
printf("m is a multiple of n");
} else{
printf("m is not a multiple of n");
}
return 0;
}
Output:

• Problem-10: Write a program to read three values using scanf statement and print the
following result:
a) Sum of the values
b) Average of the three values
c) Largest of the three
d) Smallest of the three
𝑠𝑠𝑠𝑠𝑠𝑠
Flowchart : Ini�alize 𝑎𝑎𝑎𝑎𝑎𝑎 = 3

Start
Ini�alize Largest and
Smallest number using
Take input three required logic
number n1, n2
and n3 as input

Print sum, avg,


largest and
Ini�alize
smallest number
𝑠𝑠𝑠𝑠𝑠𝑠 = 𝑛𝑛1 + 𝑛𝑛2 + 𝑛𝑛3

End

16 | P a g e
Code:
#include<stdio.h>
int main(){
printf("Name: Amit Kumar Dey\nID: 2102033\n\n");
int n1,n2,n3,sum,max,min;
float avg;
printf("Enter three numbers: ");
scanf("%d%d%d",&n1,&n2,&n3);
sum = n1+n2+n3;
avg = (float)sum/3;
if(n1>=n2 && n1>=n3){
max = n1;
} else if(n2>=n3 && n2>=n3){
max = n2;
} else{
max = n3;
}
if(n1<=n2 && n1<=n3){
min = n1;
} else if(n2<=n3 && n2<=n3){
min = n2;
} else{
min = n3;
}
printf("Sum of Three Numbers: %d\n",sum);
printf("Average of Three Numbers: %f\n",avg);
printf("Largest of Three Numbers: %d\n",max);
printf("Smallest of Three Numbers: %d\n",min);
return 0;
}

17 | P a g e
Output:

• Problem-11: The cost of one type of mobile service is Rs. 250 plus Rs. 1.25 for each call
made over and above 100 calls. Write a program to read customer codes and calls made
and print the bill for each customer.
Flowchart :

Start

Take test case


number as input

𝑡𝑡 ≠ 0 End

Take customer code


Ini�alize t=t-1 call number as input

Print bill Assign 𝑏𝑏𝑏𝑏𝑏𝑏𝑏𝑏 = 250 +


𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐 𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛 ≤ 100 1.25 ×
(𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐 𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛 − 100)

Assign 𝑏𝑏𝑏𝑏𝑏𝑏𝑏𝑏 = 250

18 | P a g e
Code:
#include<stdio.h>
int main(){
printf("Name: Amit Kumar Dey\nID: 2102033\n\n");
int t,cus_c,cus_call;
float bill;
printf("Enter how many customer's bill you want to generate: ");
scanf("%d",&t);
printf("\n");
while(t--){
printf("Enter customer code: ");
scanf("%d",&cus_c);
printf("Enter the number of calls: ");
scanf("%d",&cus_call);
if(cus_call<=100){
bill = 250;
} else{
bill = 250 + 1.25*(cus_call-100);
}
printf("Bill = %.2f\n\n",bill);
}
return 0;
}

19 | P a g e
Output:

• Problem-12: Write a program to illustrate the use of symbolic constants in a real-life


application.
Flowchart :

Start

Initialize i = 0

𝑖𝑖 ≤ 180 End

Yes

Ini�alize
𝜋𝜋
𝑟𝑟𝑟𝑟𝑟𝑟 = ( ) × 𝑖𝑖
180

Print
sin(𝑟𝑟𝑟𝑟𝑟𝑟) 𝑎𝑎𝑎𝑎𝑎𝑎 cos(𝑟𝑟𝑟𝑟𝑟𝑟)

20 | P a g e
Code:
#include<stdio.h>
#include<math.h>
#define pi acos(-1)
int main(){
printf("Name: Amit Kumar Dey\nID: 2102033\n\n");
double rad,res_sin,res_cos;
for(int i=0;i<=180;i+=15){
rad = (pi/180)*i;
printf("sin(%d) = %.3lf cos(%d) = %.3lf\n",i,sin(rad),i,cos(rad));
}
return 0;
}
Output:

Discussion and Conclusion:


In this assignment I have learnt the usage of Operators and their Expression in C programming.
Besides that I have practiced many programming problems to achieve proficiency in the
commands. And now I can believe that I can properly use these operators in program without any
mistake.

21 | P a g e

You might also like