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

Programs ict

The document is an assignment from the Islamia University of Bahawalpur for a Discrete Structures course, authored by Milhan Zahid. It includes a series of C programming examples covering basic concepts such as input/output, arithmetic operations, control structures, and simple algorithms. Each example is presented with code snippets and brief descriptions of their functionality.

Uploaded by

milhanzahid222
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)
5 views

Programs ict

The document is an assignment from the Islamia University of Bahawalpur for a Discrete Structures course, authored by Milhan Zahid. It includes a series of C programming examples covering basic concepts such as input/output, arithmetic operations, control structures, and simple algorithms. Each example is presented with code snippets and brief descriptions of their functionality.

Uploaded by

milhanzahid222
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/ 8

Islamia University of Bahawalpur

DISCRETE STRUCTURES ASSIGNMENT

Name Roll No.

Milhan Zahid F24BDATS1M02012

Professor’s Name Submission Date

Miss Sidra Saleem 05-01-2025


Department of Data Science

PROGRAMS IN C
1. Say “Hello World”:
#include <stdio.h>

int main () {
printf ("Hello, World! \n");
return 0;
}
2. Area of rectangle:
#include <stdio.h>

int main () {
double length, width, area;
printf ("Enter the length of the rectangle: ");
scanf ("%lf", &length);
printf ("Enter the width of the rectangle: ");
scanf ("%lf", &width);
area = length * width;
printf ("The area of the rectangle is: %.2lf\n", area);
return 0;
}
3. Speed and Time to calculate Distance:

#include <stdio.h>

int main () {
double speed, time, distance;
printf ("Enter the speed in kilometers per hour: ");
scanf ("%lf", &speed);
printf ("Enter the time in hours: ");
scanf ("%lf", &time);
distance = speed * time;
printf ("Distance traveled: %.2lf kilometers\n", distance);
return 0;
}

4. Adding two Numbers:


#include <stdio.h>

int main () {
int num1, num2, sum;
printf ("Enter two numbers: ");
scanf ("%d %d", &num1, &num2);
sum = num1 + num2;
printf ("The sum of %d and %d is: %d\n", num1, num2, sum);
return 0;
}
5. Triangle Pattern:
#include <stdio.h>

int main () {
printf (" *\n");
printf (" ***\n");
printf (" *****\n");
printf (" ********\n");
return 0;
}

6. Division of two Number:


#include <stdio.h>

int main () {
double x, y;
printf ("Enter the first number (x): ");
scanf ("%lf", &x);
printf ("Enter the second number (y): ");
scanf ("%lf", &y);

if (y != 0) {
printf ("Floating-point Division (x / y): %.2lf\n", x / y);
} else {
Printf ("Error: Division by zero is not allowed.\n");
}

return 0;
}
7. Multiplication of two Numbers:
#include <stdio.h>

int main () {
double x, y;
printf ("Enter the first number (x): ");
scanf ("%lf", &x);
printf ("Enter the second number (y): ");
scanf ("%lf", &y);

printf ("Multiplication (x * y): %.2lf\n", x * y);

return 0;
}
8. Pass or Fail:
#include <stdio.h>

int main () {
int number;
printf ("Enter a number: ");
scanf ("%d", &number);
if (number >= 50) {
printf("PASS\n");
} else {
printf("Fail\n");
}
return 0;
}

9. Positive or Negative:
#include <stdio.h>

int main () {
double number;
printf ("Enter a number: ");
scanf ("%lf", &number);

if (number > 0) {
printf ("The number %.2lf is positive.\n", number);
} else if (number < 0) {
printf ("The number %.2lf is negative.\n", number);
} else {
printf ("The number is zero.\n");
}

return 0;
}
10. Prefix and Postfix Increment:
#include <stdio.h>

int main () {
int x;
printf ("Enter a number: ");
scanf ("%d", &x);

printf ("Postfix: %d\n", x++);


printf ("Prefix: %d\n", ++x);
return 0;
}
11. Print 10 numbers by While Loop:

#include <stdio.h>

int main () {
int num = 1;
while (num <= 10) {
printf ("%d \n ", num);
num++;
}
return 0;
}

12. Eligibility for Voting:


#include <stdio.h>

int main() {
char citizenship[10];
int age;

printf("Enter your citizenship status (yes or no): ");


scanf("%s", citizenship);

printf("Enter your age: ");


scanf("%d", &age);

if (citizenship[0] == 'y' && citizenship[1] == 'e' && citizenship[2] == 's') {


if (age >= 18) {
printf ("You are eligible to vote.\n");
} else {
printf ("You are not eligible to vote due to age.\n");
}
} else {
printf ("You are not eligible to vote due to citizenship.\n");
}

return 0;
}

13. Vowel or Consonant Checker:


#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("The character '%c' is a vowel.\n", ch);
} else {
printf("The character '%c' is a consonant.\n", ch);
}

return 0;
}

14. Days of Week:


#include <stdio.h>

int main() {
int day;
printf("Enter a number (1-7) to represent a day of the week: ");
scanf("%d", &day);
switch(day) {
case 1: printf("Sunday\n"); break;
case 2: printf("Monday\n"); break;
case 3: printf("Tuesday\n"); break;
case 4: printf("Wednesday\n"); break;
case 5: printf("Thursday\n"); break;
case 6: printf("Friday\n"); break;
case 7: printf("Saturday\n"); break;
default: printf("Invalid day number.\n");
}
return 0;
}

15. Prefix and Postfix Decrement:

#include <stdio.h>

int main() {
int x;
printf ("Enter a number: ");
scanf ("%d", &x);

printf("Postfix: %d\n", x--);


printf("Prefix: %d\n", --x);

return 0;
}

16. Grade of Student:


#include <stdio.h>

int main() {
int marks;
printf("Enter marks (0-100): ");
scanf("%d", &marks);

if (marks >= 90) {


printf("Grade: A\n");
} else if (marks >= 80) {
printf("Grade: B\n");
} else if (marks >= 70) {
printf("Grade: C\n");
} else if (marks >= 60) {
printf("Grade: D\n");
} else {
printf("Grade: F\n");
}

return 0;
}

17. Area and Perimeter of Triangle:


#include <stdio.h>

int main() {
double length, width, area, perimeter;

printf("Enter the length of the rectangle: ");


scanf("%lf", &length);

printf("Enter the width of the rectangle: ");


scanf("%lf", &width);

area = length * width;


perimeter = 2 * (length + width);

printf("Area: %.2lf\n", area);


printf("Perimeter: %.2lf\n", perimeter);

return 0;
}

18. Calculator:
#include <stdio.h>

int main() {
char op;
double num1, num2;

printf("Enter operator (+, -, *, /): ");


scanf(" %c", &op);

printf("Enter two numbers: ");


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

switch (op) {
case '+':
printf("Result: %.2lf\n", num1 + num2);
break;
case '-':
printf("Result: %.2lf\n", num1 - num2);
break;
case '*':
printf("Result: %.2lf\n", num1 * num2);
break;
case '/':
if (num2 != 0) {
printf("Result: %.2lf\n", num1 / num2);
} else {
printf("Error: Division by zero.\n");
}
break;
default:
printf("Invalid operator.\n");
}

return 0;
}

You might also like