PSPC Lab-1
PSPC Lab-1
2. Write a program to check whether the given number is even number or odd number.
Source Code:-
#include <stdio.h>
void main()
{
int num;
printf("Enter the number : \n");
scanf("%d",&num);
if(num%2==0)
{
printf("%d is a even number. \n",num);
}
else
{
printf("%d is a odd number. \n",num);
}
}
3. Write a menu based program to take of input of two values followed input of choice and
accordingly perform arithmetic operations like Addition, Subtraction, Multiplication,
Modulus, Division, Power( Using Switch Statement)
/*Run this file as gcc filename.c -lm here lm stands for Link Math which will be used for run Case:6
i.e., Power.*/
Source Code:-
#include <stdio.h>
#include <math.h>
int main()
{
int a,b,power;
int op;
printf("1.Addition \n 2.Subtraction \n 3.Multiplication \n 4.Division \n 5.Modulus \n 6.Power \n");
printf("Enter the two values : \n");
scanf("%d%d",&a,&b);
printf("Enter the choice of number for operation : \n");
scanf("%d",&op);
switch(op)
{
case 1:
printf("%d + %d = %d \n",a,b,a+b);
break;
case 2:
printf("%d - %d = %d \n",a,b,a-b);
break;
case 3:
printf("%d * %d = %d \n",a,b,a*b);
break;
case 4:
printf("%d / %d = %d \n",a,b,a/b);
break;
case 5:
printf("%d %% %d = %d \n",a,b,a%b);
break;
case 6:
power=pow(a,b);
printf("%d ^ %d = %d \n",a,b,power);
break;
default:
printf("Enter only the choices provided... \n");
}
return 0;
}
4. Write a program to swap two given numbers with and without using extra variable.
Source Code:-
/* Don’t run two programs at a time, there is only onw main function at a time, if you want
without variable comment the with variable main method and vice-versa.*/
//Without using extra variable
#include <stdio.h>
int main()
{
int a,b;
printf("Enter the values of a and b : \n");
scanf("%d%d",&a,&b);
printf("Before swapping, a=%d and b=%d \n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("After swapping, a=%d and b=%d \n",a,b);
return 0;
}/*
//With using extra variable
int main()
{
int a,b,c;
printf("Enter the values of a and b : \n");
scanf("%d%d",&a,&b);
printf("Before swapping, a=%d and b=%d \n",a,b);
c=a;
a=b;
b=c;
printf("After swapping, a=%d and b=%d \n",a,b);
return 0;
}*/
5. Write a program to find out the whether the given number is a perfect square or not.
Source Code:-
#include <stdio.h>
#include <math.h>
int main()
{
int num;
float fVar;
int iVar;
printf("Enter the number : \n");
scanf("%d",&num);
fVar=sqrt(num);
iVar=fVar;
if(iVar==fVar)
{
printf("%d is a perfect sqaure of %d . \n",num,iVar);
}
else
{
printf("%d is not a perfect square. \n",num);
}
return 0;
}
6. Write a program to find out whether the given number is positive, negative or zero value.
Source Code:-
#include <stdio.h>
int main()
{
int num;
printf("Enter the number : \n");
scanf("%d",&num);
if(num > 0)
{
printf("%d is a positive integer. \n",num);
}
else if (num<0)
{
printf("%d is a negative integer . \n",num);
}
else
{
printf("%d is ZERO . \n",num);
}
return 0;
}