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

ICS-Lab01

This document provides a comprehensive guide for getting started with C programming, including setting up the environment, writing, compiling, and running C programs on both Linux and Windows. It also includes a series of programming exercises that cover basic concepts in C, such as arithmetic operations, temperature conversion, and calculating areas. Additionally, it outlines how to submit programs for evaluation in the Prutor system.
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)
6 views

ICS-Lab01

This document provides a comprehensive guide for getting started with C programming, including setting up the environment, writing, compiling, and running C programs on both Linux and Windows. It also includes a series of programming exercises that cover basic concepts in C, such as arithmetic operations, temperature conversion, and calculating areas. Additionally, it outlines how to submit programs for evaluation in the Prutor system.
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/ 14

Lab-0: Getting Started

In this lab, we are first going to understand how to get started with C programming and
the Prutor system which will be used for evaluations:

How to Write and Run a C Program in Linux or windows

Following are the steps to configure the environment for the C program.

Open terminal (for Linux) or git bash (for Windows)

1. In order to compile and execute a C program, you need to have the essential packages
installed on your system. Enter the following command as root in your Linux Terminal:

For writing C code we require an editor in Linux you can use vim or gedit tools, and for
windows, you can use a text editor also. For every c program file, you have to save it in the “.c”
extension. In this session, we are using vim editor

2. Install vim using the following commands:

3. Create and open the .C file in the vim editor

It will create a hello.c file and the file will open in the vim editor.
Then press “i” for insert mode and type your C code.
To save the .c file in vim press the “Esc” button and then enter “:wq” which will save the
program and exit the vim editor.

4. Enter the following command to make an executable version of the program you have
written:

Make sure your program is located in your Documents folder. Otherwise, you will need to
specify appropriate paths in this command.

5. The final step is to run the compiled C program. Use the following syntax to do so:

How to submit C Program in the Prutor portal

Choose the problem which you have to solve.


You can write a program on prutor editor itself or you can write a program on your machine and
upload the code on the prutor.

To submit C program you can directly press “Evaluate” button this will automatically compile
and execute your program. Or you can compile and execute separately and then you can submit
your program. Refer the below figure.
After the evaluation of your program, you can see the visible test cases passed by your program.

Lab-1: The Basics


Note: First 5 problems, to be completed and submitted before the end of the lab session, the rest can be
completed before the deadline. All solutions to be submitted to the Mimir system.

In this lab, we shall continue with the basics of C programming.

1. Write a program to add the first n natural numbers. Take input of n from the user.
Solution:
num = input(“Enter the value of n”)
sum = num * (num + 1)/2
print(sum)

Answer the following:


A. What is the input( ) call?
B. Explain the expression you see in line 2.
C. What are the datatypes of num, and sum?

2. Write a program to first add, then subtract, then multiply two numbers that you take as
input from the user. [AMIT]

Solution:
#include<stdio.h>
int main()
{
int num1,num2;
scanf("%d %d",&num1,&num2);
printf("%d\n",num1+num2);
printf("%d\n",num1-num2);
printf("%d\n",num1*num2);
return 0;
}

3. Write a program to take the input of a temperature and assuming it is in centigrade,


convert it to Fahrenheit. [AMIT]

Solution:
#include<stdio.h>
int main()
{
float tempc,tempf;
scanf("%f",&tempc);
tempf=1.8*tempc+32;
printf("%f",tempf);
return 0;
}
4. Write a program to do the reverse of the earlier program, that is convert from Fahrenheit
to centigrade. [VIKASH]

Solution:

#include<stdio.h>

int main(){
float x;
scanf("%f", &x);
printf("%f", (x - 32)*5/9);

return 0;
}

5. Consider a quadratic equation of the form:


2
ax + bx + c = 0
Here, x is the unknown, and a, b, and c are the coefficients. Assuming that the roots of
the equation are real and distinct, compute the roots of the equation. [VIKASH]

Solution:
#include<stdio.h>
#include<math.h>

int main(){
// print roots of the equation ax^2 + bx + c = 0
float a, b, c;
scanf("%f %f %f", &a, &b, &c);
float x1, x2;
x1 = (-b + sqrt(b*b - 4*a*c))/(2*a);
x2 = (-b - sqrt(b*b - 4*a*c))/(2*a);
printf("%f %f", x2, x1);

return 0;
}

6. Write a program to input two characters and print them in reverse order separated by 1 space.
[NAMAN]
For example,
Input
ab
Output
ba
Solution:
#include <stdio.h>
int main(){
char a;
char b;
scanf("%c",&a);
scanf("%c",&b);
printf("%c %c",b,a);
return 0;
}

7. Write a program to input the radius of a circle and print the area, and circumference.
[NAMAN]

Solution:
#include<stdio.h>
int main(){
float r;
scanf("%f",&r);
float area,circumference;
area = 3.14*r*r;
circumference = 2*3.14*r;
printf("%f\n",area);
printf("%f",circumference);
return 0;
}

