0% found this document useful (0 votes)
18 views

Assignment 2

Uploaded by

syamantoksaha8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Assignment 2

Uploaded by

syamantoksaha8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

1.

Aim:

1.1. Types of Operators, Precedence and associativity of Operators


1.2. Control Statements
1.3. Loop Statement
2. Hardware Requirement:

Device name LAPTOP-C19KJEDO


Processor AMD Ryzen 7 5800H with Radeon Graphics 3.20 GHz
Installed RAM16.0 GB (15.3 GB usable)
Device ID 57D7EDC7-F482-43E0-B727-EFD96C13150E
Product ID 00342-42643-48855-AAOEM
System type 64-bit operating system, x64-based processor
Pen and touch No pen or touch input is available for this display
3. Software Requirements:

EditionWindows 11 Home Single Language


Version 22H2
Installed on 30-07-2023
OS build 22621.2283
Experience Windows Feature Experience Pack 1000.22662.1000.0
C Compiler Online (GDB Compiler)
4. Knowledge requirement:

4.1 Basic:
1. How to create a C program
2. How to Save a C program
3. How to compile a C program
4. How to edit a C program

4.2 Structure of C:
1. Documentation
2. Definition Section
3. Global Declaration
4. Main Function
5. Sub programs

4.3 Predefined functions in C :


Printf, Scanf, Clrsct, sqrt, etc..

4.4 Variables:
Valid names of variables, Variable declaration, Data type of Variables and more.

4.5 Operators:
Arithmetic operators, Binary operators, logical operators and more. Hierarchy of operators,
Associativity of operators.

4.6 Decision control Instructions:


If, If else, Nested If.

4.7 Complex decision making


1. The first two operators, && and ||, allow two or more conditions to be combined in an if
statement. Let us see how they are used in a program. (a) As the number of conditions go on
increasing the level of indentation also goes on increasing. As a result, the whole program
creeps to the right. So much so that entire program is not visible on the screen. So, if
something goes wrong with the program locating what is wrong where becomes difficult. (b)
Care needs to be exercised to match the corresponding ifs and else. (c) Care needs to be
exercised to match the corresponding pair of braces.
2.&& and || are binary operators, whereas, ! is a unary operator.

4.8 Loop control Instruction:


There are three methods by way of which we can repeat a part of a program. They are:
(a) Using a for statement
(b) Using a while statement
(c) Using a do-while statement
1. The while Loop
It is often the case in programming that you want to repeat something a fixed number of
times. Perhaps you want to calculate gross salaries of ten different persons, or you want to
convert temperatures from Centigrade to Fahrenheit for 15 different cities. The while loop is
ideally suited for this. Let us look at a simple example that uses a while loop to calculate
simple interest for 3 sets of values of principal, number of years and rate of interest.

2. The for Loop


Perhaps one reason why few programmers use while is that they are too busy using the for,
which is probably the most popular looping instruction. The for allows us to specify three
things about a loop in a single line: (a) Setting a loop counter to an initial value. (b) Testing
the loop counter to determine whether its value has reached the number of repetitions desired.
(c) Increasing the value of loop counter each time the body of the loop has been executed.

3. Do while loop
There is a minor difference between the working of while and do-while loops. This difference
is the place where the condition is tested. The while tests the condition before executing any
of the statements within the while loop. As against this, the do-while tests the condition after
having executed the statements within the loop
5. Theory:

Communicating with a computer involves speaking the language the computer understands,
which immediately rules out English as the language of communication with computer.
However, there is a close analogy between learning English language and learning C
language. The classical method of learning English is to first learn the alpha bets used in the
language, then learn to combine these alphabets to form words, which, in turn, are combined
to form sentences and sentences are combined to form paragraphs.

Data types, sizes and ranges.


6. Problems:

6.1 (a) Given the values of the variables x, y and z, write a program to rotate their
values such that x has the value of y, y has the value of z, and z has the value of x.
Code:
//Online program to compile c program
#include <stdio.h>

int main()
{
printf("Syamantok Saha 23BEC076 \n");
int x,y,z,a;
printf("Enter number x: \n");
scanf("%d",&x);
printf("Enter number y: \n");
scanf("%d",&y);
printf("Enter number z: \n");
scanf("%d",&z);
a=x;
x=y;
y=z;
z=a;
printf("x=%d \t",x);
printf("y=%d \t",y);
printf("z=%d \t",z);
}
Output:
(b) The total distance travelled by a vehicle in t seconds is given by distance = ut + (at2)/2
where u is the initial velocity (metres per second), a is the acceleration (metres per second2).
Write a program to evaluate the distance travelled at regular intervals of time, given the
values of u and a. The program should provide the flexibility to the user to select his own
time intervals and repeat the calculations for different values of u and a.
Code:
//Online program to compile c program
#include <stdio.h>

int main()
{
printf("Syamantok Saha 23BEC076 \n");
float u,a,t,s;
printf("Enter the initial velocity of the car \n");
scanf("%f",&u);
printf("Enter the acceleration of the car \n");
scanf("%f",&a);
printf("Enter the time duration of the car \n");
scanf("%f",&t);
s=(u*t)+((a*t*t)/2);
printf("The total distance covered by the car: %f",s);
}

Output:
6.2 (a) Write a program that determines whether a given integer is odd or even and displays
the number and description on the same line.
Code:
//Online program to compile c program
#include <stdio.h>

