0% found this document useful (0 votes)
2 views

Comp

The document contains a series of programming exercises and their corresponding C code solutions, authored by Shaurya Singh. Each exercise covers different programming concepts such as calculating areas, salary computation, series generation, and pattern printing. The document also includes user-defined functions for tasks like checking Armstrong numbers and generating Fibonacci series.

Uploaded by

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

Comp

The document contains a series of programming exercises and their corresponding C code solutions, authored by Shaurya Singh. Each exercise covers different programming concepts such as calculating areas, salary computation, series generation, and pattern printing. The document also includes user-defined functions for tasks like checking Armstrong numbers and generating Fibonacci series.

Uploaded by

vedanshmanwal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

/*Name: Shaurya Singh

University Roll No. : 24021250


Section: A G2
Roll: 67
Q1.The length & breadth of a rectangle and radius of a circle are input through the keyboard.
Write a
program to calculate the area & perimeter of the rectangle, and the area & circumference of
the circle.*/

ANS:
#include <stdio.h>
int main()
{
float length, breadth, radius, areaRec, perimeterRec, areaCircle, Circumference;
printf("Enter the Length of the Rectangle: ");
scanf("%f", &length);
printf("Enter the Breadth of the Rectangle: ");
scanf("%f", &breadth);
printf("Enter the Radius of the Circle: ");
scanf("%f", &radius);
areaRec= lengthbreadth;
perimeterRec=2lengthbreadth;
areaCircle=23.14radius;
Circumference=3.14radius*radius;
printf("/n The Area of the rectange is: %.2f unit square",areaRec);
printf("/n The Perimeter of the rectange is: %.2f unit square",perimeterRec);
printf("/n The Area of the Circle is: %.2f unit square",areaCircle);
printf("/n The Circumference of the Circle is: %.2f unit square",Circumference);
return 0;
}
/*Name: Shaurya Singh
University Roll No. : 24021250
Section: A G2
Roll: 67
Q2. Mr. X’s basic salary is input through the keyboard. If the basic salary exceeds 50000, his
dearness
allowance is 30% of the basic salary, and the house rent allowance is 15% of basic salary
otherwise
his dearness allowance is 20% of the basic salary, and the house rent allowance is 10% of
basic salary.
Write a program to calculate his gross salary.*/

ANS:
#include <stdio.h>
int main()
{
float salary, grossSalary;
printf("\t\t******INPUT******");
printf("\n Enter the Basic Salary of Mr. X: ");
scanf("%f", &salary);
if(salary>50000)
{
grossSalary= salary +(0.3salary)+(.15salary);
}
else
{
grossSalary= salary +(0.2salary)+(.10salary);
}
printf("\t\t******OUTPUT******");
printf("\nThe Gross Salary of Mr. X is: %.2f", grossSalary);
return 0;
}
/*Name: Shaurya Singh
University Roll No. : 24021250
Section: A G2
Roll: 67
Q3. The following calculates value of f(x) if x has different ranges of value as below:
F(x) =x2+2 if 0<=x<=10
F(x) =x2+2x if 11<=x<=20
F(x) =x3+2x2 if 21<=x<=30
F(x) =0 if x>30*/

ANS:
#include <stdio.h>
int main()
{
int x,fx;
printf("\t\t******INPUT*******");
printf("\n Enter the the value of 'x' : ");
scanf("%f", &x);
if(x>=0 && x<=10){
fx=(xx)+2;
}
else if(x>=11 && x<=20){
fx=(xx)+(2x);
}
else if(x>=21 && x<=30){
fx=(xxx)+(2(xx));
}
else if(x>30){
fx= 0;
}
else {
printf("x is less than 0 and is an invalid input");
}
printf("\t\t******OUTPUT******");
printf("\n The vlaue of F(%d) is %.2f", x, fx);
return 0;
}*
/*Name: Shaurya Singh
University Roll No. : 24021250
Section: A G2
Roll: 67

Q4.Write a program to input a three-digit number and print it in words using switch case.
Sample input 256
Output: Two Five Six*/

