ICS-Lab01
ICS-Lab01
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:
Following are the steps to configure the environment for the C program.
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
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:
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.
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)
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;
}
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;
}
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;
}
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);
}
int main() {
int P, T, R;
scanf("%d %d %d", &P, &T, &R);
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 sumOfSquare = 0;
sumOfSquare += (x2 - x1) * (x2 - x1);
sumOfSquare += (y2 - y1) * (y2 - y1);
float euclideanDistance = sqrtl(sumOfSquare);
printf("%f", euclideanDistance);
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);
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;
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;
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);
return 0;