int main()
{
printf("Syamantok Saha 23BEC076 \n");
int a;
printf("Enter a number: ");
scanf("%d",&a);
if(a%2==0)
printf("The entered number %d is even",a);
else
printf("The entered number %d is odd",a);
}
Output:
(b) Write a program to find the largest number among the inputted four numbers using: (i)
Nested if (ii) Logical operators
Code 1:
//Online program to compile c program
#include <stdio.h>

int main()
{
printf("Syamantok Saha 23BEC076 \n");
int a,b,c;
printf("Enter number a \n");
scanf("%d",&a);
printf("Enter number b \n");
scanf("%d",&b);
printf("Enter number c \n");
scanf("%d",&c);
if(a>b)
{
if(a>c)
printf("The largest number is: %d",a);
else
printf("The largest number is: %d",c);
}
else if(b>a)
{
if(b>c)
printf("The largest number is: %d",b);
else
printf("The largest number is: %d",c);
}
else if(c>a)
{
if(c>b)
printf("The largest number is: %d",c);
else
printf("The largest number is: %d",a);
}

}
Output:

Code 2:
//Online program to compile c program
#include <stdio.h>

int main()
{
printf("Syamantok Saha 23BEC076 \n");
int a,b,c;
printf("Enter number a \n");
scanf("%d",&a);
printf("Enter number b \n");
scanf("%d",&b);
printf("Enter number c \n");
scanf("%d",&c);
if((a>b)&&(a>c))
printf("The largest number is: %d",a);
else if((b>c)&&(b>a))
printf("The largest number is: %d",b);
else if((c>a)&&(c>b))
printf("The largest number is: %d",c);
}
Output:.

(c) Admission to a professional course is subject to the following conditions: Marks in


Mathematics >= 60 Marks in Physics >= 50 Marks in Chemistry >= 40 Total in all three
subjects >= 200 or Total in Mathematics and Physics >= 150 Given the marks in the three
subjects, write a program to process the applications to list whether the candidate is eligible
or not.

Code:
//Online program to compile c program
#include <stdio.h>

int main()
{
printf("Syamantok Saha 23BEC076 \n");
float a,b,c,s,p;
printf("Enter marks in Maths \n");
scanf("%f",&a);
printf("Enter marks in Physics \n");
scanf("%f",&b);
printf("Enter marks in Chemistry \n");
scanf("%f",&c);
s=a+b+c;
p=a+b;
if((a>=60)&&(b>=50)&&(c>=40)){
if((s>=200)||(p>=150))
printf("The candidate is egligible for the course");
}
else
printf("The candidate is not egligible for the course");
}

Output:
6.3 (a) Write a program to calculate the average of a set of N numbers.

Code:
#include <stdio.h>
int main()
{
printf("Syamantok Saha 23BEC076 \n");
float num,i,j,sum,avg;
printf("How many number’s avarage you want \n ");
scanf("%f",&j);
printf("Please enter your numbers :");
sum=0;
for(i=1;i<=j;++i)
{
scanf("%f",&num);
sum=sum+num;
}
avg=sum/j;
printf("Avarage of this %f numbers is =%f",j,avg);
return 0;
}
Output:

(b) Write a program to count all the prime numbers that lie between 100 and 200.
Code:
//Online program to compile c program
#include <stdio.h>

int main()
{
printf("Syamantok Saha 23BEC076 \n");
int a=0,i,j,b=0;
for(i=100;i<=200;i++)
{
for(j=i;j>=1;j--)
{
if(i%j==0)
a=a+1;
}
if(a==2)
b=b+1;
a=0;
}
printf("No.of prime numbers are: %d",b);
}
Output:

(c) Write a program to output the following multiplication table for any number and display the
output in the following format:

5*1=5
5 * 2 = 10
5 * 3 = 15
.
.
.
.
5 * 10 = 50

Code:
//Online program to compile c program
#include <stdio.h>

int main()
{
printf("Syamantok Saha 23BEC076 \n");
int n,i;
printf("Enter a number \n");
scanf("%d",&n);
for(i=1;i<11;i++)
{
printf("%d x %i = %d \n",n,i,(n*i));
}
}

Output:

(d) Write a program to print all integers that are not divisible by either 2 or 3 and lie between
1 and 100. Program should also count the number of such integers and print the result.

Code:
//Online program to compile c program
#include <stdio.h>

int main()
{
printf("Syamantok Saha 23BEC076 \n");
int i,a=0;
printf("Numbers that are neither divisible by 2 noy by 3 \n");
for(i=1;i<101;i++)
{
if((i%2!=0)&&(i%3!=0))
{
printf("%d ",i);
a++;
}
}
printf("\nNo.of digits are: %d",a);

Output:

(e) Write a program to compute and display the sum of all integers that are divisible by 6 but
not divisible by 4 and lie between 0 and 100. The program should also count and display the
number of such values.

Code:
//Online program to compile c program
#include <stdio.h>

int main()
{
printf("Syamantok Saha 23BEC076 \n");
int i,a=0;
printf("Numbers that are divisible by 6 but not by 4 \n");
for(i=0;i<101;i++)
{
if((i%6==0)&&(i%4!=0))
{
printf("%d \n",i);
a++;
}
}
printf("No.of digits are: %d",a);

Output:
7. Conclusion:
We learn to use of loops (for loop, while and do... while loops). Also, how to control loops
and how to control statements some algebraic logics use of logical operators, etc.
8. Reference:
7.1 Let us C.pdf
7.2 Hand written Notes

You might also like