#include <stdio.h>
int main()
{
int n, i, ch,rev=0;
printf("\t\t**INPUT**");
printf("Enter a Three Digit Number: ");
scanf("%d",&n);
int temp=n;
while(temp!=0)
{ int a=temp%10;
rev=rev*10 +a;
temp=temp/10;
}
printf("\t\t******OUTPUT******\n");
while(rev!=0)
{
int d=rev%10;
rev=rev/10;
switch (d)
{
case 0: printf("Zero ");
break;
case 1: printf("One ");
break;
case 2: printf("Two ");
break;
case 3: printf("Three ");
break;
case 4: printf("Four ");
break;
case 5: printf("Five ");
break;
case 6: printf("Six ");
break;
case 7: printf("Seven ");
break;
case 8: printf("Eight ");
break;
case 9: printf("Nine ");
break;
}
}

return 0;
}
/*Name: Shaurya Singh
University Roll No. : 24021250
Section: A G2
Roll: 67

Q5.Consider the below series:


0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8
This series is a mixture of 2 series all the odd terms in this series form even numbers in
ascending order and every even terms is derived from the previous term using the formula
(x/2). Write a program to find the nth term in this series.*/

#include<stdio.h>
int main()
{
int n;
printf("******INPUT******\n");
printf("Enter the value of n:");
scanf("%d",&n);
printf("******OUTPUT******\n");
printf("The %dth term is:",n);
if(n%2==1)
{
printf("%d ",n-1);
}
else
{
printf("%d",(n-1)/2);
}

return 0;
}
/*Name: Shaurya Singh
University Roll No. : 24021250
Section: A G2
Roll: 67

Q6. Write a program to print the sum of given series: 1!+2!-3!+4!-5!..................n!*/

#include<stdio.h>
int main()
{
int n,i,sum=1;
printf("******INPUT******\n");
printf("enter the nth term");
scanf("%d",&n);
printf("******OUTPUT******\n");
for(i=2;i<=n;i++)
{
int fact=1;
for(int j=i;j>0;j--)
{
fact=fact*j;

}
if( i%2==0)
{
sum=sum+fact;

}
else
sum=sum-fact;
}

printf("the sum of series with %d terms is %d",n,sum);


return 0;
}
/*Name: Shaurya Singh
University Roll No. : 24021250
Section: A G2
Roll: 67

Q7.Write a program to print all the prime numbers between a given range p and q.*/

#include <stdio.h>
int main()
{
int p, q, i,c=0;
printf("\t\t******INPUT******\n");
printf("Enter the vlaue of p as lower bound: ");
scanf("%d",&p);
printf("\nEnter the vlaue of q as upper bound: ");
scanf("%d",&q);
printf("\t\t******OUTPUT******\n");
for(int i=(p+1);i<q;i++)
{
c=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
c=c+1;
}
}
if(c==2)
{
printf("%d ",i);
}
}
return 0;
}
/*Name: Shaurya Singh
University Roll No. : 24021250
Section: A G2
Roll: 67

8.
Write a user defined function to create a game where a player will get a chance to input three
numbers from keyboard with his eyes closed. Check if the sum of the input numbers is odd or
even. If sum is even then ask him to play again and for every chance give one point. If he can
score three points
(continuous three times got even sum)
, declare him winner and print “you won” otherwise print “you lost.” Print the result in
main.*/

#include <stdio.h>
int game();
int main()
{
int points=0,totalSum=0;
printf("\t\t******INPUT******\n");
for(int i=1;i<=3;i++)
{
int roundP=game();
if(roundP%2!=0)
{
printf("\nYou Lost\n");
i=4;
}
else
{
totalSum=totalSum+roundP;
if(roundP%2==0)
{
points=points+1;
}
}

}
if(points==3)
{
printf("\nYour Total Sum is: %d", totalSum);
printf("\nYou Won");
}
else
{
printf("\nYour Total Sum is: %d", totalSum);
printf("\nYou Lost");
}
return 0;
}
int game()
{
int n1,n2,n3;
printf("Enter three number with eyes closed:");
scanf("%d",&n1);
scanf("%d",&n2);
scanf("%d",&n3);
printf("Sum of this round is: %d \n", (n1+n2+n3));
return n1+n2+n3;
}
/*Name: Shaurya Singh
University Roll No. : 24021250
Section: A G2
Roll: 67

9.Write a user defined function to check a number is Armstrong or not.*/

