Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Assignment # 2

The document contains programming assignments involving writing code to: 1) Count characters in user input and print totals for each type. 2) Compare two numbers entered by the user and print the larger using a function. 3) Design a calculator to perform operations like square root, cube, multiplication, and division using switch statements. 4) Calculate roots of a quadratic equation using appropriate library functions. 5) Print different patterns of right angle triangles based on user input number. 6) Print sequences using for loops either ascending or descending based on user input.

Uploaded by

Blink Pink
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Assignment # 2

The document contains programming assignments involving writing code to: 1) Count characters in user input and print totals for each type. 2) Compare two numbers entered by the user and print the larger using a function. 3) Design a calculator to perform operations like square root, cube, multiplication, and division using switch statements. 4) Calculate roots of a quadratic equation using appropriate library functions. 5) Print different patterns of right angle triangles based on user input number. 6) Print sequences using for loops either ascending or descending based on user input.

Uploaded by

Blink Pink
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

18-ELE-10 Programming fundamentals

Assignment # 2
Programming fundamentals
 Write a program that counts blank spaces, uppercase, lowercase, digits,
new line from the data entered by the user. Use character ‘q’ to
terminate the program.
Input:
#include <stdio.h>
int main(){
int upper=0,lower=0,space=0,newline=0,digits=0;
char x;
printf("please enter the text:\n");
while ((x=getchar())!=q){
if (x==' '){
space+=1;
}
if (x=='\n'){
newline+=1;
}
if (x>='A'&x<='Z'){
upper+=1;
}
if (x>='a'&x<='z'){
lower+=1;
}
if (x>='0'&x<='9'){
digits+=1;
}
}
printf("\ntotal no. of enters are: %d",newline);
printf("\ntotal no. of spaces are: %d",space);
printf("\ntotal no. of uppercase letters are: %d",upper);
printf("\ntotal no. of lowercase letters are: %d",lower);
printf("\ntotal no. of digits are: %d",digits);

}
Output:
please enter the text:
the great point
pour
q

total no. of enters are: 2


total no. of spaces are: 2
total no. of uppercase letters are: 0

1
18-ELE-10 Programming fundamentals

total no. of lowercase letters are: 17


total no. of digits are: 0
--------------------------------
Process exited after 13.15 seconds with return value 27
Press any key to continue . . .
 Write a program that printout the larger number out of the two
numbers entered by the user. Use function to do the actual comparison
of the two numbers. Pass the two numbers to the function as arguments.
Input:
#include <stdio.h>
int max(int x,int y);
int main(){
int a,b,i=0;
while (i==0){
printf("Enter two numbers to be checked:\n");
scanf("%d%d",&a,&b);
printf("maximum of both is: %d\n",max(a,b));
}
}
int max(int x,int y){
if (x>y){
return x;
}
else {
return y;
}
}
Output:
Enter two numbers to be checked:
35
53
maximum of both is: 53
Enter two numbers to be checked:
67
89
maximum of both is: 89
Enter two numbers to be checked:
 Without using a function, Write a code to design a calculator that
performs four fundamental operations namely multiplication, division,
square root and cube using switch and related statements.
Input:
# include <stdio.h>
int main() {
char operator;
float num1,num2,x,Lc,ans,a=0;

2
18-ELE-10 Programming fundamentals

while (a==0){
printf("Enter operator either s or c or * or / :\n ");
operator=getchar();
switch(operator) {
case 's':
Lc=0.0001;
printf("enter operand:\n");
scanf("%f",&x);
for (ans=0;ans<x;ans=ans+Lc){
if((ans*ans)>x){
ans=ans-Lc;
break;
}
}
printf("sqrt(%f)=%.2f\n",x,ans);
break;
case 'c':
printf("enter operands:\n");
scanf("%f",&x);
printf("cube(%f)=%.0f\n",x,x*x*x);
break;
case '*':
printf("Enter two operands:\n");
scanf("%f%f",&num1,&num2);
printf("num1*num2=%.2f\n",num1*num2);
break;
case '/':
printf("Enter two operands:\n");
scanf("%f%f",&num1,&num2);
printf("num2/num1 = %.2f\n",num1/num2);
break;
default:
printf("Error! operator is not correct\n");
break;
}

}
}
Output:
Enter operator either s or c or * or / :
s
enter operand:
3
sqrt(3.000000)=1.73
Enter operator either s or c or * or / :
d
Error! operator is not correct
Enter operator either s or c or * or / :

