C LANGUAGE: INTRODUCTION, VARIABLES, AND
DATA TYPES
Introduction of C:
C is a high-level, general-purpose programming language developed by Dennis Ritchie in 1972 at
Bell Labs. It is widely used for developing system software, compilers, and embedded systems. C
provides low-level access to memory and supports structured programming, making it both
powerful and flexible.
Variables in C:
Variables are used to store data values in a program. Each variable in C has a specific data type,
which defines the type and size of data that can be stored in it.
Syntax: data_type variable_name;
Example: int age; float salary;
Data Types in C:
C supports different data types to define the nature of variables. The most common ones are: - int:
for integers - float: for real numbers - char: for characters - double: for large floating-point numbers
12 Practical Questions with Solutions:
1. Write a C program to print 'Hello, World!'.
#include int main() { printf("Hello, World!\n"); return 0; }
2. Write a program to declare and print an integer variable.
#include int main() { int age = 25; printf("Age = %d", age); return 0; }
3. Write a program to add two numbers.
#include int main() { int a=10, b=20, sum; sum = a + b; printf("Sum = %d", sum);
return 0; }
4. Write a program to find the size of different data types.
#include int main() { printf("Size of int: %lu\n", sizeof(int)); printf("Size of
float: %lu\n", sizeof(float)); printf("Size of char: %lu\n", sizeof(char));
printf("Size of double: %lu\n", sizeof(double)); return 0; }
5. Write a program to find the average of three numbers.
#include int main() { float a=10, b=20, c=30, avg; avg = (a + b + c) / 3;
printf("Average = %.2f", avg); return 0; }
6. Write a program to calculate the area of a rectangle.
#include int main() { float length=5, breadth=10, area; area = length * breadth;
printf("Area = %.2f", area); return 0; }
7. Write a program to convert Celsius to Fahrenheit.
#include int main() { float c=25, f; f = (c * 9/5) + 32; printf("Fahrenheit = %.2f",
f); return 0; }
8. Write a program to swap two numbers using a temporary variable.
#include int main() { int a=5, b=10, temp; temp = a; a = b; b = temp; printf("a =
%d, b = %d", a, b); return 0; }
9. Write a program to find whether a number is even or odd.
#include int main() { int num=7; if(num % 2 == 0) printf("Even"); else
printf("Odd"); return 0; }
10. Write a program to find the sum of first 10 natural numbers.
#include int main() { int i, sum=0; for(i=1; i<=10; i++) { sum += i; } printf("Sum =
%d", sum); return 0; }
11. Write a program to print ASCII value of a character.
#include int main() { char c = 'A'; printf("ASCII value of %c = %d", c, c); return
0; }
12. Write a program to demonstrate different variable types.
#include int main() { int age = 21; float salary = 55000.50; char grade = 'A';
printf("Age = %d\nSalary = %.2f\nGrade = %c", age, salary, grade); return 0; }
Python
April 13, 2025
[7]: # 1. What is Python Programming?
# Python is a simple and powerful programming language used to create websites,␣
↪ analyze data, build AI models, and much more. It's easy to learn because it␣
↪ uses plain English-like syntax.
# 2. What is a Variable?
↪
# A box
variable is like a during
can change box where you store information (data). The value in the␣
the program.
# 3. What is a Data Type?
# Data types define the kind of value a variable can hold, like:
# Integer (int): Whole numbers (e.g., 5, 100)
# Float (float): Decimal numbers (e.g., 3.14, 2.5)
# String (str): Text (e.g., "Ajit", "Python")
# Boolean (bool): True or False.
# What is Compiler vs Interpreter?
# Compiler:
# Converts the entire code into machine language before running it. Faster at␣
↪executionbutyouneedtocompilefirst(e.g.,C,C++).
#Example:Youwriteaprogram,compileit,andthenrunit.
# Interpreter:
↪
# Reads and executes the code line by line. Slower but easier for testing (e.g.
,Python,JavaScript).
#Example:YouwritePythoncode,anditrunsimmediatelywithoutcompiling.
# 5. What is Static and Dynamic Programming?
# Static Programming:
# You must define variable types before using them, and they can't change later.
# Example in C++:
# Dynamic Programming:
# Variable types are decided when the program runs, and they can change.
# Example in Python:
1
[9]: # simple first day program of addition
a=10
b=20
c=a+b
print(c)
30
[11]: # additing two values using user input
a=int(input("enter the value of a "))
b=int(input("enter the value of b "))
c=a+b
print("the addition of a and b is =",c)
enter the value of a 98
enter the value of b 3
the addition of a and b is = 101
[13]: # multiplication two values using user input
a=int(input("enter the value of a "))
b=int(input("enter the value of b "))
c=a*b
print("the addition of a and b is =",c)
enter the value of a 4
enter the value of b 4
the addition of a and b is = 16
[15]: # Program to Calculate Area of Triangle
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = 0.5 * base * height
print(f"Area of the triangle: {area}")
Enter the base of the triangle: 54
Enter the height of the triangle: 56
Area of the triangle: 1512.0
[17]: # Program to Calculate Area of Rectangle
length = float(input("Enter the length of the rectangle: "))
breadth = float(input("Enter the breadth of the rectangle: "))
area = length * breadth
print(f"Area of the rectangle: {area}")
Enter the length of the rectangle: 54
Enter the breadth of the rectangle: 56
2
Area of the rectangle: 3024.0
[19]: # Program to Calculate Area of Square
side = float(input("Enter the side of the square: "))
area = side ** 2
print(f"Area of the square: {area}")
Enter the side of the square: 54
Area of the square: 2916.0
[21]: # Program to Print 5 Subject Marks, Total, and Percentage
# Input marks for 5 subjects
subject1 = float(input("Enter marks for Subject 1: "))
subject2 = float(input("Enter marks for Subject 2: "))
subject3 = float(input("Enter marks for Subject 3: "))
subject4 = float(input("Enter marks for Subject 4: "))
subject5 = float(input("Enter marks for Subject 5: "))
# Calculate total and percentage
total = subject1 + subject2 + subject3 + subject4 + subject5
percentage = (total / 500) * 100 # Assuming each subject is out of 100
print(f"Total Marks: {total}")
print(f"Percentage: {percentage}%")
Enter marks for Subject 1: 66
Enter marks for Subject 2: 78
Enter marks for Subject 3: 98
Enter marks for Subject 4: 56
Enter marks for Subject 5: 87
Total Marks: 385.0
Percentage: 77.0%
[23]: name = input("Enter your name: ")
age = input("Enter your age: ")
email = input("Enter your email: ")
mobile = input("Enter your mobile number: ")
print("\n--- Biodata ---")
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Email: {email}")
print(f"Mobile: {mobile}")
Enter your name: aman
Enter your age: 44
Enter your email: aman@[Link]
3
Enter your mobile number: 37463664
--- Biodata ---
Name: aman
Age: 44
Email: aman@[Link]
Mobile: 37463664
[]:
4
mvgc4qb84
April 2, 2025
[15]: # create a list
list=[1,2,3,4,5,5]
print("the list is =",list)
[Link](2,10)
print("the updated list is =",list)
the list is = [1, 2, 3, 4, 5, 5]
the updated list is = [1, 2, 10, 3, 4, 5, 5]
[19]: # append is used to insert the element in the end of the list
list=[1,2,3,4,5,5]
[Link](120000)
print(list)
[1, 2, 3, 4, 5, 5, 120000]
[29]: # count is used to count any specific number present in the list
list=[2,3,5,3,2,2,2,4,5,3,2,2,2,4,5,4]
count=[Link](4)
print(count)
[31]: # index is used to get the index of any perticular element
list=[1,2,3,4,5,5]
[Link](5)
[31]: 4
[37]: # sort is used to sort the element of the list
list=[2,3,5,3,2,2,2,4,5,3,2,2,2,4,5,4]
[Link]()
print(list)
[2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5]
[39]: # clear is used to clear the list now the list is empty
list=[2,3,5,3,2,2,2,4,5,3,2,2,2,4,5,4]
1
[Link]()
print(list)
[]
[41]: # concatination is used to combine the elements of two list in a single list
list1=[3,2,4,3]
list2=[74,543,34]
result=list1+list2
print (result)
[3, 2, 4, 3, 74, 543, 34]
[43]: # len is used to check the length of the list
list=[2,3,5,3,2,2,2,4,5,3,2,2,2,4,5,4]
print(len(list))
16
[45]: # remove is used to remove the element
list=[2,3,5,3,2,2,2,4,5,3,2,2,2,4,5,4]
[Link](2)
print(list)
[3, 5, 3, 2, 2, 2, 4, 5, 3, 2, 2, 2, 4, 5, 4]
[51]: list=[2,3,5,3,2,2,2,4,5,3,2,2,2,4,5,4]
print([Link](4))
[59]: string="welcome to the home "
s=(string[14:20])
print(s)
home
[1]: input_string="my name is ajit rawat"
# output_string="is"
p=(input_string[8:11])
print(p)
is
[11]: string="helloworldajit"
# output---->elowrd
s=(string[Link])
print(s)
2
eorat
[15]: list=[2,4,5,65,3,5,43,4,3]
# output=[5,65,3]
s=(list[2:5])
print(s)
[5, 65, 3]
[19]: list=[2,4,5,65,3,5,43,4,3]
s=(list[::-1])
print(s)
[3, 4, 43, 5, 3, 65, 5, 4, 2]
[1]: # creating a tuple
tuple=(1,2,2,2,1,3,2,3)
print(tuple)
(1, 2, 2, 2, 1, 3, 2, 3)
[3]: # tuple unpacking
tuple=(1,2,4,5)
x,y,z,p=tuple
print(x)
print(y)
print(z)
print(p)
1
2
4
5
[5]: # tuple packing
x,y,z=1,2,4
tuple=x,y,z
print(tuple)
(1, 2, 4)
[7]: # accessing element of a tuple
t=(34,34,345,5,66,6,4,6,54)
print(t[3])
3
[9]: # cancatination
t1=(34,34,345,5,66,6,4,6,54)
t2=(32,4,54,3,53,5,23,43,433)
result=t1+t2
print(result)
(34, 34, 345, 5, 66, 6, 4, 6, 54, 32, 4, 54, 3, 53, 5, 23, 43, 433)
[11]: t1=(34,34,345,5,66,6,4,6,54)
result=t1*2
print(result)
(34, 34, 345, 5, 66, 6, 4, 6, 54, 34, 34, 345, 5, 66, 6, 4, 6, 54)
[13]: # wap to create a ATM machine stimulation
account_number=[334432432,3254215,1465362,3415342,415342532]
pin=[4523,3241,2314,3244,4534]
account_balance=[123321,23424,566776,4231,454365]
print("WELCOME TO THE ATM MACHINE ")
# authentication loop
while True:
try:
# take user input of acount number nd pins
entered_account_number=int(input("enter account number"))
entered_pin=int(input("enter your ATm pin"))
# validate account number and pins
if entered_account_number in account_number and entered_pin in pin:
account_index=account_number.index(entered_account_number)
# if account_number[account_index]==entered_account_number
if pin[account_index]==entered_pin:
print("login successfull")
break
else:
print("invalid account pin please enter a correct account pin ")
else:
print("invalid account number please enter a correct account␣
↪number")
except ValueError:
print("invald")
# menu for operations
while True:
print("main menu")
print("1. withdraw amount")
print("2. deposite amount")
print("3. check balance ")
print("4. exit")
# now take user choice whatever they want to perform operation
4
choice=input("enter the operation you want to perform ")
if choice=="1":
# withdrawel option that means amount will deduct
try:
withdrawal_amount=int(input("enter the withdrawal amount"))
if withdrawal_amount>0 and␣
↪withdrawal_amount<=account_balance[account_index]:
account_balance[account_index]-=withdrawal_amount
print(f"{withdrawal_amount} amount successfull")
print(f"the new updated balance␣
↪",account_balance[account_index])
elif(withdrawal_amount>account_balance[account_index]):
print("insufficient balance please add some money ")
else:
print("please enter the amount greater then zero")
except ValueError:
print("invalid input ")
# second choice is for amount deposite
elif choice=="2":
try:
deposite_amount=int(input("enter the deposite amount"))
if deposite_amount>0:
account_balance[account_index]+=deposite_amount
print(f"{deposite_amount} amount deposite succesfully")
print("updated balance",account_balance[account_index])
else:
print("enter a valid amount greater then zero")
except ValueError:
print("please enter a valid value")
# third choice is for check balance
elif choice=="3":
print("\n account details")
print("account number",account_number[account_index])
print("account balance =",account_balance[account_index])
# fourth choice is for exit
elif choice=="4":
print("thank you for using the ATM machine have a nice day! ")
break
else:
print("invalid choice please try again ")
WELCOME TO THE ATM MACHINE
enter account number 334432432
enter your ATm pin 4523
login successfull
main menu
5
1. withdraw amount
2. deposite amount
3. check balance
4. exit
enter the operation you want to perform 1
enter the withdrawal amount 10
10 amount successfull
the new updated balance 123311
main menu
1. withdraw amount
2. deposite amount
3. check balance
4. exit
enter the operation you want to perform 2
enter the deposite amount 1000
1000 amount deposite succesfully
updated balance 124311
main menu
1. withdraw amount
2. deposite amount
3. check balance
4. exit
enter the operation you want to perform 3
account details
account number 334432432
account balance = 124311
main menu
1. withdraw amount
2. deposite amount
3. check balance
4. exit
enter the operation you want to perform 4
thank you for using the ATM machine have a nice day!
[ ]: