PROGRAMMING FUNDAMENTALS
SUBMITTED TO:
DR. KASHIF BILAL
SUBMITTED BY:
WIHAJ TAHIR (FA22-BSE-158)
DATE OF SUBMISSION:-
4thJUNE,2023
Page1|20
QUESTION NO 01:
ANSWER:
#include <stdio.h>
Void swap(int *a, int *b) {
Int temp = *a;
*a = *b;
*b = temp;
}
Int main() {
Int num1, num2;
Printf(“Enter two numbers: “);
Scanf(“%d %d”, &num1, &num2);
Printf(“Before swapping: num1 = %d, num2 = %d\n”, num1, num2);
Swap(&num1, &num2);
Printf(“After swapping: num1 = %d, num2 = %d\n”, num1, num2);
Return 0;
}
QUESTION NO 02:
ANSWER:
#include <stdio.h>
Int checkEvenOdd(int num) {
If (num % 2 == 0)
Return 1; // Even
Else
Page2|20
Return 0; // Odd
}
Int main() {
Int number;
Printf(“Enter a number: “);
Scanf(“%d”, &number);
If (checkEvenOdd(number))
Printf(“%d is even.\n”, number);
Else
Printf(“%d is odd.\n”, number);
Return 0;
}
QUESTION NO 03:
ANSWER:
#include <stdio.h>
Int isPrime(int num) {
If (num <= 1)
Return 0; // Not prime
For (int I = 2; I * I <= num; i++) {
If (num % I == 0)
Return 0; // Not prime
}
Return 1; // Prime
}
Page3|20
Int main() {
Int number;
Printf(“Enter a number: “);
Scanf(“%d”, &number);
If (isPrime(number))
Printf(“%d is a prime number.\n”, number);
Else
Printf(“%d is not a prime number.\n”, number);
Return 0;
}
QUESTION NO 04:
ANSWER:
#include <stdio.h>
Unsigned long long factorial(int num) {
Unsigned long long fact = 1;
For (int I = 1; I <= num; i++)
Fact *= I;
Return fact;
}
Int main() {
Int number;
Printf(“Enter a number: “);
Scanf(“%d”, &number);
Page4|20
Printf(“Factorial of %d is %llu\n”, number, factorial(number));
Return 0;
}
QUESTION NO 05:
ANSWER:
#include <stdio.h>
Double power(double base, double exponent) {
Double result = 1;
While (exponent != 0) {
Result *= base;
--exponent;
}
Return result;
}
Int main() {
Double base, exponent;
Printf(“Enter base number: “);
Scanf(“%lf”, &base);
Printf(“Enter exponent: “);
Scanf(“%lf”, &exponent);
Printf(“%.2lf ^ %.2lf = %.2lf\n”, base, exponent, power(base, exponent));
Return 0;
Page5|20
}
QUESTION NO 06:
ANSWER:
#include <stdio.h>
#include <math.h>
Double squareRoot(double num) {
If (num < 0) {
Printf(“Square root of negative number is not defined.\n”);
Return 0.0;
}
Return sqrt(num);
}
Int main() {
Double number;
Printf(“Enter a number: “);
Scanf(“%lf”, &number);
Printf(“Square root of %.2lf is %.2lf\n”, number, squareRoot(number));
Return 0;
}
QUESTION NO 07:
ANSWER:
#include <stdio.h>
Page6|20
Int gcd(int num1, int num2) {
While (num1 != num2) {
If (num1 > num2)
Num1 -= num2;
Else
Num2 -= num1;
}
Return num1;
}
Int main() {
Int number1, number2;
Printf(“Enter two numbers: “);
Scanf(“%d %d”, &number1, &number2);
Printf(“GCD of %d and %d is %d\n”, number1, number2, gcd(number1, number2));
Return 0;
}
QUESTION NO 08:
ANSWER:
#include <stdio.h>
Int gcd(int num1, int num2) {
While (num1 != num2) {
If (num1 > num2)
Num1 -= num2;
Else
Num2 -= num1;
Page7|20
}
Return num1;
}
Int lcm(int num1, int num2) {
Return (num1 * num2) / gcd(num1, num2);
}
Int main() {
Int number1, number2;
Printf(“Enter two numbers: “);
Scanf(“%d %d”, &number1, &number2);
Printf(“LCM of %d and %d is %d\n”, number1, number2, lcm(number1, number2));
Return 0;
}
QUESTION NO 09:
ANSWER:
#include <stdio.h>
Int binomialCoefficient(int n, int k) {
Int res = 1;
If (k > n – k)
K = n – k;
For (int I = 0; I < k; i++) {
Page8|20
Res *= (n – i);
Res /= (I + 1);
}
Return res;
}
Void printPascalTriangle(int numRows) {
For (int n = 0; n < numRows; n++) {
For (int k = 0; k <= n; k++) {
Printf(“%d “, binomialCoefficient(n, k));
}
Printf(“\n”);
}
}
Int main() {
Int rows;
Printf(“Enter the number of rows: “);
Scanf(“%d”, &rows);
printPascalTriangle(rows);
return 0;
}
Page9|20