Lab Activity3 Vinay Kapoor
Lab Activity3 Vinay Kapoor
1. Write a C program to declare variables of different data types (int, float, char) and print their
sizes using the sizeof() operator.
Solution:
#include<stdio.h>
int main(){
int num=3;
float fnum=89.7;
char chr='A';
double dnum=56789.90;
return 0;
Output:
2. Create a program that accepts an integer input and prints its binary, octal, and hexadecimal
equivalents.
Solution:
#include<stdio.h>
int main(){
int a,num,rem,bin=0,pow=1;
printf("enter a number:");
scanf("%d",&num);
a=num;
while(num!=0){
rem=num%2;
bin+=(rem*pow);
num=num/2;
pow=pow*10;
return 0;
Output:
3. Write a program that declares two integer variables and swaps their values without using a third
variable.
Solution:
#include<stdio.h>
int main()
int a,b,c;
scanf("%d%d",&a,&b);
printf("\n\nbefore swapping:\n");
printf("a=%d\nb=%d\n",a,b);
c=a;
a=b;
b=c;
printf("\n\nafter swapping:\n");
printf("a=%d\nb=%d\n",a,b);
return 0;
Output:
4. Design a program that accepts user input for two float numbers and outputs their sum,
difference, product, and quotient.
Solution:
#include<stdio.h>
int main()
float a,b;
scanf("%f%f",&a,&b);
printf("\nquotioent is",a/b);
return 0;
Output:
5. Write a C program to calculate the area of a circle using float data type and print the result up to
two decimal places.
Solution:
#include<stdio.h>
int main()
float r,area;
scanf("%f",&r);
area=3.14*r*r;
return 0;
Output:
6. Write a program to convert temperature from Fahrenheit to Celsius and vice versa, using float
variables.
Solution:
#include<stdio.h>
int main()
float tempc,tempf;
int c;
printf("\ncase1:celsius to fehrenhite");
printf("\ncase2:fehrenhite to celsius");
scanf("%d",&c);
switch(c){
case 1:
scanf("%f",&tempc);
tempf=(tempc*9/5)+32;
break;
case 2:
scanf("%f",&tempf);
tempc=(tempf-32)*5/9;
break;
return 0;
Output:
7. Create a program that declares a char variable and determines whether the character entered is a
vowel or consonant.
Solution:
#include<stdio.h>
int main()
char c;
scanf("%c",&c);
if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U'||c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
else{
return 0;
Output:
8. Write a C program that inputs a char and checks if it's a digit, uppercase letter, or lowercase letter
using if-else statements.
Solution:
#include<stdio.h>
int main()
char c;
scanf("%c",&c);
if(c=='A'||c=='B'||c=='C'||c=='D'||c=='E'||c=='F'||c=='G'||c=='H'||c=='I'||c=='J'||c=='K'||
c=='L'||c=='M'||c=='N'||c=='O'||c=='P'||c=='Q'||c=='R'||c=='S'||c=='T'||c=='U'||c=='V'||c=='W'||
c=='X'||c=='Y'||c=='Z'){
else if(c=='a'||c=='b'||c=='c'||c=='d'||c=='e'||c=='f'||c=='g'||c=='h'||c=='i'||c=='j'||
c=='k'||c=='l'||c=='m'||c=='n'||c=='o'||c=='p'||c=='q'||c=='r'||c=='s'||c=='t'||c=='u'||c=='v'||
c=='w'||c=='x'||c=='y'||c=='z'){
else{
return 0;
Output:
9. Develop a program to print the ASCII value of any char inputted by the user.
Solution:
#include<stdio.h>
int main()
char c;
scanf("%c",&c);
return 0;
Output:
10.Write a C program to find the sum of digits of an integer using int data type.
Solution:
#include<stdio.h>
int main()
int a,num,rem,sum=0;
scanf("%d",&num);
a=num;
while(num!=0){
rem=num%10;
sum+=rem;
num=num/10;
return 0;
Output:
11. Write a program to perform addition, subtraction, multiplication, division, and modulus
operations on two integers provided by the user.
Solution:
#include<stdio.h>
int main()
int a,b;
scanf("%d%d",&a,&b);
printf("\nquotioent is:%d",a/b);
printf("\nremainder is:%d",a%b);
return 0;
Output:
12.Create a C program that accepts two integers and prints the result of their increment (++) and
decrement (--) operations.
Solution:
#include<stdio.h>
int main()
int num1,num2;
scanf("%d%d",&num1,&num2);
printf("\nnum1=%d",num1);
printf("\nnum2=%d",num2);
printf("\nnum1++=%d",++num1);
printf("\nnum1--=%d",----num1);
printf("\nnum2++=%d",++num2);
printf("\nnum2--=%d",----num2);
return 0;
Output:
13.Write a C program to check if two numbers are equal using the == operator.
Solution:
#include<stdio.h>
int main()
int num1,num2;
scanf("%d%d",&num1,&num2);
printf("\nnum1=%d",num1);
printf("\nnum2=%d",num2);
if(num1==num2){
else{
return 0;
Output:
14.Design a program that checks if a given number is even or odd using the modulus operator (%).
Solution:
#include<stdio.h>
int main()
int num;
printf("enter a numbers:");
scanf("%d",&num);
if(num%2==0){
printf("\n%d is even",num);
else{
printf("\n%d is odd",num);
return 0;
Output:
15.Write a program to check if a number is divisible by both 3 and 5 using logical operators (&&).
Solution:
#include<stdio.h>
int main()
int num;
printf("enter a numbers:");
scanf("%d",&num);
if((num%3==0)&&(num%5==0)){
else if(num%3==0){
else if(num%5==0){
else{
return 0;
Output:
16.Create a program that finds the largest of three numbers using conditional operators (?:).
Solution:
#include<stdio.h>
int main()
int num1,num2,num3,big;
scanf("%d%d%d",&num1,&num2,&num3);
printf("\nnum1=%d\nnum2=%d\nnum3=%d",num1,num2,num3);
num1=num1>num2?num1:num2;
big=num1>num3?num1:num3;
return 0;
Output:
17.Write a C program to demonstrate the use of the bitwise AND (&), OR (|), and XOR (^) operators
on two integer inputs.
Solution:
#include<stdio.h>
int main()
int a,b;
scanf("%d%d",&a,&b);
printf("\nbitwse AND:%d",a&b);
printf("\nbitwse OR:%d",a|b);
printf("\nbitwse XOR:%d",a^b);
return 0;
Output:
18.Develop a program that uses the shift operators (<< and >>) to multiply and divide a number by 2,
respectively.
Solution:
#include<stdio.h>
int main()
int num;
printf("enter a numbers:");
scanf("%d",&num);
return 0;
Output:
19.Create a program that demonstrates the difference between logical AND (&&) and bitwise AND
(&) using two integer inputs.
Solution:
#include <stdio.h>
int main() {
int a, b;
if (a && b) {
} else {
return 0;
Output:
20.Write a C program to check if a number is positive, negative, or zero using relational and
conditional operators.
Solution:
#include<stdio.h>
int main()
int num;
printf("enter a number:");
scanf("%d",&num);
if(num>0){
else if(num<0){
else{
return 0;
Output:
21.Write a C program that accepts an integer input and checks if it is a leap year or not using if-else
statements.
Solution:
#include<stdio.h>
int main()
int year;
scanf("%d",&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0)){
else{
return 0;
Output:
22.Design a program that inputs a student's marks and prints "Pass" or "Fail" based on whether the
marks are greater than or equal to 40.
Solution:
#include<stdio.h>
int main()
int marks;
scanf("%d",&marks);
if(marks>=40){
printf("\nPass");
else{
printf("Fail");
return 0;
Output:
23.Write a C program that checks whether a number is prime or not using if-else.
Solution:
#include <stdio.h>
int main() {
scanf("%d", &num);
if (num <= 1) {
isPrime = 0;
} else {
if (num % i == 0) {
isPrime = 0;
break;
if (isPrime) {
} else {
return 0;
Output:
24.Create a program that accepts three sides of a triangle and checks if it forms a valid triangle using
if-else conditional statements.
Solution:
#include<stdio.h>
int main()
int s1,s2,s3;
scanf("%d%d%d",&s1,&s2,&s3);
if((s1+s2)>s3){
else if((s2+s3)>s1){
else if((s1+s3)>s2){
else{
return 0;
Output:
25.Write a program that inputs a year and checks whether it is a leap year using nested if-else.
Solution:
#include <stdio.h>
int main() {
int year;
scanf("%d", &year);
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
} else {
} else {
} else {
return 0;
Output: