0% found this document useful (0 votes)
20 views

C Programming Notes

C Programming Notes For FYBCA students in GOA 2024-2025

Uploaded by

hanakotoba191
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

C Programming Notes

C Programming Notes For FYBCA students in GOA 2024-2025

Uploaded by

hanakotoba191
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

FYBCA

R.NO:2408005

1]print("Hello, World!")

Output
Hello, World!

2]
num = int(input("Enter an integer: "))
print("You entered:", num)

Output
Enter an integer: 5
You entered: 5

3]num = float(input("Enter a float: "))


print("You entered:", num)

Output
Enter a float: 3.14
You entered: 3.14

4]char = input("Enter a character: ")[0]


print("You entered:", char)

Output
Enter a character: a
You entered: a

5]num = float(input("Enter a float: "))


print(f"You entered: {num:.2f}")

Output
Enter a float: 3.1459
You entered: 3.15

6]char = input("Enter a character: ")[0]


print("ASCII value:", ord(char))

Output
Enter a character: A
ASCII value: 65
7]num = float(input("Enter a double (float): "))
print("You entered:", num)

Output
Enter a double (float): 7.12345
You entered: 7.12345

8]num = int(input("Enter an integer (ASCII value): "))


print("Character:", chr(num))

Output
Enter an integer (ASCII value): 97
Character: a

9]char = input("Enter a lowercase character: ")[0]


print("Uppercase:", char.upper())

Output
Enter a lowercase character: b
Uppercase: B

10]char = input("Enter a character: ")[0]


shifted_char = chr((ord(char) - 97 + 1) % 26 + 97)
print("Caesar cipher:", shifted_char)

Output
Enter a character: a
Caesar cipher: b

11]a = float(input("Enter first number: "))


b = float(input("Enter second number: "))
print("Sum:", a + b)

Output
Enter first number: 3.5
Enter second number: 2.5
Sum: 6.0

12]a = float(input("Enter first number: "))


b = float(input("Enter second number: "))
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b if b != 0 else "Division by zero error")
Output

Enter first number: 8


Enter second number: 4
Addition: 12.0
Subtraction: 4.0
Multiplication: 32.0
Division: 2.0

13]num = int(input("Enter an integer: "))


print("Remainder:", num % 2)

Output
Enter an integer: 7
Remainder: 1

14]a = float(input("Enter first number: "))


b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
print("Average:", (a + b + c) / 3)

Output
Enter first number: 3
Enter second number: 4
Enter third number: 5
Average: 4.0

15]base = float(input("Enter base of the triangle: "))


height = float(input("Enter height of the triangle: "))
print("Area:", 0.5 * base * height)

Output
Enter base of the triangle: 5
Enter height of the triangle: 4
Area: 10.0

16]import math
radius = float(input("Enter radius of the circle: "))
print("Area:", math.pi * radius ** 2)

Output
Enter radius of the circle: 3
Area: 28.27
17]a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
a, b = b, a
print("After swapping, first number:", a)
print("After swapping, second number:", b)

Output
Enter first number: 4
Enter second number: 5
After swapping, first number: 5
After swapping, second number: 4

18]def area_of_triangle(base, height):


return 0.5 * base * height

base = float(input("Enter base of triangle: "))


height = float(input("Enter height of triangle: "))
print("Area of triangle:", area_of_triangle(base, height))

Output
Enter base of triangle: 6
Enter height of triangle: 3
Area of triangle: 9.0

19]num = int(input("Enter a number: "))


if num > 5:
print("Number is greater than 5")
else:
print("Number is not greater than 5")

Output
Enter a number: 6
Number is greater than 5

20]age = int(input("Enter your age: "))


if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")

Output
Enter your age: 17
Not eligible to vote
21]num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")

Output
Enter a number: 7
Odd

22]a = int(input("Enter first number: "))


b = int(input("Enter second number: "))
if a > b:
print("Greatest:", a)
else:
print("Greatest:", b)

Output
Enter first number: 4
Enter second number: 5
Greatest: 5

23]a = int(input("Enter first number: "))


b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a > b and a > c:
print("Greatest:", a)
elif b > c:
print("Greatest:", b)
else:
print("Greatest:", c)

Output
Enter first number: 4
Enter second number: 8
Enter third number: 6
Greatest: 8

24]gender = input("Enter gender (M/F): ").upper()


if gender == "M":
print("Gender: Male")
elif gender == "F":
print("Gender: Female")
else:
print("Invalid input")

Output
Enter gender (M/F): F
Gender: Female

25]age = int(input("Enter your age: "))


income = int(input("Enter your income: "))
if age >= 18:
if income < 50000:
print("Eligible for Scheme A")
elif income < 100000:
print("Eligible for Scheme B")
else:
print("Not eligible for schemes")
else:
print("Not eligible for any scheme")

