0% found this document useful (0 votes)
221 views34 pages

10TH Class All Activities and Excercise Program

The document contains examples of C programs that take user input, perform calculations, and output results. The programs demonstrate basic concepts like input/output, variables, operators, conditional statements, loops, and functions. Examples include programs that calculate area of a square, total number of balls, salary calculations, temperature conversions and more to teach fundamental programming skills.

Uploaded by

Shaani sunny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
221 views34 pages

10TH Class All Activities and Excercise Program

The document contains examples of C programs that take user input, perform calculations, and output results. The programs demonstrate basic concepts like input/output, variables, operators, conditional statements, loops, and functions. Examples include programs that calculate area of a square, total number of balls, salary calculations, temperature conversions and more to teach fundamental programming skills.

Uploaded by

Shaani sunny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

COMPUTER SCIENCE

10

Al Noor Science Academy

~1~ Created by Sir Hammad Hassan


COMPUTER SCIENCE UNIT 2

/ Activity 2.2**********************************************************************
Write a program that shows your first name in Uppercase and your last name in lower case letters on
screen.
***********************************************************************************/
#include <stdio.h> OUTPUT:
int main()
{ HAMMAD hassan
printf("HAMMAD hassan");

/ Activity 2.3**********************************************************************

Write a program that takes roll number, percentage of marks and grade from user as input.
***********************************************************************************/
#include <stdio.h>
#include <conio.h>
int main() OUTPUT:
{ Please Enter Your Roll Number:101
int rollnumber; // input rollnumber of student Please Enter Your Percentage:75.0
float percentage; // input percentage of student Please Enter Your Grade:A
char grade; // input grade of student

printf(“Please Enter Your Roll Number:”);


scanf(“%d”,&rollnumber); Roll No: 101
Percentage: 75.000000
Grade: A
printf(“Please Enter Your Percentage:”);
scanf(“%f”,& percentage);

printf(“Please Enter Your Grade:”);


scanf(“%c”,& grade);

printf(“\n\n\n\n Roll No: \t %d”,rollnumber);


printf(“\n Percentage: \t %f”,percentage);
printf(“\n Grade: \t %c”,grade);
getch();

~2~
/ Activity 2.4**********************************************************************

Write a program that takes as input the length of one side of a square and calculates the area of
square.
***********************************************************************************/
#include <stdio.h>
#include <conio.h> OUTPUT:
int main() Please Enter One Side of Square Length:12
{ Area of Square is: 144
int one_side_length, area_of_square;

printf(“Please Enter One Side of Square Length:”);


sacnf(“%d”,& one_side_length);

area_of_square= one_side_length* one_side_length;

printf(“Area of Square is: %d”, area_of_square);


getch();
}
/ Activity 2.5**********************************************************************

Write a program that takes as input the number of balls in jar A and the number of balls in jar B. The
program calculates and displays the total number of balls.
***********************************************************************************/
#include <stdio.h>
int main() OUTPUT:
{ Please Enter the number of balls in Jar A: 50
int total_balls, jarA_balls,jarB_balls; Please Enter the number of balls in Jar B: 20

printf("Please Enter the number of balls in Jar A:"); Total Number of Balls are: 70
scanf(“%d”,&jarA_balls);

printf("Please Enter the number of balls in Jar B:");


scanf(“%d”,&jarB_balls);

total_balls= jarA_balls+jarB_balls;

printf(“\nTotal Number of Balls are:%d”, total_balls);


}

~3~
/ Activity 2.6**********************************************************************

Write a program that takes original price of a shirt and discount percentage from user. Program
should display the original price of shirt, discount on price and price after discount.
***********************************************************************************/
#include <stdio.h>
#include <conio.h>
int main()
{
int original_price, discount_price, discount_percentage, price_after_dicount;

printf (“Please Enter Original Price:”); OUTPUT:


scanf(“%d”, &original_price); Please Enter Original Price: 800
printf (“Please Enter Discount Percentage:”); Please Enter Discount Percentage:40.0
scanf(“%d”, &discount_percentage); Discount Price is:320
Price After Discount is:480
discount_price= original_price* discount_percentage/100;

printf(“Discount Price is: %d”, discount_price);

price_after_dicount= original_price - discount_price;

printf(“\nPrice After Discount is: %d”, price_after_dicount);


getch();
}
/ Activity 2.7**********************************************************************

Write a program that takes 2 digit number from user, computes the product of both digits and show
the output.
***********************************************************************************/
#include <stdio.h>
#include <conio.h>
int main() OUTPUT:
{ Please enter the first digit number: 50
Please enter the second digit number:10
int digit1, digit2;
Product of first and second digit is:500
printf (“Please enter the first digit number:”);
scanf(“%d”,& digit1);
printf (“Please enter the second digit number:”);
scanf(“%d”,& digit2);
printf(“\nProduct of first and second digit is: %d”, digit1*digit2);
getch();
}

~4~
/ Activity 2.8**********************************************************************

Write a program that takes seconds as input and calculates equivalent number of hours, minutes and
seconds.
***********************************************************************************/
#include <stdio.h>
#include <conio.h>
int main()
{ OUTPUT:
float seconds, minutes, hours; Please enter the seconds:3600

printf(“Please enter the seconds:”);


Total seconds:3600.00
scanf(“%f”,& seconds);
Total Minutes:60.00
Total Hours:1.00
minutes=seconds/60;
hours=minutes/60;

printf(“\nTotal seconds: %.2f\nTotal Minutes: %.2f \nTotal Hours: %.2f”,seconds,minutes,hours);


getch();
}

/ Activity 2.9**********************************************************************

Convert the following algebraic expressions into C expressions.


***********************************************************************************/
#include <stdio.h>
#include <conio.h>
int main()
{
printf(“ x= 6y+z \nx=yz^3 + 3y \nz= x+y^2/3x \nz= (x-2)^2+3y \n y=(x+3z/2)+z^3+x/y”);
getch();
}
OUTPUT:
x=6*y+z
x=y*z*z+3*y
z=x+y*y/3*x
z=(x-2)^2+3*y
y+(x+3*z/2)+z*z*z+x/z

~5~
/ Exercise 1**********************************************************************

The criteria for calculation of wages in a company are given below.


Basic Salary =Pay Rate Per Hour X Working Hours Of Employee
Overtime Salary =Overtime Pay Rate X Overtime Hours Of Employee
Total Salary = Basic Salary + Working Hours Of Employee

Write a program that should take working hours and overtime hours of employee as input. The
program should calculate and display the total salary of employee.
***********************************************************************************/
#include <stdio.h>
int main()
{
int Basic_Salary, Overtime_Salary, Total_Salary,Working_Hours, Overtime_Hours;

printf("Please Enter Working Hours of Employee:");


scanf("%d",&Working_Hours); OUTPUT:
Please Enter Working Hours of Employee:8
printf("Please Enter Overtime Hours of Employee:"); Please Enter Overtime Hours of Employee:4
scanf("%d",&Overtime_Hours);
Total Salary of Employee is :2800RS
Basic_Salary=Working_Hours*200;
Overtime_Salary=Overtime_Hours*300;

printf("\nTotal Salary of Employee is : %dRS" , Basic_Salary+Overtime_Salary);


}
/ Exercise 2**********************************************************************
Write a program that takes Celsius temperature as input, converts the temperature into Fahrenheit
and show the output. Formula for conversion of temperature from Celsius to Fahrenheit is:
F=9/5C+32
***********************************************************************************/
#include <stdio.h>
OUTPUT:
int main()
Please Enter Celsius Temperature:30
{
float Celsius_Temperature;
The Fahrenheit Temperature is:86.000000
printf("Please Enter Celsius Temperature:");
scanf("%f",&Celsius_Temperature);

printf("\nThe Fahrenheit Temperature is: %f", 9.0/5*Celsius_Temperature+32);


}

~6~
/ Exercise 3**********************************************************************

Write a program that display the following output using single printf statement:
* * * *
1 2 3 4
***********************************************************************************/
#include <stdio.h>
int main()
{
printf("*\t*\t*\t*\t\n1\t2\t3\t4");
}
/ Exercise 4**********************************************************************
Write a program that display the following output using single printf statement:
I am a Boy
I live in Pakistan
I am a proud Pakistani
***********************************************************************************/
#include <stdio.h>
int main() OUTPUT:
{ I am Boy
printf("I am Boy\nI Live in Pakistan\nI am a Proud Pakistani"); I Live in Pakistan
I am a Proud Pakistani
}
/ Exercise 5**********************************************************************

A clothing brand offers 15% discount on each item. A lady buys 5 shirts from this brand. Write a
program that calculates total price after discount and amount of discounts availed by lady. Original
prices of the shirts are: Shirt1 = 423, Shirt1 = 320, Shirt1 = 270, Shirt1 = 680, Shirt1 = 520.
***********************************************************************************/
#include <stdio.h>
int main() OUTPUT:
{ Total Price is:2213
int Shirt1=423,Shirt2=320,Shirt3=270,Shirt4=680,Shirt5=520; Total Discount is:331
int Total_Price, Total_Discount, Final_Price; Final Price is:1882

Total_Price=Shirt1+Shirt2+Shirt3+Shirt4+Shirt5;
Total_Discount=(Total_Price*15)/100;

Final_Price=Total_Price - Total_Discount;

printf("Total Price is:%d \nTotal Discount is: %d \nFinal Price is: %d",Total_Price,Total_Discount,Final_Price);
}

~7~
/ Exercise 6**********************************************************************

Write a program that swaps the values of two integer variable without help of any third variable.
***********************************************************************************/
#include <stdio.h>
#include <conio.h>
OUTPUT:
int main()
Before swap a=10 b=20
{
After swap a=20 b=10
int a=10, b=20;

printf("Before swap a=%d b=%d",a,b);

a=a+b; //a=30 (10+20)


b=a-b; //b=10 (30-20)
a=a-b; //a=20 (30-10)

printf("\nAfter swap a=%d b=%d",a,b);


getch();
}

/ Exercise 7**********************************************************************

Write a program that takes a 5 digit number as input, calculate and displays the sum of first and last
digit of number.
***********************************************************************************/
#include <stdio.h>
#include <conio.h> OUTPUT:
int main() Enter a number with length 5 digits:12345
{ The Sum of first and fifth digit is:6
int number;

printf("Enter a number with length 5 digits:");


scanf("%d",&number);

printf("The Sum of first and fifth digit is: %d", (number/10000)+(number%10));


getch();
}

~8~
/ Exercise 8**********************************************************************

Write a program that takes monthly income and monthly expense of the user like electricity bill, gas
bill, food expense.
***********************************************************************************/
#include <stdio.h>
#include <conio.h>
int main()
{
int Monthy_Income, Electricity_Bill, Gas_bill, Food_Expense, Monthy_Expenses, Monthy_Savings,Yearly_Expenses;

printf("Please Enter your Monthy Income:"); OUTPUT:


scanf("%d",&Monthy_Income); Please Enter your Monthy Income:30000
Please Enter your Monthy Expenses:
printf("Please Enter your Monthy Expenses:"); Your Electricity Bill:2000
Your Gas Bill:250
printf("\n\n\tYour Electricity Bill:"); Your Food Expense:10000
scanf("%d",&Electricity_Bill);
Total Monthly Expenses are:12250
printf("\tYour Gas Bill:"); Total Yearly Expenses are:147000
scanf("%d",&Gas_bill);
Total Monthly Savings is:62766
printf("\tYour Food Expense:"); Total Yearly Savings are:753192
scanf("%d",&Food_Expense);
Average Saving Per Month:62766
Monthy_Expenses=Electricity_Bill+Gas_bill+Food_Expense; Average Expense Per Month:12250

printf("\n\n\tTotal Monthly Expenses are:%d", Monthy_Expenses);


printf("\n\tTotal Yearly Expenses are:%d", Monthy_Expenses*12);

Monthy_Savings=Monthy_Income + Monthy_Savings;

printf("\n\n\tTotal Monthly Savings is:%d", Monthy_Savings);


printf("\n\tTotal Yearly Savings are:%d", Monthy_Savings*12);
printf("\n\n\tAverage Saving Per Month:%d", Monthy_Savings);
printf("\n\tAverage Expense Per Month:%d", Monthy_Expenses);

getch();
}

~9~
/ Exercise 9**********************************************************************
Write a program that takes a character and number of steps as input from user. Program should then
jump number of steps from of steps from that character.
***********************************************************************************/
#include <stdio.h>
#include <conio.h>
int main()
OUTPUT:
{
Enter Character:a
char c;
Enter Steps:2
int steps;
New Character:c

printf("Enter Character:");
scanf("%c",&c);

printf("Enter Steps:");
scanf("%d",&steps);

int x = c + steps;

printf("New Character: %c", x);


getch();
}
/ Exercise 10**********************************************************************

Write a program that takes radius of a circle as input. The program should calculate and display the
area of circle.

***********************************************************************************/
#include <stdio.h>
#include <conio.h> OUTPUT:
int main() Please Enter The Radius of Circle:36
{ Area of Circle is:4069.44
float radius;

printf("Please Enter The Radius of Circle:");


scanf("%f",&radius);

printf("\nArea of Circle is: %.2f", 3.14*radius*radius);


getch();
}

~ 10 ~
COMPUTER SCIENCE UNIT 3

/ Activity 3.1**********************************************************************
Write a program that takes the age of a person as an input and displays “Teenager” if the age lies
between 13 and 19.
***********************************************************************************/
#include <stdio.h>
int main()
OUTPUT:
{
int age; Enter Your Age:15
You are Teenager
printf(“ Enter Your Age:”);
scanf(“%d”, &age);

if(age .>= 13 && age<19)


printf(“You are Teenager”);
else
printf(“You are NOT Teenager”);
}
/ Activity 3.2**********************************************************************
Write a program that takes year as input and display “leap year” if the input year is leap year. Leap
year is divisible by 4.
***********************************************************************************/
#include <stdio.h>
int main()
{ OUTPUT:
int year;
Enter The Year in Numbers:2021
printf(“ Enter The Year in Numbers:”);
scanf(“%d”, &year); NOT a Leap Year

if(year %4 == 0)

printf(“Leap Year”);
else
printf(“NOT a Leap Year”);
}

~ 11 ~
/ Activity 3.3**********************************************************************
Write a program that takes the value of body temperature of a person as an input and displays “You
have fever”. If body temperature is more than 98.6 otherwise displays “You don’t have fever.”
***********************************************************************************/

#include <stdio.h>

int main()
OUTPUT:
{
float temp; Enter Body Temperature:98

printf(“ Enter Body Temperature:”); You don’t have fever


scanf(“%f”, &temp);

if(temp > 98.6)


printf(“You have fever”);
else
printf(“You don’t have fever”);
}
/ Activity 3.4**********************************************************************

***********************************************************************************/
#include <stdio.h>

int main()
{
float percentage; OUTPUT:

printf(“ Enter Percentage of Student:”); Enter Percentage of Student:77.3


scanf(“%f”, &percentage);
Eligible BSCS program

if(percentage >= 80)


printf(“Eligible BSSE program”);
else if(percentage >= 70)
printf(“Eligible BSCS program”);
else if(percentage >= 60)
printf(“Eligible BSIT program”);
else
printf(“You are NOT Eligible”);

~ 12 ~
/ Activity 3.5**********************************************************************

***********************************************************************************/

#include <stdio.h>
#include <conio.h> OUTPUT:
int main() Enter First Number:2
{ Enter Second Number:6
int a,b,c ; Enter Choice from 1 to 4:3

printf(“ Enter First Number:”); Multiplication:12


scanf(“%d”, &a);
printf(“ Enter Second Number:”);
scanf(“%d”, &b);
printf(“ Enter Choice from 1 to 4:”);
scanf(“%d”, &c);

if(c == 1)
printf(“Addition %d\n” , a + b);
if(c == 2)
printf(“Subtraction %d\n” , a - b);
if(c == 3)
printf(“Multiplication %d\n” , a * b);
if(c == 4)
printf(“Division %d\n” , a / b);
getch();
}

~ 13 ~
/ Activity 3.6**********************************************************************
Write a program that finds and displays area of triangle , parallelogram, rhombus or trapezium
according to the choice of user.
***********************************************************************************/

#include <stdio.h>
#include <conio.h>
int main()
{
int choice ;
float triangle,parallelogram;
printf(“ Find Area\n”);
printf(“ 1. Triangle\n 2. Parallelogram \n Enter Your Choice:”);
scanf(“%d”, & choice);

if(choice==1)
{ OUTPUT:
float base,height;
printf(“ Enter Base:”); Find Area
scanf(“%f”,&base); 1. Triangle
printf(“ Enter Height:”); 2. Parallelogram
scanf(“%f”, &height); Enter Your Choice:1
triangle = 0.5 *base * height; Enter Base:30
printf(“ Area of a triangle is: %f”, triangle); Enter Hieght:40
} Area of a triangle is: 600.000000
else if(choice==2)
{
float base,height;
printf(“ Enter Base:”);
scanf(“%f”,&base);
printf(“ Enter Height:”);
scanf(“%f”, &height);
parallelogram = base * height;
printf(“ Area of a triangle is: %f”, parallelogram);
}
else
printf(“invalid choice”);
getch();
}

~ 14 ~
/ Exercise 1**********************************************************************

Write a program that takes two integers as input and tells whether first one is a factor of the second
one?
***********************************************************************************/

#include <stdio.h>
#include <conio.h>
int main()
OUTPUT:
{
int num1, num2; Please Enter The First Number: 6
Please Enter The Second Number: 12
printf("Please Enter The First Number:"); First Number is Factor of Second Number
scanf("%d",&num1);
printf("Please Enter The Second Number:");
scanf("%d",&num2);

if(num2%num1==0)
printf(“First Number is Factor of Second Number”);
else
printf(“First Number is NOT A Factor of Second Number”);
getch();
}
/ Exercise 2**********************************************************************

Write a program that takes a number as input and display “YES” if the input number is a multiple of 3,
and has 5 in unit’s place e.g. 15, 75.
***********************************************************************************/
#include <stdio.h>
#include <conio.h>
int main() OUTPUT:
{
Please Enter a Number:15
int num;
Yes

printf("Please Enter a Number:");


scanf("%d",&num);

if(num%3==0 && num%10==5)


printf(“Yes”);
getch();
}

~ 15 ~
/ Exercise 3**********************************************************************

***********************************************************************************/
#include <stdio.h>
#include <conio.h>
int main() OUTPUT:
{
int total_bill; Please Enter the Total Bill:5000
Discount =35
printf("Please Enter the Total Bill:"); Discount price =1750.000000
scanf("%d", & total_bill);

if(total_bill > =1000 && total_bill < 2499)


printf(“ Discount =%d \n Discount price = %f ” , 10, total_bill * 0.1);
else
if(total_bill >=2500 && total_bill < 4999)
printf(“ Discount =%d \n Discount price = %f ” , 20, total_bill * 0.2);
else
if(total_bill >=5000 && total_bill < 9999)
printf(“ Discount =%d \n Discount price = %f ” , 35, total_bill * 0.35);
else
if(total_bill >=10000)
printf(“ Discount =%d \n Discount price = %f ” , 50, total_bill * 0.5);

getch();
}

~ 16 ~
/ Exercise 4**********************************************************************

Write a program that takes as input, the original price and sale price of a product and tells whether
the product is sold on profit or loss. The program should also tell the profit/loss percentage.
***********************************************************************************/
#include <stdio.h>
#include <conio.h>
OUTPUT:
int main()
{ Please Enter the Total Original Price:800
float org_price, sale_price, profit, loss;
Please Enter the Total Sale Price:1000
printf("Please Enter the Total Original Price:");
Your Profit is:200.00 Your Profit Percentage is:50.00
scanf("%f", & org_price);
printf("Please Enter the Total Sale Price:");
scanf("%f", & sale_price);

if(org_price < sale_price )


{
profit = sale_price - org_price;
printf(“Your Profit is: % .2f\t Your Profit Percentage is: %.2f” , profit, (profit/ org_price*100));
}
else {
loss = org_price- sale_price ;
printf(“Your Loss is: % .2f\t Your Loss Percentage is: %.2f” , loss, (loss/ org_price*100));
}

getch();
}

~ 17 ~
/ Exercise 5**********************************************************************

Write a program that takes as input, the lengths of 3 sides of a triangle and tells whether it is a right
angle triangle or not. For right angle triangle,

hypotenuse2 = base2 + height2


***********************************************************************************/
#include <stdio.h>
#include <conio.h> OUTPUT:
int main() Please Enter the Base of Triangle:10
{ Please Enter the Height of the Triangle:5
int hypotenuse, base, height; Please Enter the Hypotenuse of the Triangle:8
It is NOT a right angle Triangle.
printf("Please Enter the Base of Triangle:");
scanf("%d", &base);
printf("Please Enter the Height of the Triangle:");
scanf("%d", &height);
printf("Please Enter the Hypotenuse of the Triangle:");
scanf("%d", & hypotenuse);

if(hypotenuse * hypotenuse == base * base + height * height)


printf(“ \nIt is a right angle Triangle. ”);
else
printf(“ \nIt is NOT a right angle Triangle. ”);
getch();
}

~ 18 ~
/ Exercise 6**********************************************************************

Write a program that takes as input, the obtained and total marks of Matric, Intermediate, Entrance
Test. The program shulod tekk whether the student is eligible or not.
***********************************************************************************/
#include <stdio.h>
#include <conio.h>
int main()
{
float obt_mark_Matric , total_mark_Matric , obt_mark_Inter , total_mark_Inter, obt_mark_Test,
total_mark_Test;
float matric_percentage, inter_percentage, test_percentage;

printf("Please Enter Obtain Marks in Matriculation:");


OUTPUT:
scanf("%f", & obt_mark_Matric);
Please Enter Obtain Marks in Matriculation:1050
printf("Please Enter Total Marks in Matriculation:");
Please Enter Total Marks in Matriculation:1100
scanf("%f", & total_mark_Matric);
Please Enter Obtain Marks in Intermediate:1000
Please Enter Total Marks in Intermediate:1100
printf("Please Enter Obtain Marks in Intermediate:");
Please Enter Obtain Marks in Entry Test:150
scanf("%f", & obt_mark_Inter);
Please Enter Total Marks in Entry Test:200
printf("Please Enter Total Marks in Intermediate:");
scanf("%f", & total_mark_Inter);
The Student is Eligible.
printf("Please Enter Obtain Marks in Entry Test:");
scanf("%f", & obt_mark_Inter);
printf("Please Enter Total Marks in Entry Test:");
scanf("%f", & total_mark_Inter);

matric_percentage = obt_mark_Matric/ total_mark_Matric*100.0;


inter_percentage = obt_mark_Inter/ total_mark_Inter*100.0;
test_percentage = obt_mark_Test/ total_mark_Test*100.0;

if(matric_percentage >= 60.0 && inter_percentage >= 65.0 && test_percentage >= 65.0 )
printf(“ \nThe Student is Eligible. ”);
else
printf(“ \nThe Student is NOT Eligible. ”);
getch();
}

~ 19 ~
/ Exercise 7**********************************************************************

***********************************************************************************/
#include <stdio.h>
#include <conio.h>
int main()
{
int salary, experience, bonus_task; OUTPUT:
Please Enter the Salary of Employee:10000
printf("Please Enter the Salary of Employee:"); Please Enter the Experience of Employee:2
scanf("%d", & salary); Please Enter the Bouns of Employee:5
printf("Please Enter the Experience of Employee:"); Bouns is: 1500
scanf("%d", & experience);
printf("Please Enter the Bouns of Employee:");
scanf("%d", & bonus_task);

if(salary == 10000 && experience == 2 && bonus_task == 5 )


printf(“ Bouns is: 1500 ”);
else if(salary == 10000 && experience == 3 && bonus_task == 10 )
printf(“ Bouns is: 2500 ”);
else if(salary == 25000 && experience == 3 && bonus_task == 4 )
printf(“ Bouns is: 2000 ”);
else if(salary == 75000 && experience == 4 && bonus_task == 7 )
printf(“ Bouns is: 3500 ”);
else if(salary == 10000 && experience == 5 && bonus_task == 10 )
printf(“ Bouns is: 5000 ”);
getch();
}

~ 20 ~
COMPUTER SCIENCE UNIT 4

Programming Time 4.5: Write a program that 5 times displays the numbers from 1-10 on
computers screen.

#include <stdio.h>
int main() Output:
{ 12345678910
for (int i= 1; i<=5; i++) 12345678910
{ 12345678910
for (int j=1; j<=10; j++) 12345678910
{ 12345678910
printf(“%d”,j);
}
printf(“\n”);
}
}

Programming Time 4.6: Write a program to display the following pattern of stars on
screen.

*
**
***
****
*****
******
. #include <stdio.h>
int main() Output:
{ *
for (int i= 1; i<=6; i++) **
{ ***
****
for (int j=1; j<=i; j++)
*****
{
******
printf(“*”);
}
printf(“\n”);
}
}

~ 21 ~
/ Activity 4.1**********************************************************************

Write a program that displays table of 2.


***********************************************************************************/
#include <stdio.h>
#include <conio.h>
int main() Output:
{ 2*1=2
int n=2; 2*2=4
for (int i=1; i<=10; i++) 2*3=6
printf(“%d *%d=%d\n”,n , i , n*i); 2*4=8
getch(); 2 * 5 = 10
}
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20

/ Activity 4.2**********************************************************************
Write a program that displays the tables of 2, 3, 4, 5 and 6.
***********************************************************************************/
#include <stdio.h>
Output:
#include <conio.h>
Multiplication Tables from 2 to 6
int main()
2*1=2
{
2*2=4
int i,j, n=6;
2*3=6
printf(“ Multiplication Tables from 2 to 6\n”);
2*4=8
for (i=2;i<=n; i++)
2 * 5 = 10
{
2 * 6 = 12
for (j=1;j<=10;j++)
2 * 7 = 14
printf(“%d*%d=%d\t\n”,i,j,i*j);
2 * 8 = 16
}
2 * 9 = 18
printf(“\n”);
2 * 10 = 20
getch();
} 3*1=3
3*2=6
3*3=9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
~ 22 ~ 3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
/ Activity 4.3**********************************************************************
Write a program that takes as input the marks obtained in matriculation by 30 students of a class. The
program should display the average marks of the class.
***********************************************************************************/
#include <stdio.h>
#include <conio.h> Output:
int main() Enter Obtained Marks of 30 Students:505
{ 405
int marks[30], total=0; 250
float avg; 475
printf(“Enter Obtained Marks of 30 Students:”); 450
for (int i=0; i<=30; i++) 415
{ 418
scanf(“%d”,&marks[i]); 246.
total=total+marks[i]; .
} .
avg= total/30; .
printf(“The Average of 30 Student is %.2f,avg); .
getch(); The Average of 30 Student is: 602.00
}

~ 23 ~
/ Exercise 1**********************************************************************
Use loops to print the following patterns on console.
a)
*****
*****
*****
b)
A
BC
DEF
GHIJ
KLMNO
***********************************************************************************/
#include <stdio.h>
#include <conio.h> Output:
int main()
*****
{ *****
for (int i=0; i<3; i ++) *****
{
for (int j=0; j<5; j ++)
printf(“*”);
printf(“\n”);
}
getch();
}
*******************************************************************************
#include <stdio.h>
#include <conio.h> Output:
int main() A
{ BC
Char ch=’A’;
DEF
for (int j =1; j <=5; j ++) GHIJ
{ KLMNO
for (int i =1; i<=j; i ++)
{
printf(“%c”,ch++);
}

printf(“\n”);
}
getch();
}

~ 24 ~
/ Exercise 4**********************************************************************
Write a program that takes two positive integers a and b as input and displays the value of a b.
***********************************************************************************/
#include <stdio.h>
#include <conio.h> Output:
int main()
Please Enter the Value of A: 2
{
Please Enter the Value of A: 3
int a, b, sum=1;
printf(“Please Enter the Value of A:”);
The Answer of A power B is: 8
scanf(“%d”,&a);
printf(“Please Enter the Value of B:”);
scanf(“%d”,&b);

for (int i =1; i <=b; i++)


sum=sum*a;
printf(“ The Answer of A power B is: %d”,sum);
getch();
}
/ Exercise 4**********************************************************************
Write a program that takes two number as input and displays their Greatest Common Divisor (GCD)
using Euclidean method.
***********************************************************************************/
#include <stdio.h>
#include <conio.h> Output:
int main() Please Enter the First number:15
{ Please Enter the Second number:3
int num1,num2,r;
printf(“Please Enter the First number:”); The GCD is:3
scanf(“%d”,&num1);
printf(“Please Enter the Second number:”);
scanf(“%d”,&num2);

while (num1% num2 >0){


r=num1%num2;
num1=num2;
num2=r;
}
Printf(“The GCD is: %d” ,num2);
getch();
}

~ 25 ~
/ Exercise 4**********************************************************************
Write a program to display factroials number from 1 to 7. (Hint: Use Nested Loop)
***********************************************************************************/
#include <stdio.h>
#include <conio.h>
Output:
int main()
The factorial of 1: is 1
{
The factorial of 2: is 2
int j, factorial=1;
The factorial of 3: is 12
for (int i =1; i<=7; i++)
The factorial of 4: is 72
{
The factorial of 5: is 480
for (j=1; j<=i; j++)
The factorial of 6: is 3600
{
The factorial of 7: is 30240
factorial= factorial*j;
}
printf(“The factorial of %d: is %d\n”, i , factorial);
factorial=i;
}
getch();
}

~ 26 ~
/ Exercise 4**********************************************************************
Write a program that takes 10 numbers as input in an array and displays the product of first and last
element on console.
***********************************************************************************/
#include <stdio.h>
int main() Output:
{ Please Enter 1/10 Number:1
int numbers[9]; Please Enter 2/10 Number:2
for (int i=0; i<10; i++) Please Enter 3/10 Number:3
{ Please Enter 4/10 Number:4
printf(“Please Enter %d/10 Numbers:”,i+1); Please Enter 5/10 Number:5
scanf(“%d”,&numbers[i]); Please Enter 6/10 Number:6
} Please Enter 7/10 Number:7
Please Enter 8/10 Number:8
Please Enter 9/10 Number:9
Please Enter 10/10 Number:10
The product of First and Last Number is :10

printf(“The product of First and Last Number is : %d”, numbers[0]*numbers[9]);

/ Exercise 4**********************************************************************
Write a program that declares and initializes an array of 7 elements and tells how many elements in
the array are greater than 10.

***********************************************************************************/
#include <stdio.h>
#include <conio.h>
int main()
{ Output:
int count=0, numbers[6]={4,8,11,15,19,2}; The elements in the array Greater than 10 are: 3

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


if(numbers[i]>10)
count=count+1;
}
printf(“The elements in the array Greater than 10 are:%d”, count);
getch();
}

~ 27 ~
COMPUTER SCIENCE UNIT 5

/ Exercise 1**********************************************************************
Write a function int square (int x); to calculate the square of an integer x.

***********************************************************************************/
#include <stdio.h>
#include <conio.h>
int square (int x)
{ Output:
return x*x; Please Enter Number to calculate Square:5
}
Square of 5 is: 25
int main()
{
int number;
printf(“Please Enter Number to calculate Square:”);
scanf(“%d”,&number);
printf(“Square of %d is: %d”, number, square (number));
getch();
}

~ 28 ~
/ Exercise 2**********************************************************************
Write a function int power (int x, int y); to calculate and return x y.

***********************************************************************************/
#include <stdio.h>
#include <conio.h> Output:
Int power (int x, int y) Please Enter the value of X:5
{ Please Enter the value of Y:3
int i, result =1;
if (y == 0) X the power of Y is:125
return 1;
for ( i=1; i <=y; i++)
result = result *x ;
return result;
}

int main()
{
int num1,num2;
printf(“Please Enter the value of X:”);
scanf(“%d”,&num1);
printf(“Please Enter the value of Y:”);
scanf(“%d”,&num2);

Printf(“\nX the power of Y is: %d”, power(num1,num2));


getch();
}

~ 29 ~
/ Exercise 3**********************************************************************
Write a function to calculate factorial of a number .

***********************************************************************************/
#include <stdio.h>
#include <conio.h>
Output:
int factorial (int x)
Please enter a number to calculate Factorial:6
{
The factorial of 6: is 720
int sum =1;
for (int i =1; i <=x; i++)
sum = sum*i;
return sum;
}

int main()
{
int fact;
printf(“Please enter a number to calculate Factorial:”);
scanf(“%d”, &fact);
printf(“The factorial of %d: is %d”, fact , factorial(fact));
getch();
}

~ 30 ~
/ Exercise 4**********************************************************************
Write a function which takes values for three angles of a triangle and prints whether the given values
make a valid triangle or not. A valid triangle is the one where the sum of three angles is equal to 180.

***********************************************************************************/
#include <stdio.h>
#include <conio.h> Output:
int triangle (int x, int y, int z) Please Enter First angle of the Triangle:60
{ Please Enter Second angle of the Triangle:60
int sum = x + y + z; Please Enter Third angle of the Triangle:60
if ( sum== 180 && x> 0 && y >0 && z >0) This is a valid Triangle
printf(“This is a valid Triangle”);
else
printf(“This is NOT a valid Triangle”);
}

int main()
{
int firstangle, secondangle, thirdangle ;
printf(“Please Enter First angle of the Triangle:”);
scanf (“%d”, &firstangle);
printf(“Please Enter Second angle of the Triangle:”);
scanf (“%d”, &secondangle);
printf(“Please Enter Third angle of the Triangle:”);
scanf (“%d”, &thirdangle);

triangle (firstangle, secondangle, thirdangle);


getch();
}

~ 31 ~
/ Exercise 5**********************************************************************
Write a function which takes the amount and the interest percentage and return the interest amount.

***********************************************************************************/
#include <stdio.h>
#include <conio.h>
Output:
float calculateInterest (float x, float y) Please Enter the Amount:11001
{ Please Enter the Interest Percentage:5
float interest= y/100*x; Your Interest Amount is:550.049988
return interest;
}
int main()
{
float amount, interestPersentage;
printf(“Please Enter the Amount:”);
scanf(“%f”, &amount);
printf(“Please Enter the Interest Percentage:”);
scanf(“%f”, & interestPersentage);

printf(“ Your Interest Amount is: %f”, calculateInterest (amount, interestPersentage));


getch();
}

~ 32 ~
/ Exercise 6**********************************************************************
Write a function which takes a number as input and display its digits with spaces in between.

***********************************************************************************/
#include <stdio.h>
#include <conio.h>

void spaces (int number)


Output:
{
Please Enter a Number:2345
If (x>10)
2345
{
space (x/10);
printf ( “ “);
}
printf(“%d”,x%10);
}
int main()
printf(“Please Enter a Number:”);
scanf(“%d”, &number);
spaces (number);

getch();
}

~ 33 ~
/ Exercise 7**********************************************************************
Write a function to print the table of a number.

***********************************************************************************/
#include <stdio.h>
#include <conio.h>

Output:
void printTable (int x)
{ Please enter a Number to print Table:3
3*1=3
for (int I = 1; i<= 10; i++)
3*2=6
printf(“ %d 8 %d = %d\n” , x,i,i*x);
3*3=9
} 3 * 4 = 12
3 * 5 = 15
int main() 3 * 6 = 18
{ 3 * 7 = 21
int number; 3 * 8 = 24
printf(“Please enter a Number to print Table:”); 3 * 9 = 27
scanf(“%d”,&number); 3 * 10 = 30
printTable(number);

getch();
}

~ 34 ~

You might also like