Assignment # 2
Assignment # 2
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
1
18-ELE-10 Programming fundamentals
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
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
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