Assignment 2
Assignment 2
Aim:
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.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.
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.
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:.
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