Output
Enter your age: 20
Enter your income: 40000
Eligible for Scheme A

26]marks = int(input("Enter marks: "))


if marks >= 90:
print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
elif marks >= 60:
print("Grade: D")
else:
print("Grade: F")

Output
Enter marks: 85
Grade: B

27]num = int(input("Enter a number (1-3): "))


if num == 1:
print("One")
elif num == 2:
print("Two")
elif num == 3:
print("Three")
else:
print("Invalid number")

Output

Enter a number (1-3): 2


Two

28]char = input("Enter a character (a, b, or c): ").lower()


if char == "a":
print("Apple")
elif char == "b":
print("Banana")
elif char == "c":
print("Cherry")
else:
print("Invalid character")

Output
Enter a character (a, b, or c): a
Apple

29]num = 1
while num <= 10:
print(num, end=" ")
num += 1

Output
1 2 3 4 5 6 7 8 9 10

30]num = 20
while True:
print(num, end=" ")
num -= 1
if num < 1:
break

Output
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
31]#include <stdio.h>

int main() {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
printf("%d ", i);
}
}
return 0;
}

Output: Prints even numbers from 2 to 100.

32]#include <stdio.h>

int main() {
int choice;
printf("Choose a color:\n1. Red\n2. Green\n3. Blue\n");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("You chose Red.\n");
break;
case 2:
printf("You chose Green.\n");
break;
case 3:
printf("You chose Blue.\n");
break;
default:
printf("Invalid choice.\n");
}
return 0;
}

Output: Displays the chosen color.

33]#include <stdio.h>

