0% found this document useful (0 votes)
10 views21 pages

Programs Lab

The document contains multiple C programming code snippets demonstrating basic programming concepts such as a simple calculator, smart home control system, loops (while, do-while, for), and various algorithms (ATM PIN verification, prime number check, Armstrong number check, perfect number check, and leap year determination). Each code snippet includes user input and output examples to illustrate functionality. The examples cover a range of topics suitable for beginners learning C programming.

Uploaded by

Anugraha K.R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views21 pages

Programs Lab

The document contains multiple C programming code snippets demonstrating basic programming concepts such as a simple calculator, smart home control system, loops (while, do-while, for), and various algorithms (ATM PIN verification, prime number check, Armstrong number check, perfect number check, and leap year determination). Each code snippet includes user input and output examples to illustrate functionality. The examples cover a range of topics suitable for beginners learning C programming.

Uploaded by

Anugraha K.R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

1.

Simple calculator

#include <stdio.h>
void main() {
int num1, num2, choice;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

printf("Choose operation: \n1. Add \n 2.


Subtract \n 3. Multiply \n 4. Divide\n");
scanf("%d", &choice);

switch(choice) {
case 1:
printf("Result: %d\n", num1 + num2);
break;
case 2:
printf("Result: %d\n", num1 - num2);
break;
case 3:
printf("Result: %d\n", num1 * num2);
break;
case 4:
if (num2 != 0)
printf("Result: %.2f\n", (float)num1 /
num2);
else
printf("Error! Division by zero.\n");
break;
default:
printf("Invalid choice\n");
}
}

Output
Enter two numbers: 3 4
Choose operation:
1. Add
2. Subtract
3. Multiply
4. Divide
3
Result: 12

*********************************

2.smart home

#include <stdio.h>
void main() {
int choice;
printf("Smart Home Control System\n");
printf("1. Turn on Lights\n");
printf("2. Turn off Lights\n");
printf("3. Set AC Temperature\n");
printf("4. Lock Doors\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch(choice) {
case 1:
printf("Lights turned ON\n");
break;
case 2:
printf("Lights turned OFF\n");
break;
case 3:
printf("Enter desired temperature: ");
int temp;
scanf("%d", &temp);
printf("AC temperature set to %d degree
celcius\n", temp);
break;
case 4:
printf("Doors locked!\n");
break;
case 5:
printf("Exiting Smart Home System...\n");
break;
default:
printf("Invalid choice! Please select a valid
option.\n");
}

Switch -case without break

void main() {
int day;
printf("Enter a number (1-7) for the day of the
week: ");
scanf("%d", &day);

switch(day) {
case 1:
printf("Monday\n");
case 2:
printf("Tuesday\n");
case 3:
printf("Wednesday\n");
case 4:
printf("Thursday\n");
case 5:
printf("Friday\n");
case 6:
printf("Saturday\n");
case 7:
printf("Sunday\n");
default:
printf("Invalid input!\n");
}
}

While
Print 1 to 5

#include <stdio.h>

void main() {
int i = 1;
while (i <= 5)
{
printf("%d\n", i);
i=i+1;
}

Do- while loop

#include <stdio.h>
void main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
}

Hai 10 times using do-while


#include<stdio.h>
void main()
{
int i=1;
do
{
printf("\nhai");
i=i+1;
}while(i<=10);
}
ATM pin
#include <stdio.h>

void main() {
int pin, attempt = 0;
int correctPin = 5566;

do {
printf("Enter your ATM PIN: ");
scanf("%d", &pin);
attempt++;

if (pin == correctPin) {
printf("Access Granted!\n");
break;
} else {
printf("Incorrect PIN. Try again.\n");
}
} while (attempt < 3);

if (attempt == 3 && pin != correctPin) {


printf("Too many failed attempts! Card
blocked.\n");
}

}
For loop- print 1 to 5

#include <stdio.h>

void main()
{
int i;
for (i = 1; i <= 5; i++)
{
printf("%d\n", i);
}

For loop
Sum of N natural numbers

#include <stdio.h>

void main() {
int N, sum = 0;
printf("Enter a number: ");
scanf("%d", &N);

for (int i = 1; i <= N; i++)


{
sum =sum +i;
}

printf("Sum of first %d natural numbers: %d\n",


N, sum);

Factorial of a number
#include <stdio.h>
void main() {
int N, fact = 1;
printf("Enter a number: ");
scanf("%d", &N);

for (int i = 1; i <= N; i++) {


fact=fact*i;
}

printf("Factorial of %d is: %d\n", N, fact);

}
**********************
Reverse of a number

#include <stdio.h>

void main() {
int num, rev = 0, digit;

printf("Enter a number: ");


scanf("%d", &num);

while (num > 0)


{
digit = num % 10; // Extract last digit
rev = rev * 10 + digit; // Append digit to
reversed number
num = num / 10; // Remove last digit
}

printf("Reversed Number: %d\n", rev);


}
Prime number
#include <stdio.h>
#include <math.h> // Necessary for the sqrt
function

void main() {
int n, i, flag= 0;

printf("Enter any number: ");


scanf("%d", &n);
if (n <= 1)
{
printf("%d is not a Prime number.\n", n);
}
else
{

for (i = 2; i <= sqrt(n); i++) {


if (n % i == 0) {
flag=1;
break;
}
}
}

if (flag == 0) {
printf("%d is a Prime number.\n", n);
} else
{
printf("%d is not a Prime number.\n", n);
}

}
Armstrong number

#include <stdio.h>
#include <math.h>

void main() {
int num, original, sum = 0, digits = 0,
remainder;

printf("Enter a number: ");


scanf("%d", &num);

original = num;
while (original != 0) {
digits++;
original = original/ 10;
}

original = num;
while (original != 0) {
remainder = original % 10;
sum = sum+pow(remainder, digits);
original =original/ 10;
}

if (sum == num)
printf("%d is an Armstrong Number\n",
num);
else
printf("%d is not an Armstrong Number\n",
num);
}

Perfect number
#include <stdio.h>

void main() {
int num, i, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);

for (i = 1; i <= num / 2; i++) {


if (num % i == 0)
sum =sum+ i;
}

if (sum == num)
printf("%d is a Perfect Number\n", num);
else
printf("%d is not a Perfect Number\n", num);

Leap year
#include <stdio.h>

void main() {
int year;

// Input: Read a year from the user


printf("Enter a year: ");
scanf("%d", &year);

// Check leap year conditions


if ((year % 4 == 0 && year % 100 != 0) || (year
% 400 == 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
}

Output
2000 – leap year
1900-not a leap year
2024 leap year
Leap year 2
#include <stdio.h>

void main() {
int year;

// Input: Read a year from the user


printf("Enter a year: ");
scanf("%d", &year);
if(year%100==0)
{
if (year % 400== 0 )
{
printf("%d is a leap year.\n", year);
}
else
printf("%d is not leap year.\n", year);
}

else
{
if (year%4==0)
{
printf("%d is a leap year.\n", year);
}
else
{
printf("%d is not a leap year.\n", year);
}
}

You might also like