3
18-ELE-10 Programming fundamentals

/
Enter two operands:
46
6
num2/num1 = 7.67
Enter operator either s or c or * or / :
*
Enter two operands:
45
3
num1*num2=135.00
Enter operator either s or c or * or / :
 Write a program that will calculate the roots of a quadratic equation.
Use corresponding library.
Input:
#include <stdio.h>
#include <math.h>
int main()
{
float a,b,c,disc,root1,root2,realpart,imaginarypart;
printf("enter the coefficients:\n");
scanf("%f%f%f",&a,&b,&c);
disc=(b*b)-(4*a*c);
printf("dicreminent =%.2f\n",disc);
if (disc>0){
printf("roots are real but different.");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("so\nroot1 = %.2f\nroot2 = %.2f",root1,root2);
}
else{
if (disc==0){
printf("roots are real and equal.");
root1=-b/(2*a);
root2=-b/(2*a);
printf("so\nroot1 = %.2f\nroot2 =
%.2f",root1,root2);
}
else{
if (disc<0){
printf("roots are complex and imaginary.");
realpart=-b/(2*a);
imaginarypart=(-sqrt(disc))/(2*a);
printf("so\nroot1 = %.2f+%.2fi\nroot2 = %.2f-
%.2fi",realpart,imaginarypart,realpart,imaginarypart);
}
}

4
18-ELE-10 Programming fundamentals

}
}
Output:
enter the coefficients:
45
67
9
dicreminent =2869.00
roots are real but different.so
root1 = -0.15
root2 = -1.34
--------------------------------
Process exited after 8.156 seconds with return value 30
Press any key to continue . . .
 Write program to print following patterns,
Input:
#include <stdio.h>
int main(){
int name,i=0;
while (i==0){
printf("enter the number of right angle triangle:");
scanf("%d",&name);
if (name==1){

printf("*\n**\n***\n****\n*****\n******\n*******\n********\n**
*******\n**********\n");
}
else{
if(name==2){

printf("**********\n*********\n********\n*******\n******\n****
*\n****\n***\n**\n*\n");
}
else{
if(name==3){
printf("**********\n *********\n
********\n *******\n ******\n *****\n ****\n
***\n **\n *\n");
}
else{
if(name==4){
printf(" *\n **\n
***\n ****\n *****\n ******\n *******\n ********\n
*********\n");
}
else{

5
18-ELE-10 Programming fundamentals

printf("the entered number is wrong


or out of range!\n");
}
}
}
}
}
}
Output:
enter the number of right angle triangle:1
*
**
***
****
*****
******
*******
********
*********
**********
enter the number of right angle triangle:2
**********
*********
********
*******
******
*****
****
***
**
*
enter the number of right angle triangle:4
*
**
***
****
*****
******
*******
********
*********
enter the number of right angle triangle:3
**********
*********
********
*******
******
*****

6
18-ELE-10 Programming fundamentals

****
***
**
*
enter the number of right angle triangle:3
**********
*********
********
*******
******
*****
****
***
**
*
enter the number of right angle triangle:
 Write for statements that print the following sequences of values using
loops, a) 1, 2, 3, 4, 5, 6, 7 b) 3, 8, 13, 18, 23 c) 20, 14, 8, 2, –4, –10 d) 19,
27, 35, 43, 51
Input:
#include <stdio.h>
int main(){
int i=0,x,y,z,a,count;
while(i==0){
printf("if a=1 then acend if a=0 then decend: ");
scanf("%d",&a);
if (a==1){
printf("enter the initialize value:");
scanf("%d",&x);
printf("give the conditional integer:");
scanf("%d",&y);
printf("enter the increment or decrement integer:");
scanf("%d",&z);
for (count=x;count<=y;count=count+z){
printf("%d\n",count);
}printf("this is the output sequence\n");
}
if (a==0){
printf("enter the initialize value:");
scanf("%d",&x);
printf("give the conditional integer:");
scanf("%d",&y);
printf("enter the increment or decrement integer:");
scanf("%d",&z);
for (count=x;count>=y;count=count+z){
printf("%d\n",count);
}printf("this is the output sequence\n");

7
18-ELE-10 Programming fundamentals

}
}
}
Output:
if a=1 then acend if a=0 then decend: 1
enter the initialize value:1
give the conditional integer:7
enter the increment or decrement integer:1
1
2
3
4
5
6
7
this is the output sequence
if a=1 then acend if a=0 then decend: 1
enter the initialize value:3
give the conditional integer:23
enter the increment or decrement integer:5
3
8
13
18
23
this is the output sequence
if a=1 then acend if a=0 then decend: 0
enter the initialize value:20
give the conditional integer:-10
enter the increment or decrement integer:-6
20
14
8
2
-4
-10
this is the output sequence
if a=1 then acend if a=0 then decend: 1
enter the initialize value:19
give the conditional integer:51
enter the increment or decrement integer:8
19
27
35
43
51
this is the output sequence
if a=1 then acend if a=0 then decend:

8
18-ELE-10 Programming fundamentals

 Develop a function that receives marks obtained by a student in three


subjects and return average and percentage of the marks called from
the main function. The final output should print the result of the
student.
Input:
#include <stdio.h>
float avg(int x,int y,int z){
float avg=(x+y+z)/3;
return avg;
}
float percent(int x,int y,int z){
float percent=((x+y+z)*100)/150;
return percent;
}
int main(){
float a,b,c;
printf("enter the marks of three subjects:\n");
scanf("%f%f%f",&a,&b,&c);
printf("average=
%.2f\npercntage=%.2f",avg(a,b,c),percent(a,b,c));
}
Output:
enter the marks of three subjects:
45
43
23
average= 37.00
percntage=74.00
--------------------------------
Process exited after 27.77 seconds with return value 30
Press any key to continue . . .
 Write a program using for loop to print multiplication tables from 1 to
5.
Input:
#include <stdio.h>
int main(){
int x=1,count;
while(x<6){
for (count=1;count<11;++count){
printf("%d*%d=%d\n",x,count,x*count);
}
++x;
printf("next table\n");
}
}

9
18-ELE-10 Programming fundamentals

Output:
1*1=1
1*2=2
1*3=3
1*4=4
1*5=5
1*6=6
1*7=7
1*8=8
1*9=9
1*10=10
next table
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
next table
3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
3*10=30
next table
4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
4*10=40
next table
5*1=5
5*2=10
5*3=15

10
18-ELE-10 Programming fundamentals

5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50
next table

--------------------------------
Process exited after 0.1744 seconds with return value 0
Press any key to continue . . .
 Write a program to find the equivalent resistance of the following
circuit. The values are,
R1=R2=R3=1OHM
R4=R5=R6=2 OHM
Input:
#include <stdio.h>
int main()
{
float
r1=1,r2=1,r3=1,r4=2,r5=2,r6=2,formula=r1+r2+r3+((1/r4)+(1/r5)+(1/
r6));
printf("the resistance in a given circuit is: %.2f",formula);

}
Output:
the resistance in a given circuit is: 4.50
--------------------------------
Process exited after 0.1521 seconds with return value 42
Press any key to continue . . .
 Write a program to calculate the sum of following series up to 7th term,
1/1! + 2/2! +3/3!+….. where !=factorial
Input:
#include<stdio.h>
int fact(int n){
if(n==1)
return 1;
else
return(n*fact(n-1));
}
int main(){
float x,f,sum=0;
for (x=1;x<=7;++x)
{

11
18-ELE-10 Programming fundamentals

sum+=(x/(f=fact(x)));
}printf("sum of the series is %.3f",sum);
}
Output:
sum of the series is 2.718
--------------------------------
Process exited after 0.1575 seconds with return value 26
Press any key to continue . . .

12

You might also like