int main() {
for (int i = 1; i <= 50; i++) {

printf("%d ", i);

if (i % 5 == 0) {

printf("\n");

return 0;

Output: Prints numbers 1 to 50, five numbers per line.

34]#include <stdio.h>

int main() {

int count = 0;

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

if (i % 2 != 0) {

printf("%d ", i);

count++;

if (count % 5 == 0) {

printf("\n");

return 0;

}
Output: Prints odd numbers from 1 to 50, five numbers per line.

35]#include <stdio.h>

int factorial(int n) {

if (n == 0) return 1;

return n * factorial(n - 1);

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("Factorial of %d is %d\n", num, factorial(num));

return 0;

Output: Prompts for a number and prints its factorial.

36]#include <stdio.h>

void fibonacci(int n) {

int a = 0, b = 1, c;

printf("%d %d ", a, b);

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

c = a + b;

printf("%d ", c);


a = b;

b = c;

printf("\n");

int main() {

int terms;

printf("Enter number of terms: ");

scanf("%d", &terms);

fibonacci(terms);

return 0;

Output: Prompts for number of terms and prints Fibonacci sequence up to that term.

37]#include <stdio.h>

int isPrime(int num) {

if (num <= 1) return 0;

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

if (num % i == 0) return 0;

return 1;

}
int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (isPrime(num)) {

printf("%d is a prime number.\n", num);

} else {

printf("%d is not a prime number.\n", num);

return 0;

Output: Prompts for a number and checks if it is prime.

38]#include <stdio.h>

int main() {

int size;

printf("Enter the size of pattern: ");

scanf("%d", &size);

for (int i = 0; i < size; i++) {

for (int j = 0; j < size; j++) {

if ((i + j) % 2 == 0) {

printf("X ");
} else {

printf("O ");

printf("\n");

return 0;

Output: Displays an XO pattern based on the specified size.

39]#include <stdio.h>

int main() {

int rows = 4;

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

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

printf("%d", j);

printf("\n");

return 0;

Output: Prints a pyramid with each row displaying increasing numbers from 1 to the row
number:

Output

1
12

123

1234

40]#include <stdio.h>

int main() {

int rows = 4;

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

for (int j = rows; j >= rows - i + 1; j--) {

printf("%d", j);

printf("\n");

return 0;

Output

43

432

4321
41]#include <stdio.h>

int isGreaterThanTen(int num) {

return num > 10;

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (isGreaterThanTen(num)) {

printf("%d is greater than 10.\n", num);

} else {

printf("%d is not greater than 10.\n", num);

return 0;

Output: Checks if the number is greater than 10 and prints a message.

42. Function to generate multiplication table:

#include <stdio.h>

void multiplicationTable(int num) {


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

printf("%d x %d = %d\n", num, i, num * i);

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

multiplicationTable(num);

return 0;

Output: Prints the multiplication table for a specified number.

43. Function to calculate square and cube of a number:

#include <stdio.h>

void squareAndCube(int num, int *square, int *cube) {

*square = num * num;

*cube = num * num * num;

}
int main() {

int num, square, cube;

printf("Enter a number: ");

scanf("%d", &num);

squareAndCube(num, &square, &cube);

printf("Square: %d, Cube: %d\n", square, cube);

return 0;

Output: Prints the square and cube of a given number.

44. Function to calculate area of a triangle using global variables:

#include <stdio.h>

float base, height, area;

void calculateArea() {

area = 0.5 * base * height;

int main() {

printf("Enter base and height of the triangle: ");

scanf("%f %f", &base, &height);


calculateArea();

printf("Area of the triangle: %.2f\n", area);

return 0;

Output: Calculates and displays the area of a triangle using global variables.

45. Swap two numbers using pointers:

#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int main() {

int x, y;

printf("Enter two numbers: ");

scanf("%d %d", &x, &y);

swap(&x, &y);

printf("After swapping: x = %d, y = %d\n", x, y);

return 0;
}

Output: Swaps two numbers using pointers.

46. Find area of a triangle using pointers:

#include <stdio.h>

void calculateArea(float *base, float *height, float *area) {

*area = 0.5 * (*base) * (*height);

int main() {

float base, height, area;

printf("Enter base and height of the triangle: ");

scanf("%f %f", &base, &height);

calculateArea(&base, &height, &area);

printf("Area of the triangle: %.2f\n", area);

return 0;

Output: Calculates the area of a triangle using pointers.

47. Factorial of a number using recursion:


#include <stdio.h>

int factorial(int n) {

if (n == 0) return 1;

return n * factorial(n - 1);

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("Factorial of %d is %d\n", num, factorial(num));

return 0;

Output: Calculates the factorial of a number recursively.

48. Sum of n numbers using recursion:

#include <stdio.h>

int sum(int n) {

if (n == 0) return 0;

return n + sum(n - 1);


}

int main() {

int n;

printf("Enter n: ");

scanf("%d", &n);

printf("Sum of first %d numbers is %d\n", n, sum(n));

return 0;

Output: Calculates the sum of first n numbers recursively.

49. Math functions (example of some common operations):

#include <stdio.h>

#include <math.h>

int main() {

double num = 16;

printf("Square root: %.2f\n", sqrt(num));

printf("Power (2^3): %.2f\n", pow(2, 3));

printf("Cosine (0): %.2f\n", cos(0));

return 0;

}
Output: Displays the results of basic math operations.

50. 1D array to take input and print 10 numbers:

#include <stdio.h>

int main() {

int arr[10];

printf("Enter 10 numbers:\n");

for (int i = 0; i < 10; i++) {

scanf("%d", &arr[i]);

printf("You entered: ");

for (int i = 0; i < 10; i++) {

printf("%d ", arr[i]);

printf("\n");

return 0;

Output: Takes 10 numbers and displays them.

51. Sum and average of elements in a 1D array:


#include <stdio.h>

int main() {

int arr[10], sum = 0;

float avg;

printf("Enter 10 numbers:\n");

for (int i = 0; i < 10; i++) {

scanf("%d", &arr[i]);

sum += arr[i];

avg = sum / 10.0;

printf("Sum: %d, Average: %.2f\n", sum, avg);

return 0;

Output: Displays sum and average of 10 numbers.

52. Find max element of an array:

#include <stdio.h>

int main() {

int arr[10], max;

printf("Enter 10 numbers:\n");
for (int i = 0; i < 10; i++) {

scanf("%d", &arr[i]);

if (i == 0 || arr[i] > max) max = arr[i];

printf("Max element: %d\n", max);

return 0;

Output: Finds the maximum element in the array.

53. String functions (example of common operations):

#include <stdio.h>

#include <string.h>

int main() {

char str[100];

printf("Enter a string: ");

gets(str);

printf("Length of string: %zu\n", strlen(str));

printf("String in uppercase: %s\n", strupr(str));

return 0;

}
Output: Performs string operations like length and uppercase conversion.

54. 2D array to take input and print 3x3 numbers:

#include <stdio.h>

int main() {

int arr[3][3];

printf("Enter 9 numbers:\n");

for (int i = 0; i < 3; i++) {

Output The 3x3 matrix is:

1 2 3

4 5 6

7 8 9

55]#include <stdio.h>

int main() {

int rows, columns, count = 0;

// Input the size of the 2D array

printf("Enter the number of rows and columns: ");

scanf("%d %d", &rows, &columns);

int arr[rows][columns];
// Input elements into the 2D array

printf("Enter the elements of the array:\n");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

scanf("%d", &arr[i][j]);

if (arr[i][j] == 0) {

count++;

// Output the count of zeroes

printf("Number of zeroes in the array: %d\n", count);

return 0;

Output

Number of zeroes in the array: 3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy