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

30 Program

Uploaded by

Muhammad Sohaib
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)
15 views

30 Program

Uploaded by

Muhammad Sohaib
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/ 41

ASSIGNMENT

NAME: MUHAMMAD SOHAIB

ROLL NO: CU-2829-2022

CLASS: GF-08

SECTION: A

DEPARTMENT: BSCS

TOPIC: “PROGRAMS”

SUBJECT: PROGRAMMING FUNDAMENTALS

SUBMITTED TO: SIR BILAL

DATE: 13.02.23
PROGRAM 1:

#include <stdio.h>

int main() {

int weekday;

printf("Enter weekday number (1-7): ");

scanf("%d", &weekday);

switch (weekday) {

case 1:

printf("Monday\n");

break;

case 2:

printf("Tuesday\n");

break;

case 3:

printf("Wednesday\n");
break;

case 4:

printf("Thursday\n");

break;

case 5:

printf("Friday\n");

break;

case 6:

printf("Saturday\n");

break;

case 7:

printf("Sunday\n");

break;

default:

printf("ERROR: Invalid input. Please enter a number between 1 and 7.\n");

return 0;
}

PROGRAM 2:

num = int(input("Enter a whole number between 1 and 10: "))

if num < 1 or num > 10:

print("ERROR: The number you entered is outside the specified range.")

else:

print("Multiplication table of", num)

for i in range(1, 13):

print(num, "x", i, "=", num*i)

Program 3:

#include <stdio.h>

int main() {
int year;

printf("Enter a year: ");

scanf("%d", &year);

if (year % 4 == 0) {

if (year % 100 == 0) {

if (year % 400 == 0) {

printf("%d is a leap year.", year);

else {

printf("%d is not a leap year.", year);

else {

printf("%d is a leap year.", year);

}
else {

printf("%d is not a leap year.", year);

return 0;

Program 4:

#include <stdio.h>

int main() {

int num;

printf("Enter a multiple of 10 less than or equal to 50: ");

scanf("%d", &num);
switch (num) {

case 10:

printf("Ten\n");

break;

case 20:

printf("Twenty\n");

break;

case 30:

printf("Thirty\n");

break;

case 40:

printf("Forty\n");

break;

case 50:

printf("Fifty\n");

break;

default:

printf("ERROR: The number you entered is out of range.\n");

break;
}

return 0;

Program 5:

#include <stdio.h>

int main() {

int arr1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int arr2[10];

int i;

// Copy elements of arr1 to arr2

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

arr2[i] = arr1[i];

}
// Display elements of arr2

printf("Elements of arr2:\n");

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

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

printf("\n");

return 0;

Program 6:

#include <stdio.h>

int main() {

int arr[5];

int i;

// Input 5 integer values


for (i = 0; i < 5; i++) {

printf("Enter an integer value: ");

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

// Square each element of the array

for (i = 0; i < 5; i++) {

arr[i] = arr[i] * arr[i];

// Display the overwritten elements of the array

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

for (i = 0; i < 5; i++) {

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

printf("\n");

return 0;

}
Program 7:

#include <stdio.h>

int main() {

int n, i;

float sum = 0, avg;

// Input the size of the array

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

scanf("%d", &n);

int arr[n];

// Input the elements of the array

for (i = 0; i < n; i++) {

printf("Enter element %d: ", i+1);

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

// Calculate the average of the array elements

avg = sum / n;

// Display the average

printf("Average of the array elements: %.2f\n", avg);

return 0;

Program 8:

#include <stdio.h>

int main() {

int arr1[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

int i, j;
// Display the elements of the 2D array

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

for (j = 0; j < 3; j++) {

printf("%d ", arr1[i][j]);

printf("\n");

return 0;

Program 9:

#include <stdio.h>

int main() {

float arr[4][4];

int i, j;

float sum = 0;
// Input 16 float values from the user

for (i = 0; i < 4; i++) {

for (j = 0; j < 4; j++) {

printf("Enter value for element [%d][%d]: ", i, j);

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

sum += arr[i][j];

// Display the elements of the 4x4 array

printf("Elements of the array:\n");

for (i = 0; i < 4; i++) {

for (j = 0; j < 4; j++) {

printf("%.2f ", arr[i][j]);

printf("\n");

}
// Display the sum of all the elements of the array

printf("Sum of all the elements of the array: %.2f\n", sum);

return 0;

Program 10:

#include <stdio.h>

int main() {

int arr[3][3];

int i, j;

// Input 9 integer values from the user and store them in the 3x3 array

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

for (j = 0; j < 3; j++) {

printf("Enter value for element [%d][%d]: ", i, j);

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

// Display the elements of the 3x3 array

printf("Elements of the array:\n");

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

for (j = 0; j < 3; j++) {

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

printf("\n");

// Display the diagonal of the 3x3 array

printf("Diagonal elements of the array: ");

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

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

printf("\n");
return 0;

Program 11:

#include <stdio.h>

int main() {

int arr[3][3];

int i, j;

int even_count = 0, odd_count = 0;

// Input 9 integer values from the user and store them in the 3x3 array

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

for (j = 0; j < 3; j++) {

printf("Enter value for element [%d][%d]: ", i, j);

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

}
// Display the elements of the 3x3 array

printf("Elements of the array:\n");

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

for (j = 0; j < 3; j++) {

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

printf("\n");

// Count the number of even and odd elements in the 3x3 array

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

for (j = 0; j < 3; j++) {

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

even_count++;

} else {

odd_count++;

}
// Display the number of even and odd elements in the 3x3 array

printf("Number of even elements in the array: %d\n", even_count);

printf("Number of odd elements in the array: %d\n", odd_count);

return 0;

Program 12:

#include <stdio.h>

int main() {

int num1, num2, sum;

printf("Enter first number: ");

scanf("%d", &num1);

printf("Enter second number: ");


scanf("%d", &num2);

sum = num1 + num2;

printf("Sum of %d and %d is %d\n", num1, num2, sum);

return 0;

Program 13:

#include <stdio.h>

int main() {

int num, i, fact = 1;

printf("Enter a number: ");

scanf("%d", &num);

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


fact *= i;

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

return 0;

Program 14:

#include <stdio.h>

#include <math.h>

int main() {

int num, originalNum, remainder, n = 0;

float result = 0.0;

printf("Enter an integer: ");

scanf("%d", &num);
originalNum = num;

// count number of digits

while (originalNum != 0) {

originalNum /= 10;

++n;

originalNum = num;

// calculate result

while (originalNum != 0) {

remainder = originalNum % 10;

result += pow(remainder, n);

originalNum /= 10;

if ((int)result == num) {
printf("%d is an Armstrong number.\n", num);

} else {

printf("%d is not an Armstrong number.\n", num);

return 0;

Program 15:

#include <stdio.h>

#include <string.h>

int main() {

char str[100];

printf("Enter a string: ");

scanf("%s", str);

// reverse the string


strrev(str);

printf("Reversed string: %s\n", str);

return 0;

Program 16:

#include <stdio.h>

int main() {

int a[10][10], b[10][10], c[10][10];

int r1, c1, r2, c2;

int i, j, k;

printf("Enter rows and columns for the first matrix: ");

scanf("%d %d", &r1, &c1);

printf("Enter rows and columns for the second matrix: ");


scanf("%d %d", &r2, &c2);

// check if multiplication is possible

if (c1 != r2) {

printf("Error! Column of first matrix is not equal to row of second matrix.");

return 1;

// input elements of the first matrix

printf("\nEnter elements of the first matrix:\n");

for (i = 0; i < r1; i++) {

for (j = 0; j < c1; j++) {

printf("Enter element a[%d][%d]: ", i, j);

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

// input elements of the second matrix

printf("\nEnter elements of the second matrix:\n");


for (i = 0; i

Program 17:

#include <stdio.h>

int main() {

printf("Hello, world!");

return 0;

Program 18:

#include <stdio.h>

int main() {

int num1, num2, sum;

printf("Enter first number: ");

scanf("%d", &num1);

printf("Enter second number: ");

scanf("%d", &num2);

sum = num1 + num2;


printf("Sum is %d", sum);

return 0;

Program 19:

#include <stdio.h>

int main() {

int num1, num2, num3, largest;

printf("Enter first number: ");

scanf("%d", &num1);

printf("Enter second number: ");

scanf("%d", &num2);

printf("Enter third number: ");

scanf("%d", &num3);

largest = num1;

if (num2 > largest) {

largest = num2;

}
if (num3 > largest) {

largest = num3;

printf("Largest number is %d", largest);

return 0;

Program 20:

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num % 2 == 0) {

printf("%d is even", num);

} else {

printf("%d is odd", num);

}
return 0;

Program 21:

#include <stdio.h>

int main() {

char ch;

printf("Enter a character: ");

scanf("%c", &ch);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch ==

'O' || ch == 'U') {

printf("%c is a vowel", ch);

} else {

printf("%c is a consonant", ch);

return 0;

}
Program 22:

#include <stdio.h>

int main() {

int num, fact = 1, i;

printf("Enter a number: ");

scanf("%d", &num);

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

fact = fact * i;

printf("Factorial of %d is %d", num, fact);

return 0;

Program 23:

#include <stdio.h>
int main() {

int n, t1 = 0, t2 = 1, nextTerm;

printf("Enter the number of terms: ");

scanf("%d", &n);

printf("Fibonacci Series: ");

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

printf("%d, ", t1);

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

return 0;

Program 24:
#include <stdio.h>

int main() {

int num1, num2, gcd;

printf("Enter two integers: ");

scanf("%d %d", &num1, &num2);

for (int i = 1; i <= num1 && i <= num2; ++i) {

if (num1 % i == 0 && num2 % i == 0)

gcd = i;

printf("GCD of %d and %d is %d", num1, num2, gcd);

return 0;

Program 25:
#include <stdio.h>

int main() {

float celsius, fahrenheit;

printf("Enter temperature in Celsius: ");

scanf("%f", &celsius);

fahrenheit = (celsius * 1.8) + 32;

printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);

return 0;

Program 26:

#include <stdio.h>

#include <string.h>
int main() {

char str[100];

printf("Enter a string: ");

gets(str);

printf("Reverse of the string: ");

for (int i = strlen(str) - 1; i >= 0; i--)

printf("%c", str[i]);

return 0;

Program 27:

#include <stdio.h>

int main() {

int low, high, i, flag;


printf("Enter two numbers(intervals): ");

scanf("%d %d", &low, &high);

printf("Prime numbers between %d and %d are: ", low, high);

while (low < high) {

flag = 0;

for (i = 2; i <= low / 2; ++i) {

if (low % i == 0) {

flag = 1;

break;

if (flag == 0)

printf("%d ", low);

++low;
}

return 0;

Program 28:

#include <stdio.h>

int main() {

float radius, area;

printf("Enter the radius of circle: ");

scanf("%f", &radius);

area = 3.14159 * radius * radius;

printf("Area of circle is: %.2f", area);

return 0;
}

Program 29:

#include <stdio.h>

#define MAX_SIZE 100

void bubbleSort(int arr[], int n);

int main()

int arr[MAX_SIZE], i, n;

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

scanf("%d", &n);

if (n > MAX_SIZE)

{
printf("Error: Maximum array size exceeded.\n");

return 1;

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

for (i = 0; i < n; i++)

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

bubbleSort(arr, n);

printf("Sorted array in ascending order:\n");

for (i = 0; i < n; i++)

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

return 0;
}

void bubbleSort(int arr[], int n)

int i, j, temp;

for (i = 0; i < n - 1; i++)

for (j = 0; j < n - i - 1; j++)

if (arr[j] > arr[j + 1])

temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}
Program 30:

#include <stdio.h>

int main()

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num > 0)

printf("%d is positive.\n", num);

else if (num < 0)

{
printf("%d is negative.\n", num);

else

printf("%d is zero.\n", num);

return 0;

You might also like