#include <stdio.h>
#include <math.h>
int main()
{
int temp,temp2,c=0,d,sum=0,n;
printf("\t\t******INPUT******\n");
printf("Enter the Number: ");
scanf("%d",&n);
temp=n;
temp2=n;
while(n!=0)
{
c=c+1;
n=n/10;
}
while(temp!=0)
{
d=temp%10;
sum=sum+pow(d,c);
temp=temp/10;
}
printf("\t\t******OUTPUT******\n");
if(sum==temp2)
printf("It is an Armstrong number");
else
printf("It is not an Armstrong number");
}
/*Name: Shaurya Singh
University Roll No. : 24021250
Section: A G2
Roll: 67

10.
Write a program to print the given patterns-:
A
BC
DEF
GHIJ
K L M N O */

#include<stdio.h>

int main()
{
int i,j,k=1,n;
printf("\t\t******INPUT******\n");
printf("Enter the no of lines\n");
scanf("%d",&n);
printf("\t\t******OUTPUT******\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++,k++)
{
printf("%4c",(char)(k+64)); }
printf("\n");
}
return 0;
}

/*Name: Shaurya Singh


University Roll No. : 24021250
Section: A G2
Roll: 67

11.
Write a program to print the given patterns-:

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

#include <stdio.h>
int main()
{
int i, space, rows, k = 0;
printf("\t\t******INPUT******\n");
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("\t\t******OUTPUT******\n");
for (i = 1; i <= rows; ++i, k = 0)
{
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}

/*Name: Shaurya Singh


University Roll No. : 24021250
Section: A G2
Roll: 67

12.
Write a user defined function to check a number is Armstrong or not using function.*/

#include <stdio.h>
#include <math.h>
int Armstrong(int);
int main()
{
int n;
printf("\t\t******INPUT******\n");
printf("Enter the Number: ");
scanf("%d",&n);
int Sum=Armstrong(n);
printf("\t\t******OUTPUT******\n");
if(Sum==n)
{
printf("The number %d is an Armstrong Number.", n);
}
else
{
printf("The number %d is NOT an Armstrong Number.", n);
}
return 0;
}
int Armstrong(int n)
{
int temp,temp2,c=0,d,sum=0;
temp=n;
temp2=n;
while(n!=0)
{
c=c+1;
n=n/10;
}
while(temp!=0)
{
d=temp%10;
sum=sum+pow(d,c);
temp=temp/10;
}
return sum;
}

/*Name: Shaurya Singh


University Roll No. : 24021250
Section: A G2
Roll: 67

13.
Write a program to find reverse of a number using recursion.*/

#include<stdio.h>

int reverse(int , int );

int main()
{
int number, result;
printf("******INPUT******");
printf("Enter number: ");
scanf("%d", &number);
printf("******OUTPUT******");
result = reverse(number, 0);

printf("Reverse of %d is %d.", number, result);


return 0;
}

int reverse(int num, int rev)