8. Write a program to input a number x and output x+xx+xxx+xxxx [PRASHANT]


For example, if input is 5. Output is: 5+55+555+5555 = 6170
Solution:
#include<stdio.h>
int main(){
int num;
scanf("%d",&num);
printf("%d",1234*num);
return 0;
}

9. Write a program to input to numbers a, and b and print the result of the integer division of a
by b, floating point division of a by b, and the remainder on dividing a by b. [PRASHANT]

Solution:
#include<stdio.h>
int main(){
int a, b;
scanf("%d%d",&a,&b);
float x = a, y = b;
printf("%d\n", a/b);
printf("%f\n", x/y);
printf("%d\n", a%b);
return 0;
}

b
10. Write a program to input two numbers a, and b and print a . [DEV]
Solution:
#include <stdio.h>
#include <math.h>
int main()
{
float a;
scanf("%f", &a);
float b;
scanf("%f", &b);
float ans = pow(a, b);
printf("%f", ans);
return(0);
}

11. Write a program to input height and base of a triangle and prints it's area. [DEV]
Solution:

#include <stdio.h>
int main()
{
float height;
scanf("%f", &height);
float base;
scanf("%f", &base);
float ans = (height*base)/2;
printf("%f", ans);
return(0);
}

12. Write a program to calculate the simple interest. [YASH]


Solution:
#include <stdio.h>

int main() {
int P, T, R;
scanf("%d %d %d", &P, &T, &R);

float simpleInterest = (P * T * R);


simpleInterest /= 100;
printf("%f", simpleInterest);

return 0;
}

13. Write a program that would take 4 integers as input namely x1, y1, x2, y2 and calculate the
euclidean distance between the points (x1, y1) and (x2, y2). Print the output distance rounded
upto two decimal places. [YASH]
Hint- Euclidean distance between points (x1, y1) and (x2, y2) is given by
2 2
𝐷= (𝑥1 − 𝑥2) + (𝑦1 − 𝑦2) where D is the euclidean distance.
Solution:
#include <stdio.h>
#include <math.h>

int main() {
int x1, y1;
scanf("%d %d", &x1, &y1);

int x2, y2;


scanf("%d %d", &x2, &y2);

int sumOfSquare = 0;
sumOfSquare += (x2 - x1) * (x2 - x1);
sumOfSquare += (y2 - y1) * (y2 - y1);
float euclideanDistance = sqrtl(sumOfSquare);
printf("%f", euclideanDistance);

return 0;
}

14. Write a program to calculate the cube of a number. [STUTI]


Solution :
#include <stdio.h>
int main(void)
{
int n, cube;
scanf("%d", &n);
cube = n * n * n;
printf("%d", cube);
return 0;
}

15. Write a program to swap two numbers x, y and print the final swapped values. [STUTI]
Solution :
#include <stdio.h>

int main(void)
{
int x, y, temp;
scanf("%d", &x);
scanf("%d", &y);

temp = x;
x = y;
y = temp;

printf("%d", x);
printf("\n");
printf("%d", y);
return 0;
}

16. Write a program to check whether a year is a leap year or not. Print “1” if the year is a leap
year and print “0” if the year is not a leap year. [SANIDHYA]
Solution:
#include <stdio.h>

int main() {

int year;

scanf("%d", &year);

int ans = (((year%4 == 0) && (year%100 != 0)) || (year%400 == 0));

printf("%d", ans);

return 0;

}
17. Find the difference between the square of the sum of the first n natural numbers and the sum
of the squares of the first n natural numbers. [SANIDHYA]
Solution:
#include<stdio.h>

int main(){

int n;

scanf("%d", &n);

int sum=n*(n+1)/2;

int sum_square = n*(n+1)*(2*n+1)/6;

printf("%d", sum*sum-sum_square);

return 0;

18. Write a program that takes x, y, and z as input and prints xyz as a single number. You should
not use string methods to concatenate the numbers but use multiplication.
(You can assume the x,y,z to be single digit inputs.) [SANIDHYA]
Solution:
#include <stdio.h>

int main(){
int x,y,z;
scanf("%d", &x);
scanf("%d", &y);
scanf("%d", &z);

int ans = 0;

ans = 100*x + 10*y + z;

printf("%d", ans);
return 0;

19. Write a program to check whether a character is an alphabet or not(a-z or A-Z). Print 1 if it
is, else print 0 . [RAJANDEEP]
Solution :

#include <stdio.h>

int main(){

char c;

scanf("%c" , &c);

int result = ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z' ));

printf("%d" , result);

return 0;

20. Write a program to take a number n as input and output each of the digits unit place
onwards in a new line.Assume that the input will be a three digit number. [RAJANDEEP]

Example:
Input : 196
Output:
6
9
1

Solution:
#include <stdio.h>
int main(){

int n;

scanf("%d" , &n);

printf("%d\n%d\n%d\n" , n%10 , (n/10)%10 , ((n/10)/10)%10);

return 0;

You might also like