Uploaded by Hamza MG

Programs and Algorithms to write in PPS record

advertisement
Programs and Algorithms to write in PPS
record
Q1) write a program to print an integer.
Source code:
Output:
Algorithm:
Step1: start
Step2: declare an integer value and print using %d specifier
Step3: stop
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Q2) write a program to add two integers.
Source code:
#include <stdio.h>
int main(){
int a = 10 , b = 20, c;
c = a + b ;
printf("Sum is : %d",c);
}
Output:
Sum is : 30
Algorithm:
Step1: start
Step2: declare three variables a, b and c
Step3: c=a+b
Step4: print c
Step5: stop
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Q3) write a program to compute quotient and remainder.
Source code:
#include <stdio.h>
int main(){
int a = 20 , b = 10, quo, rem;
quo = a / b ;
rem = a % b ;
printf("quotient is : %d\nremainder is: %d",quo,rem);
}
Output:
quotient is : 2
remainder is: 0
Algorithm:
Step1: start
Step2: declare four variables a, b, quo, rem
Step3: quo = a / b ;
rem = a % b ;
Step4: print quotient and remainder
Step5: stop
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Q4) write a program to all the arithmetic operators in C.
#include <stdio.h>
int main(){
int a = 20 , b = 10,sum, dif, mul, quo, rem;
sum = a + b ;
dif = a - b ;
mul = a * b ;
quo = a / b ;
rem = a % b ;
printf("sum is: %d\ndifference is: %d\nproduct is: %d\nquotient is :
%d\nremainder is: %d"
,sum,dif,mul,quo,rem);
}
Output:
sum is: 30
difference is: 10
product is: 200
quotient is : 2
remainder is: 0
Algorithm:
Step1: start
Step2: declare the variables a= 20 , b = 10,sum, dif, mul, quo, rem
Step3: sum = a + b
dif = a - b
mul = a * b
quo = a / b
rem = a % b
Step4: stop
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Q5) write a program to print the area of a circle.
#include <stdio.h>
int main(){
float rad,area;
printf("Enter the radios of circle: ");
scanf("%f",&rad);
area=3.14*rad*rad;
printf("area is : %0.2f\n",area);
return 0;
}
Output:
Enter the radios of circle: 5
area is : 78.50
Algorithm:
Step1: start
Step2: accept the radios value and use the formula pi*(r*r) to calculate the area of the circle
Step3: print area
Step4: stop
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Q6) write a program to check whether a number is even or
odd.
#include <stdio.h>
int main(){
int a;
printf("Enter a number: ");
scanf("%d",&a);
if (a % 2 == 0) printf("%d is an Even Number\n",a);
else printf("%d is an Odd Number\n",a);
return 0;
}
Output:
Enter a number: 5
5 is an Odd Number
Algorithm:
Step1: start
Step2: input a number and store it in variable “a”
Step3: print number is even if number is divisible by 2 else print number is odd
Step4: Stop
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Q7) write a program to calculate HRA, PF and SPA of an
employee.
#include <stdio.h>
int main(){
int ID;
float salary, HRA, PF, SPA;
printf("Enter Employee ID: ");
scanf("%d",&ID);
printf("Enter Employee Salary: ");
scanf("%f",&salary);
HRA = (salary * 12)/100;
SPA = salary/12;
PF = salary/10;
printf("Salary is %0.2f\n",salary);
printf("HRA is %0.2f\n",HRA);
printf("SPA is %0.2f\n",SPA);
printf("PF is %0.2f\n",PF);
return 0;
}
Output:
Enter Employee ID: 167022733128
Enter Employee Salary: 135000
Salary is 135000.00
HRA is 16200.00
SPA is 11250.00
PF is 13500.00
Algorithm:
Step1: start
Step2: input the value of employee ID and salary
Step3: calculate and print HRA, Spa and PF
Step4: stop
-----------------------------------------DRAW FLOWCHART----------------------------------------Q8) write a program to calculate percentage and grade of a student.
#include <stdio.h>
int main(){
int max;
float m1, phy, pps, eeee, per;
printf("Enter M1 Marks out of 100: ");
scanf("%f",&m1);
printf("Enter Physics Marks out of 100: ");
scanf("%f",&phy);
printf("Enter PPS Marks out of 100: ");
scanf("%f",&pps);
printf("Enter EE&EE Marks out of 100: ");
scanf("%f",&eeee);
per = (m1+phy+pps+eeee)/4;
printf("\nYour Mark Percentage is %0.2f",per);
if (per<=100 && per>=81) printf("\nYour Grade Is A");
if (per<=80 && per>=61) printf("\nYour Grade Is B");
if (per<=60 && per>=41) printf("\nYour Grade Is D");
if (per<=40 && per>=33) printf("\nYour Grade Is D");
if (per<=32 && per>=0) printf("\nYour Grade Is F");
return 0;
}
Output:
Enter M1 Marks out of 100: 93.5
Enter Physics Marks out of 100: 88
Enter PPS Marks out of 100: 98.5
Enter EE&EE Marks out of 100: 90
Your Mark Percentage is 92.50
Your Grade Is A
Algorithm:
Step1: start
Step2: input M1, physics, eeee and pps marks from user
Step3: calculate percentage = (m1+phy+pps+eeee)/4
Step4: print the grade of the student using their percentage
Step5: stop
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Q9) write a program to compare three numbers.
#include <stdio.h>
int main(){
int a, b, c;
printf("Enter 1st Number: ");
scanf("%d",&a);
printf("Enter 2nd Number: ");
scanf("%d",&b);
printf("Enter 3rd Number: ");
scanf("%d",&c);
if (a>b && a>c) printf("%d is Greatest\n",a);
if (b>a && b>c) printf("%d is Greatest\n",b);
else printf("%d is Greatest\n",c);
}
Output:
Enter 1st Number: 70
Enter 2nd Number: 125
Enter 3rd Number: 28
125 is Greatest
Algorithm:
Step1: start
Step2: input any three numbers from the user stored in a, b and c
Step3: check if a is greater than b and a is greater than c, if true, print a is greatest. Similarly check for b,
c and print the greatest number.
Step4: stop
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Q10) write a program to find size of variables.
#include <stdio.h>
int main(){
int a;
char b;
float c;
double d;
printf("Size Of integer variable is %zu bytes\n",sizeof(a));
printf("Size Of character variable is %zu bytes\n",sizeof(b));
printf("Size Of float variable is %zu bytes\n",sizeof(c));
printf("Size Of double variable is %zu bytes\n",sizeof(d));
return 0;
}
Output:
Size Of integer variable is 4 bytes
Size Of character variable is 1 bytes
Size Of float variable is 4 bytes
Size Of double variable is 8 bytes
Algorithm:
Step1: start
Step2: declare a variable for all datatypes
Step3: use %zu specifier in print statement and sizeof() to find the number of bytes each variable
occupies.
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Q11) write a program to print n prime numbers.
#include <stdio.h>
int main()
{
int i,num,count,n;
printf("Enter the value of n: ");
scanf("%d",&n);
printf("Prime numbers are:-\n");
for(num=2;num<=n;num++)
{
count=0;
for(i=1;i<=num;i++)
{ if(num%i==0) count++; }
if(count==2) printf("%d ",num);
}
}
Output:
Enter the value of n: 30
Prime numbers are:2 3 5 7 11 13 17 19 23 29
Algorithm:
Step1: start
Step2: input the number till where the user needs prime numbers
Step3: for num = 2 to n
a. for i = 1 to num
b. if num % i = 0
c. then increment count
d. if count is equal to 2
e. then print num value
Step4: stop
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Q12) write a program to print multiplication table based on
choice of user.
#include <stdio.h>
int main(){
int i;
float num, mul;
printf("Enter any number: ");
scanf("%f",&num);
for (i=1;i<=10;i++){
mul = num*i;
printf("%.2f X %d = %.2f\n",num,i,mul);
}
return 0;
}
Output:
Enter any number: 7.3
7.30 X 1 = 7.30
7.30 X 2 = 14.60
7.30 X 3 = 21.90
7.30 X 4 = 29.20
7.30 X 5 = 36.50
7.30 X 6 = 43.80
7.30 X 7 = 51.10
7.30 X 8 = 58.40
7.30 X 9 = 65.70
7.30 X 10 = 73.00
Algorithm:
Step1: start
Step2: input a number form the user to print its multiplication table.
Step3: declare a for loop where i<=10 and for each value of i, i is multiplied with num and the value is
printed.
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Q13) write a program to implement calculator usung
conditional statements.
#include <stdio.h>
int main(){
int a,b,sum,pro,div,diff,mod,choice,exit=0;
printf("Enter 1st number: ");
scanf("%d",&a);
printf("Enter 2nd number: ");
scanf("%d",&b);
printf("Calculator\n1.Sum\n2.Difference\n3.Product\n4.Quotient\n5.Reminder\n");
sum=a+b;
diff=a-b;
pro=a*b;
div=a/b;
mod=a%b;
printf("Enter your choice: ");
scanf("%d",&choice);
if (choice==1) printf("Sum is %d\n",sum);
if (choice==2) printf("Difference is %d\n",diff);
if (choice==3) printf("Product is %d\n",pro);
if (choice==4) printf("Quotient is %d\n",div);
if (choice==5) printf("Remainder is %d\n",mod);
}
Output:
Enter 1st number: 30
Enter 2nd number: 5
Calculator
1.Sum
2.Difference
3.Product
4.Quotient
5.Reminder
Enter your choice: 4
Quotient is 6
Algorithm:
Step1: start
Step2: input two numbers a, b from user
Step3: print 1.Sum, 2.Difference, 3.Product, 4.Quotient, 5.Reminder and read the choice from user
Step4: perform the operations and using conditional statements, print the required result.
-----------------------------------------DRAW FLOWCHART----------------------------------------Q14) write a program to display pyramid numbers.
#include <stdio.h>
int main(){
int i,j,n;
printf("Enter value of n: ");
scanf("%d",&n);
for (i=1;i<n;i++){
for(j=1;j<=i;j++){
printf("%d",j);
}
printf("\n");
}
return 0;
}
Output:
Enter value of n: 5
1
12
123
1234
Algorithm:
Step1: start
Step2: declare variables i, j, n
Step3: take value of n from user
Step4: for (i=1;i<n;i++){
for(j=1;j<=i;j++){
printf("%d",j);}
Step5: stop
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Q15) write a program to find whether the number entered is Armstrong
or not.
#include <stdio.h>
int main(){
int sum=0, r=0, n, m;
printf("Enter a number:");
scanf("%d",&n);
m=n;
while(n>0){
r=n%10;
sum = sum+(r*r*r);
n=n/10;
}
if (sum==m) printf("%d is an Armstrong number",m);
else {printf("%d is not an Armstrong number",m);}
return 0;
}
Output:
Enter a number: 153
153 is an Armstrong number
Algorithm:
Step1: start
Step2: sum=0, r=0, n, m
Step3: input value of n
Step4: r=n%10
n=n/10
sum=sum+(r*r*r)
Step5: repeat step4 until n>0
Step6: if sum equals to m, print its an Armstrong number else print is is not an Armstrong number
Step7: stop
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Q16) write a program to print pascal triangle.
#include <stdio.h>
int main() {
int i, space, rows, k = 0, count = 0, count1 = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
++count;
}
while (k != 2 * i - 1) {
if (count <= rows - 1) {
printf("%d ", i + k);
++count;
} else {
++count1;
printf("%d ", (i + k - 2 * count1));
}
++k;
}
count1 = count = k = 0;
printf("\n");
}
return 0;
}
Output:
Enter the number of rows: 5
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
Algorithm:
Step1: start
Step2: declare variables i, space, rows, k = 0, count = 0, count1 = 0
Step3: input the limits from user
Step4: for (space = 1; space <= rows - i; ++space) {
printf(" ");
++count;}
Step5: while (k != 2 * i - 1) {
if (count <= rows - 1) {
printf("%d ", i + k);
++count;}
else {
++count1;
printf("%d ", (i + k - 2 * count1));}
++k;}
Step6: count1 = count = k = 0;
printf("\n");
step7: stop
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Q17) write a program to find the sum of even and odd numbers from an
array.
#include <stdio.h>
void main(){
int i,size,even_sum=0,odd_sum=0;
printf("Enter the size of array: ");
scanf("%d",&size);
int array[size];
for (i=0;i<size;i++){
printf("Enter element %d: ",i+1);
scanf("%d",&array[i]);
}
for (i=0;i<size;i++){
if(array[i]%2==0) even_sum+=array[i];
else odd_sum+=array[i];
}
printf("Even Sum is %d\nOdd Sum is %d\n",even_sum,odd_sum);
}
Output:
Enter the size of array: 5
Enter element 1: 12
Enter element 2: 75
Enter element 3: 28
Enter element 4: 13
Enter element 5: 21
Even Sum is 40
Odd Sum is 109
Algorithm:
Step1: start
Step2: declare variables I, size, odd_sum, even_sum
Step3: input size of array from user
Step4: declare an array of the given size and declare a for loop to accept the elements of the array
Step5: for (i=0;i<size;i++){
if(array[i]%2==0) even_sum+=array[i];
else odd_sum+=array[i];}
Step6: print odd sum and even sum
Step7: stop
-----------------------------------------DRAW FLOWCHART----------------------------------------Q18) write a program to find maximum and minimum values form an array.
#include <stdio.h>
void main(){
int size,i,max,min;
printf("Enter the size of array: ");
scanf("%d",&size);
int array[size];
for (i=0;i<size;i++){
printf("Enter element %d: ",i+1);
scanf("%d",&array[i]);
}
max=array[0];
min=array[0];
for (i=1;i<size;i++){
if(array[i]>max) max=array[i];
if(array[i]<min) min=array[i];
}
printf("Max value is %d\nMin value is %d\n",max,min);
}
Output:
Enter the size of array: 5
Enter element 1: 123
Enter element 2: 213
Enter element 3: 231
Enter element 4: 223
Enter element 5: 312
Max value is 312
Min value is 123
Algorithm:
Step1: start
Step2: declare variables size, i, max, min
Step3: input size of array from user
Step4: declare an array of the given size and declare a for loop to accept the elements of the array
Step5: let max=a[0] and min=a[0]
Step6: declare a for loop that checks every element of array and compares it with the max and min
values, if a[i]>max, max=a[i]. Similarly if a[i]<min, min=a[i]
Step7: print max and min values
Step8: stop
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Q19) write a program to find the roots of a quadratic equation.
#include <stdio.h>
#include <math.h>
int main(){
double a,b,c,dis,root1,root2;
printf("From a Quadratic equation of form ax^2 + bx + c = 0\n");
printf("Enter the value of a: ");
scanf("%lf",&a);
printf("Enter the value of b: ");
scanf("%lf",&b);
printf("Enter the value of c: ");
scanf("%lf",&c);
dis=b*b-4*a*c;
if (dis>0){
root1=(-b + sqrt(dis))/(2*a);
root2=(-b - sqrt(dis))/(2*a);
printf("Roots are Real And Distinct\nRoot1 = %.2lf\nRoot2= %.2lf ",root1,root2);
}
else if (dis==0){
root1=-b/(2*a);
printf("Roots are Real And Equal\nRoots= %.2lf , %.2lf",root1,root1);
}
else {printf("Roots Are IMAGINARY\n");}
return 0;}
Output:
Enter the value of a: 1
Enter the value of b: -4
Enter the value of c: 4
Roots are Real And Equal
Roots= 2.00 , 2.00
Algorithm:
Step1: start
Step2: declare variables a, b, c, dis, root1, root2
Step3: dis=b*b-4*a*c
Step4: if (dis>0){ root1=(-b + sqrt(dis))/(2*a); root2=(-b - sqrt(dis))/(2*a); }. Roots are real and distinct.
Step5: if (dis==0){ root1=root2=-b/(2*a);}. Roots are real and equal
Step6: else the roots are imaginary.
Step7: stop
-----------------------------------------DRAW FLOWCHART----------------------------------------Q20)
i)write a program to find sinx value using series expansion.
#include<stdio.h>
void main()
{
int i, n;
float x, sum, term;
printf(" Enter the angle in degrees : ");
scanf("%f",&x);
printf(" Enter the number of terms : ");
scanf("%d",&n);
x=x*3.14/180;
term=x;
sum=x;
for(i=1;i<=n;i++){
term=(term*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+term;
}
printf(" Sine value = %.4f",x,sum);}
Output:
Enter the angle in degrees : 45
Enter the number of terms : 20
Sine value = 0.7850
Algorithm:
Step1: start
Step2: declare integer variables i, n and float variables x, sum, term
Step3: input the angle from user in x and the number of terms in n.
Step4: x=x*3.14/180, term=x, sum=x
Step5: for(i=1;i<=n;i++){
term=(term*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+term;}
Step6: print the value of sinx stored in sum
Step7: stop
-----------------------------------------DRAW FLOWCHART----------------------------------------ii) write a program to find sinx value using series expansion.
#include<stdio.h>
void main()
{
int i, n;
float x, sum=1, term=1;
printf(" Enter the angle in degrees: ");
scanf("%f",&x);
printf(" Enter the number of terms : ");
scanf("%d",&n);
x=x*3.14/180;
for(i=1;i<=n;i++){
term=term*(-1)*x*x/(2*i*(2*i-1));
sum=sum+term;
}
printf(" Cosine value = %.4f", x, sum);}
Output:
Enter the angle in degrees: 45
Enter the number of terms : 20
Cosine value = 0.7850
Algorithm:
Step1: start
Step2: declare integer variables i, n and float variables x, sum, term
Step3: input the angle from user in x and the number of terms in n.
Step4: x=x*3.14/180
Step5: print the value of cosx stored in sum
Step6: stop
-----------------------------------------DRAW FLOWCHART-----------------------------------------
Download