{
if(num==0)
return rev;
else
return reverse (num/10, rev*10 + num%10);

/*Name: Shaurya Singh


University Roll No. : 24021250
Section: A G2
Roll: 67

14. Write a program to generate Fibonacci series using recursion.


*/

#include <stdio.h>
int fibo(int);
int main()
{
int i,n;
printf("\t\t******INPUT******\n");
printf("Enter the value of n as number of terms in the series: ");
scanf("%d",&n);
printf("\t\t*******OUTPUT*******\n");
for(i=1;i<=n;i++)
{
printf(" %d ", fibo(i));
}
return 0;
}
int fibo(int n)
{
if(n==0)
{
return 0;
}
else if(n==1)
{
return 1;
}
else
{
return fibo(n-1)+fibo(n-2);
}
}

/*Name: Shaurya Singh


University Roll No. : 24021250
Section: A G2
Roll: 67

15.Write a menu driven program to perform the following operations on a given


number(integer) based upon the given choices from (1 to 4).
1. Print factorial of the number
2. Print the number is odd or even
3. Print reverse of the number
4. Exit
*/

#include <stdio.h>
#include <math.h>
int main()
{
int ch,n;
printf("\t\t******INPUT******\n");
printf("Enter the Number: ");
scanf("%d",&n);
printf("******MENU******\n");
printf("1. Print factorial of the number\n");
printf("2. Print the number is odd or even\n");
printf("3, Print reverse of the number\n");
printf("4. Exit\n");
printf("Enter the you choice: ");
scanf("%d",&ch);
switch(ch)
{ case 1:
int f=1,i;
for(i=1;i<=n;i++)
f=f*i;
printf("\t\t******OUTPUT******\n");
printf("The factorial of the number is %d",f);
break;
case 2:
printf("\t\t******OUTPUT******\n");
if(n%2==0)
printf("The number is even\n");
else
printf("The number is odd\n");
break;
case 3:
int r=0, temp=n;
while(temp!=0)
{
int d=temp%10;
r=r*10+d;
temp=temp/10;
}
printf("\t\t******OUTPUT******\n");
printf("The reverse of the number is %d",r);
break;
case 4:
return(0);
}
}

/*Name: Shaurya Singh


University Roll No. : 24021250
Section: A G2
Roll: 67

16.Develop a program to store age of n students in a class using array. Calculate the average
age and find the count of all the students above and below the average age. Print the higher
count with an appropriate message.
*/

#include <stdio.h>
#include <math.h>
int main()
{
int i,n,avg=0,sum=0,c1=0,c2=0;
printf("\t\t******INPUT******\n");
printf("Enter the Number of students: ");
scanf("%d",&n);
int st[n];
printf("Enter the age of the students:\n");
for(i=0;i<n;i++)
{
scanf("%d",&st[i]);

}
for(i=0;i<n;i++)
{
sum=sum+st[i];
}

avg=sum/n;
for(i=0;i<n;i++)
if(st[i]<=avg)
c1++;
else
c2++;
printf("\t\t******OUTPUT******\n");
printf("The average age of the students is %d\n",avg);
printf("Number of the students below or equal to the average age %d\n",c1);
printf("Number of the students above the average age %d\n",c2);
if(c1>c2)
printf("Number of the students below or equal to the average ageis higher %d\n",c1);
else
printf("Number of the students above the average is higher age %d\n",c2);
}

/*Name: Shaurya Singh


University Roll No. : 24021250
Section: A G2
Roll: 67

17. Write two user defined functions to implement the given operations in an array:
1.insert an element at a particular position of an array.
2.delete an element from a given position from the array.
Display the final array..
*/

#include <stdio.h>
#include <math.h>
void insert_ele(int a[20],int x,int p, int l)
{
int i;
for(i=l;i>=p;i--)
a[i]=a[i-1];
a[p-1]=x;
printf("\t\t******OUTPUT******\n");
printf("The array after insertion is \n");
for(i=0;i<=l;i++)
printf("%d\n",a[i]);
}

void delete_ele(int a[20], int p, int l)


{
int i;
for(i=p-1;i<l-1;i++)
a[i]=a[i+1];
("\t\t******OUTPUT******\n");
printf("The array after deletion is \n");
for(i=0;i<l-1;i++)
printf("%d\n",a[i]);
}

int main()
{
int ch,n,a[20],x,p,i;
printf("\t\t******INPUT******\n");
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the elements \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("******MENU******\n");
printf("1. Insertion of an element\n");
printf("2. Deletion of an element\n");
printf("3. Exit\n");
printf("Enter the you choice: ");
scanf("%d",&ch);
switch(ch)
{ case 1:
printf("Enter the elemrnt to be inserted: ");
scanf("%d",&x);
printf("Enter the position at which the element is to be inserted ");
scanf("%d",&p);
insert_ele(a,x,p,n);
break;
case 2:
printf("Enter the position of the element is to be deleted ");
scanf("%d",&p);
delete_ele(a,p,n);
break;
case 3:
return 0;
}
return 0;

/*Name: Shaurya Singh


University Roll No. : 24021250
Section: A G2
Roll: 67

18.
Write a function to sort an array of integers using any sorting technique.
*/

#include <stdio.h>
#include <math.h>

int main()
{
int n,a[20],j,i;
printf("\t\t******INPUT******\n");
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the elements \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
printf("The sorted array is \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
return 0;
}

You might also like