0% found this document useful (0 votes)
36 views31 pages

Cs 01

This document contains code snippets and output for 6 labs on basic C programming concepts. Lab-1 covers opening a coding window in vi editor, compiling a program using gcc, and running the output file. It includes a "Hello World" program. Lab-2 contains programs to calculate math operations on two numbers and find the area and perimeter of a circle. Lab-3 includes programs to swap two numbers using a third variable and without. Lab-4 checks if a number is even/odd and finds the largest of three numbers. Lab-5 orders two numbers and finds the factorial of a positive number. Lab-6 reverses a number, checks if a number is Armstrong, and checks if a number is perfect.

Uploaded by

ARCHISHMAN ROY
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)
36 views31 pages

Cs 01

This document contains code snippets and output for 6 labs on basic C programming concepts. Lab-1 covers opening a coding window in vi editor, compiling a program using gcc, and running the output file. It includes a "Hello World" program. Lab-2 contains programs to calculate math operations on two numbers and find the area and perimeter of a circle. Lab-3 includes programs to swap two numbers using a third variable and without. Lab-4 checks if a number is even/odd and finds the largest of three numbers. Lab-5 orders two numbers and finds the factorial of a positive number. Lab-6 reverses a number, checks if a number is Armstrong, and checks if a number is perfect.

Uploaded by

ARCHISHMAN ROY
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/ 31

Lab-1:

• How to open coding window?


➢ At first open terminal
➢ Create a file using ‘vi’ command (Extension
should be “.c”).
Ex: vi hello.c
➢ Press ‘i’ to write/edit your code.
• How to compile the program?
➢ Exit from editor-
▪ Press ‘esc’ button
▪ Write ‘:wq’
▪ Press enter
➢ Write this command for compile program-
▪ gcc<space>fileName<space>-
o<space>outputFileName
(Extension of output file should
be ‘.out’)
ex- gcc hello.c -o new.out
• How to run the program?
➢ Write this command for compile program-
▪ ./outputFileName
Ex- ./new.out
Q-1: Write a program to print “Hello World”.

Code:

#include<stdio.h>
int main()
{
printf("Hello World");
return 0;
}
Output:
Q-2: Write a program to print your name.

Code-

#include<stdio.h>
int main()
{
printf("Akash Roy");
return 0;
}
Output:
Lab-2

Q-1: Write a program to accept two number from user and


find it’s addition, substraction, multiplication, division,
modulo.

Code:-

#include<stdio.h>
int main()
{
int a,b;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
printf("The addition of %d and %d is %d\n",a,b,a+b);
printf("The substraction of %d and %d is %d\n",a,b,a-b);
printf("The multiplication of %d and %d is %d\n",a,b,a*b);
printf("The division of %d and %d is %d\n",a,b,a/b);
printf("The modulo of %d and %d is %d",a,b,a%b);
return 0;
}
Output:-
Q-2: Write a program to accept the radius of a circle, find its
area as well as perimeter.

Code:-

#include<stdio.h>
int main()
{
int r,area,peri;
printf("Enter radius:");
scanf("%d",&r);
area=3.14*r*r;
peri=2*3.14*r;
printf("The area of the circle is %d\n",area);
printf("The perimeter of the circle is %d",peri);
return 0;
}
Output:-
Lab-3
Q-1 : Write a program to accept two number from user and
swap that two number using third variable.
Code:-

#include<stdio.h>
int main()
{
int num1,num2,temp;
printf("Enter 1st number:");
scanf("%d",&num1);
printf("Enter 2nd number:");
scanf("%d",&num2);
printf("Before swapping the 1st number is %d and the
second number is %d\n",num1,num2);
temp=num1;
num1=num2;
num2=temp;
printf("After swapping the 1st number is %d and the
second number is %d",num1,num2);
return 0;}
Output:-
Q-2: Write a program to accept two number from user and
swap that two number without third variable.

Code:-
#include<stdio.h>
int main()
{
int num1,num2;
printf("Enter 1st number:");
scanf("%d",&num1);
printf("Enter 2nd number:");
scanf("%d",&num2);
printf("Before swapping the 1st number is %d and the
second number is %d\n",num1,num2);

num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
printf("After swapping the 1st number is %d and the
second number is %d",num1,num2);
return 0;
}
Output:-
Lab-4

Q-1: Write a program to accept a number from user and


check the number is even or odd.
Code:

#include<stdio.h>
int main()
{
int num;
printf("Enter a number:");
scanf("%d",&num);
if(num%2==0){
printf("The number is even");
}
else{
printf("The number is odd");
}
return 0;
}
Output:
Q-2: Write a program to accept three number from user and
find the largest number.
Code:

#include<stdio.h>
int main()
{
int num1,num2,num3;
printf("Enter three number:");
scanf("%d%d%d",&num1,&num2,&num3);
if(num1>=num2 && num1>=num3){
printf("The largest number is %d",num1);
}
else if(num2>=num1 && num2>=num3){
printf("The largest number is %d",num2);
}
else{
printf("The largest number is %d",num3);
}

return 0;
}
Output:
Lab-5
Q-1:- Wap to accept two number from the user and
print it in acending order.
Code-
#include<stdio.h>
int main()
{
int num1,num2,i;

printf("Enter first number:");


scanf("%d",&num1);
printf("Enter second number:");
scanf("%d",&num2);

if(num1>num2){
for(i=num2;i<=num1;i++)
printf("%d\n",i);
}
else{
for(i=num1;i<=num2;i++)
printf("%d\n",i);
}
}
Output:
Q-2:-
Wap to accept a positive number and find its factorial.
Code:-
#include<stdio.h>
int main()
{
intnum,i,fact=1;
printf("Enter a positive number:");
scanf("%d",&num);
if(num>0){
for(i=num;i>=1;i--){
fact*=i;
}
printf("The factorial of %d is %d",num,fact);
}
else{
printf("Enter a positive number!!!!!");
}
}
Output:
Q-3:- Wap to accept a positive number and check
the number is prime or not.
Code:-
#include<stdio.h>
int main()
{
intn,i,m=0,flag=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
} }
if(flag==0){
printf("Number is prime");
return 0;
}
}
Output:-
Lab-6
Q-1:- Wap to accept a positive number and
find its reverse.
Code:-
#include<stdio.h>
int main()
{
int n, reverse = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
if(n>0){
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", reverse);
}
else{
printf(“Enter a positive number”);
}
return 0;
}
Output:-
Q-2:- Wap to accept a positive number and
check the number is Armstrong or not.
Code:-
#include<stdio.h>
int main()
{
intn,num,r, ans=0;
printf("Enter an integer: ");
scanf("%d", &n);
num=n;
if(n>0){

while (n != 0) {
r = n % 10;
ans = ans+r*r*r;
n /= 10;
}
if(ans==num){
printf("%d is an Armstrong
number.",num);
}
else{
printf("%d is not an Armstrong
number.",num);
}
}
else{
printf("Enter a positive number!!!");
}
return 0;
}
Output:-
Q-3:- Wap to accept a positive number and
check the number is perfect or not.
Code:-
#include<stdio.h>
int main()
{
intnum, count = 1, sum = 0;

printf("Enter a number\n");
scanf("%d", &num);

while(count <num)
{
if(num%count == 0)
{
sum = sum + count;
}
count++;
}

if(sum == num)
{
printf("\n%d is a perfect number\n", num);
}
else
{
printf("\n%d is not a perfect number\n", num);
}

return 0;
}
Output:-

You might also like