0% found this document useful (0 votes)
9 views4 pages

PSPC Lab-1

The document contains several C programming exercises, including programs to print memory allocation for data types, check if a number is even or odd, perform arithmetic operations based on user input, swap two numbers with and without an extra variable, determine if a number is a perfect square, and identify if a number is positive, negative, or zero. Each exercise includes source code and instructions for execution. The document serves as a practical guide for learning basic programming concepts in C.

Uploaded by

jaganbure
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)
9 views4 pages

PSPC Lab-1

The document contains several C programming exercises, including programs to print memory allocation for data types, check if a number is even or odd, perform arithmetic operations based on user input, swap two numbers with and without an extra variable, determine if a number is a perfect square, and identify if a number is positive, negative, or zero. Each exercise includes source code and instructions for execution. The document serves as a practical guide for learning basic programming concepts in C.

Uploaded by

jaganbure
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/ 4

LAB 1

Statements, Expressions & Conditionals


1. Write a program to print the memory allocation required for all the datatype in C
Language.
Source Code:-
#include <stdio.h>
int main()
{
int a;
char b;
float c;
printf("Size of int is %d \n",sizeof(int));
printf("Size of float is %d \n",sizeof(float));
printf("Size of char is %d \n",sizeof(char));
printf("Size of long int is %d \n",sizeof(long int));
printf("Size of short int is %d \n",sizeof(short int));
printf("Size of long long int is %d \n",sizeof(long long int));
printf("Size of signed int is %d \n",sizeof(signed int));
printf("Size of unsigned int is %d \n",sizeof(unsigned int));
printf("Size of signed long int is %d \n",sizeof(signed long int));
printf("Size of signed short int is %d \n",sizeof(signed short int));
printf("Size of unsigned long int is %d \n",sizeof(unsigned long int));
printf("Size of unsigned short int is %d \n",sizeof(unsigned short int));
printf("Size of double is %d \n",sizeof(double));
printf("Size of signed char is %d \n",sizeof(signed char));
printf("Size of long double is %d \n",sizeof(long double));
printf("Size of unsigned char is %d \n",sizeof(unsigned char));
printf("Size of a is %d \n",sizeof(a));
printf("Size of b is %d \n",sizeof(b));
printf("Size of c is %d \n",sizeof(c));
printf("All are in bytes.... \n");
return 0;
}

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;
}

You might also like