0% found this document useful (0 votes)
36 views7 pages

QB (Unit 3) @comp2018 Soln

The document contains 10 code snippets demonstrating C programming concepts like: 1) Calculating simple and compound interest over years using a for loop. 2) Implementing a switch case to perform basic math operations based on user input. 3) Solving quadratic equations and checking for real/complex roots. 4) Converting characters between upper and lower case in a string. 5) Checking if a year is a leap year. 6) Reversing digits of a number using recursion. 7) Calculating Fibonacci numbers using recursion and iteration. 8) Computing sum of numbers and factorials using recursion. 9) Printing patterns using nested for loops. 10) Computing Taylor series approximation of

Uploaded by

shivam Gautam
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)
36 views7 pages

QB (Unit 3) @comp2018 Soln

The document contains 10 code snippets demonstrating C programming concepts like: 1) Calculating simple and compound interest over years using a for loop. 2) Implementing a switch case to perform basic math operations based on user input. 3) Solving quadratic equations and checking for real/complex roots. 4) Converting characters between upper and lower case in a string. 5) Checking if a year is a leap year. 6) Reversing digits of a number using recursion. 7) Calculating Fibonacci numbers using recursion and iteration. 8) Computing sum of numbers and factorials using recursion. 9) Printing patterns using nested for loops. 10) Computing Taylor series approximation of

Uploaded by

shivam Gautam
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/ 7

1

Unit 3_1
#include <stdio.h>
void main()
{
float p,a=0,r;
int n,i;
printf("Enter no. of P: ");
scanf("%f",&p);
printf("\nEnter no. of years: ");
scanf("%d",&n);
printf("\nEnter no. of rate of interest per year:");
scanf("%f",&r);
for(i=1;i<=n;i++)
{
a+=p;
a=a+a*r*.01;
}
printf("\nTotal amount accumulated: %.2f",a);
}

Unit 3_2
#include <stdio.h>
void main()
{
char o;
float a,b,c;
printf("\Addition-'+' Subtraction-'-'\nMultiplication-'*' Division-'/'");
printf("\nEnter character for the corresponding operation: ");
scanf("%c",&o);
printf("\nEnter 2 nos.: ");
scanf("%f%f",&a,&b);
switch(o)
{
case '+':c=a+b;
break;
case '-':c=a-b;
break;
case '*':c=a*b;
break;
case '/':c=a/b;
break;

Compiled by-civil-17
For any queries, feel free to contact us on [email protected]
2

default:printf("INVALID CHOICE!!");
}
printf("\nAnswer-%.2f",c);
}}

Unit 3_3
#include <stdio.h>
void main()
{
float a,b,c,d=0,d1=0,r1=0,r2=0;
printf("Enter a,b,c\n");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d>=0)
{
d1=sqrt(d);
r1=(-b+d1)/2.0;
r2=(-b-d1)/2.0;
printf("\nRoots are \n%.2f\n %.2f",r1,r2);
}
else
{
d1=sqrt(-d);
printf("\nRoots are\n %.2f+%.2fi\n %.2f-%.2fi",(-b/2.0),(d1/2.0),(-b/2.0),(d1/2.0));
}
}

Unit 3_4
#include <stdio.h>
void main()
{
char a[100];
int i;
printf("Enter text: ");
scanf("%s",a);
for(i=0;a[i]!=NULL;i++)
if(a[i]>=65&&a[i]<=90)
a[i]+=32;
else if(a[i]>=97&&a[i]<=122)
a[i]-=32;

Compiled by-civil-17
For any queries, feel free to contact us on [email protected]
3

printf("%s",a);
}

Unit 3_5
#include <stdio.h>
void main()
{
int a;
printf("Enter year: ");
scanf("%d",a);
if((a%4==0)&&(a%100!=0)||(a%400==0))
printf("Leap Year");
else
printf("Not a Leap Year");

Unit 3_6
#include <stdio.h>
void main()
{
int n,n1,d,r=0;
printf("Enter no.-");
scanf("%d",&n);
n1=n;
do
{
d=n1%10;
r=r*10+d;
n1=n1/10;
}while(n1);
printf("\nReversed no.-%d",r);
}

Unit 3_7
#include <stdio.h>
int rec_fib(int n);
int it_fib(int n);
void main()
{
int i,n,c;

Compiled by-civil-17
For any queries, feel free to contact us on [email protected]
4

printf("Enter n-");
scanf("%d",&n);
printf("\niterative-1 recursive-2\n ");
scanf("%d",&c);
if(c==2)
for(i=0;i<n;i++)
printf("%d,",rec_fib(i));
else if(c==1)
it_fib(n);
}
int rec_fib(int n)
{
if(n==0||n==1)
return n;
return (rec_fib(n-1)+rec_fib(n-2));
}
int it_fib(int n)
{
int i,a=0,b=1,c=0;
if(n==1)
printf("0");
else
printf("0,1,");
for(i=2;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("%d,",c);
}
}

Unit 3_8
#include <stdio.h>
int fact(int n);
int sum(int s,int n);
void main()
{
int c,n;

Compiled by-civil-17
For any queries, feel free to contact us on [email protected]
5

printf("Enter choice: ");


printf("\n1-sum of nos., 2-factorial:\n ");
scanf("%d",&c);
printf("\nEnter no.");
scanf("%d",&n);
if(c==1)
printf("sum=%d",sum(0,n));
else
if(c==2)
printf("factorial=%d",fact(n));
}
int sum(int s,int n)
{s+=n;
if(n==1)
return s;
return sum(s,n-1);
}
int fact(int n)
{
if(n==0||n==1)
return n;
return n*fact(n-1);
}

Unit 3_9a
#include <stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=i;j<=5;j++)
{

printf("* ");
}
printf("\n");

}
}

Compiled by-civil-17
For any queries, feel free to contact us on [email protected]
6

Unit 3_9b
#include <stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{

printf("* ");
}
printf("\n");

}
}

Unit 3_9c
#include <stdio.h>
void main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=i;j<=5;j++)
printf(" ");
for(k=1;k<=i;k++)
printf("* ");
printf("\n");
}
}

Unit 3_9d
#include <stdio.h>
void main()
{
int i,j,k;
for(i=1;i<=5;i++)

Compiled by-civil-17
For any queries, feel free to contact us on [email protected]
7

{
for(j=i;j<=5;j++)
printf(" ");
for(k=1;k<=i;k++)
printf("* ");
printf("\n");
}
}

Unit 3_10
#include <stdio.h>
int fact(int n);
int fact(int n)
{
if(n==1)
return 1;
return n*fact(n-1);
}
void main()
{
float s=0,x,t;
int n,i,c;
printf("Enter x: ");
scanf("%f",&x);
printf("Enter n: ");
scanf("%f",&n);
for(i=1;c<n;i+=2)
{
t=pow(x,i)/fact(i);
if(t>100000)
break;
s+=pow(-1,c)*t;
++c;
}
printf("value=%f",s);
}

Compiled by-civil-17
For any queries, feel free to contact us on [email protected]

You might also like