[go: up one dir, main page]

0% found this document useful (0 votes)
21 views71 pages

Pps Labmanual

The document contains a series of C programming exercises, each with a problem statement, logic explanation, and corresponding code solution. Topics covered include basic arithmetic operations, area calculations, interest calculations, number interchange, unit conversions, temperature conversions, distance calculations, number classification, grading systems, salary slips, day of the week identification, maximum/minimum value finding, factorial calculation, number reversal, Fibonacci series generation, and digit summation. Each program is designed to demonstrate fundamental programming concepts and control structures in C.
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)
21 views71 pages

Pps Labmanual

The document contains a series of C programming exercises, each with a problem statement, logic explanation, and corresponding code solution. Topics covered include basic arithmetic operations, area calculations, interest calculations, number interchange, unit conversions, temperature conversions, distance calculations, number classification, grading systems, salary slips, day of the week identification, maximum/minimum value finding, factorial calculation, number reversal, Fibonacci series generation, and digit summation. Each program is designed to demonstrate fundamental programming concepts and control structures in C.
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/ 71

PROGRAMME - 1

Write a program to that performs as calculator ( addition, multiplication, division,


subtraction).

Solution

#include <stdio.h>
int main()
{
int number1, number2, addition, subtraction, multiply;
float divide;
printf("Enter Number 1\n");
scanf("%d", &number1);
printf("Enter Number 2\n");
scanf("%d", &number2);
addition = number1 + number2 ;
subtraction = number1 - number2;
multiply = number1 * number2;
divide = number1 / (float)number2; //typecasting
printf("Addition = %d\n",addition);
printf("Subtraction = %d\n",subtraction);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);
return 0;
}

Output
PROGRAMME - 2

Write a program to find area of triangle(a=h*b*.5)


a = area,h = height,b = base

Logic to find area of a triangle


Below is the step by step descriptive logic to find area of a triangle.

1. Input base of the triangle. Store in some variable say b(base).


2. Input height of the triangle. Store in some variable say h(height).
3. Use triangle area formula to calculate area i.e. a = b * h * 05
4. Print the resultant value of area.

Solution

#include<stdio.h>
int main()
{
float a,h,b;
printf("\n Enter height :");
scanf("%f",&h);
printf("\n Enter base :");
scanf("%f",&b);
a=b*h*0.5;
printf("\n The Area of triangle = %.2f",a);
return 0;
}

Output
PROGRAMME - 3
Write a program to calculate simple interest (i = (p*r*n)/100 )
i = Simple interest
p = Principal amount
r = Rate of interest
n = Number of years
Logic to calculate simple interest
Step by step descriptive logic to calculate simple interest.
1. Input principle amount in some variable say p(principle)
2. Input years in some variable say n(years).
3. Input rate in some variable say r(rate).
4. Find simple interest using formula I =( p * r * n) / 100 ).
5. Finally, print the resultant value of I(intrest).

Solution
#include <stdio.h>
int main()
{
int n;
float p, r, I;
printf("\n Enter Amount :");
scanf("%f",&p);
printf("\n Enter No of Years :");
scanf("%d", &n);
printf("\n Enter Rate :");
scanf("%f", &r);
I = (p*r*n)/100;
printf("\n Interest = %.2f",I);
return 0;
}
Output
PROGRAMME - 4
Write a C program to interchange two numbers
Solution
#include <stdio.h>
int main()
{
int a,b;
printf("Enter Value of a :");
scanf("%d",&a);
printf("Enter Value of B :");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter Interchange Values A = %d B = %d",a,b);
return 0;
}
Output
PROGRAMME - 5
Write a C program to enter a distance in to kilometre and convert it in to meter,
feet, inches and centimetre
Solution
#include <stdio.h>
int main()
{
float km;
printf("Enter Length in Kilometer : ");
scanf("%f",&km);
printf("\n %.2f KM = %.2f Meters",km,km*1000);
printf("\n %.2f KM = %.2f Feets",km,km*3280.84);
printf("\n %.2f KM = %.2f Inches",km,km*39370.08);
printf("\n %.2f KM = %.2f Centimeters",km,km*1000*100);
return 0;
}
Output
PROGRAMME - 6
Write a program to compute Fahrenheit from centigrade (f=1.8*c +32)
Solution

#include <stdio.h>

int main()
{
float F,C;
printf("Enter Temperature in Celsius : " );
scanf("%f",&C);
F = (C * 1.8) + 32;
printf("\n %.2f Celsius = %.2f Fahrenheit",C, F);
return 0;
}
PROGRAMME - 7

Write a C program to find out distance travelled by the equation d = ut + at^2


Solution

#include<stdio.h>

int main()
{
float u,a,d;
int t;

printf("\nEnter the value of a : ");


scanf("%f",&a);

printf("\nEnter the value of u : ");


scanf("%f",&u);

printf("\nEnter the value of t : ");


scanf("%d",&t);

d = (u * t) + (a * t * t)/2;

printf("\n The Distance : %.2f",d);

return 0;
}
PROGRAMME - 8

Write a C program to find that the accepted number is Negative, or Positive or


Zero
Logic to check positive, negative or zero
Logic of this program is simple if you know basic maths. Let us have a quick look about
number properties.

 A number is said negative if it is less than 0 i.e. num < 0.


 A number is said positive if it is greater than 0 i.e. num > 0.

We will use the above logic inside if to check number for negative, positive or zero. Step
by step descriptive logic to check negative, positive or zero.

 Input a number from user in some variable say num.


 Check if(num < 0), then number is negative.
 Check if(num > 0), then number is positive.
 Check if(num == 0), then number is zero.

#include<stdio.h>

int main()
{
int no;
printf("\n Enter any number : ");
scanf("%d",&no);
if(no==0)
{
printf("\n Entered Number is Zero");
}
else if(no>0)
{
printf("\n Entered Number is Positive");
}
else
{
printf("\n Entered Number is Negative");
}
return 0;
}
PROGRAMME - 9

Write a program to read marks of a student from keyboard whether the student is
pass or fail(using if else)
#include<stdio.h>

int main()
{
int marks;
printf("\n Enter Marks from 0-70 :");
scanf("%d",&marks);
if(marks<23)
{
printf("\n Sorry ..! You are Fail");
}
else
{
printf("\nCongratulation ...! You are Pass");
}
return 0;
}
PROGRAMME - 10

Write a program to read three numbers from keyboard and find out maximum
out of these three. (nested if else)
Logic to find maximum between three numbers
Step by step descriptive logic to find maximum between three numbers.

 Input three numbers from user. Store it in some variable say num1, num2 and
num3.
 Compare first two numbers i.e. num1 > num2. If the statement is true then num2
is surely not max value. Perform one more comparison between num1 with
num3 i.e. if(num1 > num3), then num1 is max otherwise num3.
 If the statement num1 > num2 is false. Which indicates that num1 is not max.
Hence, this time compare num2 with num3. If the statement num2 > num3 is
true then num2 is max otherwise num3.
#include<stdio.h>
int main()
{
int a,b,c;
printf("\n Enter First Number :");
scanf("%d",&a);
printf("\n Enter Second Number :");
scanf("%d",&b);
printf("\n Enter Third Number :");
scanf("%d",&c);
if(a>b)
{
if(a>c)
{
printf("\n First Number %d is maximum",a);
}
else
{
printf("\n Third Number %d is maximum",c);
}
}
else
{
if(b>c)
{
printf("\n Second Number %d is maximum",b);
}
else
{
printf("\n Third Number %d is maximum",c);
}
}
return 0;
}
PROGRAMME - 11

Write a C program to check whether the entered character is capital, small letter,
digit or any special character.
Logic to check alphabet, digit or special character
 A character is alphabet if it in between a-z or A-Z.
 A character is digit if it is in between 0-9.
 A character is special symbol character if it neither alphabet nor digit.

Step by step descriptive logic to check alphabet, digit or special character.


 Input a character from user. Store it in some variable say ch.
 First check if character is alphabet or not. A character is alphabet if((ch >= 'a' &&
ch <= 'z') || (ch >= 'A' && ch <= 'Z')).
 Next, check condition for digits. A character is digit if(ch >= '0' && ch <= '9').
 Finally, if a character is neither alphabet nor digit, then character is a special
character.
 Program to check alphabet, digit or special character
#include<stdio.h>
int main()
{
char ch;
printf("\nEnter Any Character :");
scanf("%c",&ch);
if(ch>='0' && ch<='9')
{
printf("\n Entered Character is Digit");
}
else if(ch>='A' && ch<='Z')
{
printf("\n Entered Character is Capital Letter");
}
else if(ch>='a' && ch<='z')
{
printf("\n Entered Character is Small Letter");
}
else
{
printf("\n Entered Character is Special Character");
}
return 0;
}
PROGRAMME - 12

Write a program to read marks from keyboard and your program should display
equivalent grade according to following table(if else ladder)
Marks Grade
100 – 80 Distinction
79 – 60 First Class
59 – 40 Second Class
< 40 Fail

#include<stdio.h>
int main()
{
int marks;
printf("\n Enter Marks between 0-100 :");
scanf("%d",&marks);
if(marks>100 || marks <0)
{
printf("\n Your Input is out of Range");
}
else if(marks>=80)
{
printf("\n You got Distinction");
}
else if(marks>=60)
{
printf("\n You got First Class");
}
else if(marks>=40)
{
printf("\n You got Second Class");
}
else
{
printf("\n You got Fail");
}
return 0;
}
PROGRAMME - 13

Write a c program to prepare pay slip using following data.


Da = 10% of basic, Hra = 7.50% of basic, Ma = 300,
Pf = 12.50% of basic, Gross = basic + Da + Hra + Ma, Nt = Gross – Pf.
#include<stdio.h>

int main()
{
float basic;
printf("\n Enter Basic Salary :");
scanf("%f",&basic);
printf("\n===================================");
printf("\n SALARY SLIP");
printf("\n===================================");
printf("\n Basic : %.2f",basic);
printf("\n DA : %.2f",basic*0.10);
printf("\n HRA : %.2f",basic*0.075);
printf("\n MA : %.2f",300.00);
printf("\n===================================");
printf("\n GROSS : %.2f",basic+(basic*0.10)+(basic*0.075)+300.00);
printf("\n===================================");
printf("\n PF : %.2f",basic*0.125);
printf("\n===================================");
printf("\n NET : %.2f",(basic+(basic*0.10)+(basic*0.075)+300.00)-(basic*0.125));
printf("\n===================================");
return 0;
}
PROGRAMME - 14

Write a C program to read no 1 to 7 and print relatively day Sunday to Saturday.


Logic to print day of week name using
Step by step descriptive logic to print day name of week.
 Input day number from user. Store it in some variable say no.
 Switch the value of week i.e. use switch(no) and match with cases.
 There can be 7 possible values(choices) of week i.e. 1 to 7. Therefore write 7 case
inside switch. In addition, add default case as an else block.
 For case 1: print “MONDAY”, for case 2: print “TUESDAY” and so on. Print
“SUNDAY” for case 7:.
 If any case does not matches then, for default: case print “Invalid week number”
#include<stdio.h>

int main()
{
int no;
printf("\n Enter Day no between 1-7 : ");
scanf("%d",&no);
switch(no)
{
case 1:
printf("\n Sunday");
break;
case 2:
printf("\n Monday");
break;
case 3:
printf("\n Tuesday");
break;
case 4:
printf("\n Wednesday");
break;
case 5:
printf("\n Thursday");
break;
case 6:
printf("\n Friday");
break;
case 7:
printf("\n Saturday");
break;
default:
printf("\n Please Enter Proper Input");
break;
}
return 0;
}
PROGRAMME - 15

Write a C program to find out the Maximum and Minimum number from given 10
numbers
#include <stdio.h>

int main()
{
int a[10],i,min,max;
for(i=0;i<10;i++)
{
printf("\n Enter Interger Value [%d] : ",i+1);
scanf("%d",&a[i]);
if(i==0)
{
min=max=a[i];
}
else
{
if(min>a[i])
{
min=a[i];
}
if(max<a[i])
{
max=a[i];
}
}
}
printf("\n Minimum : %d",min);
printf("\n Maximum : %d",max);
return 0;
}
PROGRAMME - 16

Write a C program to input an integer number and check the last digit of number
is even or odd.
#include <stdio.h>

int main()
{
int i;
printf("\n Enter any Number : ");
scanf("%d",&i);
if((i%10)%2==0)
{
printf("\n Last Digit of Number is Even");
}
else
{
printf("\n Last Digit of Number is Odd");
}
return 0;
}
PROGRAMME - 17

Write a C program to find factorial of a given number.


#include <stdio.h>
int main()
{
int no, fact=1;
printf("\n Enter No to find its Factorial : ");
scanf("%d",&no);

while(no>1)
{
fact=fact*no;
no=no-1;
}

printf("\n Factorial of entered no is : %d",fact);


return 0;
}
PROGRAMME - 18

Write a program to reverse a number.


#include <stdio.h>
int main()
{
int no,rev=0;
printf("\n Enter No to make it Reverse : ");
scanf("%d",&no);
while(no>0)
{
rev=(rev*10)+(no%10);
no=no/10;
}
printf("\n Reverse of entered no is : %d",rev);
return 0;
}
PROGRAMME - 19

Write a program to generate first n number of Fibonacci series


Solution
#include <stdio.h>

int main()
{
int no=10,i=0,j=1;
printf(" %d %d",i,j);
while(no>0)
{
printf(" %d",i+j);
j=i+j;
i=j-i;
no--;
}
return 0;
}
PROGRAMME - 20

Write a program to find out sum of first and last digit of a given number
#include <stdio.h>

int main()
{
int no,sum=0;
printf("\n Enter Any Number :");
scanf("%d",&no);
if(no<10)
{
sum = sum + (no*2);
}
else
{
sum = sum + (no%10);
while(no>9)
{
no = no /10;
}
sum = sum + no;
}
printf("\n Sum of First & Last Digit is : %d",sum);
return 0;
}
PROGRAMME - 21

Write a C program to find the sum and average of different numbers which are
accepted by user as many as user wants.
#include <stdio.h>
int main()
{
int no,sum=0,i=0,val;
printf("\n How Many Number You Want to Enter : ");
scanf("%d",&no);
while(i<no)
{
printf("Enter No [%d]:",i+1);
scanf("%d",&val);
sum=sum+val;
i++;
}
printf("\n Sum = %d",sum);
printf("\n Sum = %.2f",((float)sum)/no);
return 0;
}
PROGRAMME - 22

Write a program to calculate average and total of 5 students for 3 subjects (use
nested for loops)
#include<stdio.h>

int main()
{
int student=0,sum=0,marks=0,sub;
for(student=0;student<5;student++)
{
sum=0;
printf("\n Enter Marks for Student - %d ",student+1);
for(sub=0;sub<3;sub++)
{
printf("\nEnter Marks for Subject - %d ",sub+1);
scanf("%d",&marks);
sum=sum+marks;
}
printf("\n For Student - %d : ",student+1);
printf("\n Sum = %d",sum);
printf("\n Average = %.2f",((float)sum)/sub);
}
return 0;
}
PROGRAMME - 23

Read five persons height and weight and count the number of person having
height greater than 170 and weight less than 50.
#include<stdio.h>

int main()
{
int person,height,weight,count=0;
for(person=0;person<5;person++)
{
printf("\n Enter Detail of Person - %d",person+1);
printf("\n Enter Height : ");
scanf("%d",&height);
printf("\n Enter Weight : ");
scanf("%d",&weight);
if(height>170)
{
if(weight<50)
{
count++;
}
}
}
printf("\n Total Person having Height > 170 and Weight < 50 : %d",count);
return 0;
}
PROGRAMME - 24

Write a program to check whether the given number is prime or not

#include<stdio.h>
int main()
{
int no,i;
printf("\n Enter No to check wheather its prime or not :");
scanf("%d",&no);
for(i=2;i<no;i++)
{
if(no%i==0)
{
printf("\n %d is not prime",no);
break;
}
}
if(no==i)
{
printf("\n %d is prime",no);
}
return 0;
}
PROGRAMME - 25

Write a program to evaluate the series 1^2+2^2+3^2+……+n^2


#include<stdio.h>

int main()
{
int n,i,sum=0;
printf("\n Enter Value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(i*i);
}
printf("\n Sum of Series = %d",sum);
return 0;
}
PROGRAMME - 26

Write a C program to find 1+1/2+1/3+1/4+….+1/n.


#include<stdio.h>
int main()
{
int n,i;
float sum=0;
printf("\n Enter Value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(1.0/i);
}
printf("\n Sum of Series = %f",sum);
return 0;
}
PROGRAMME - 27

Write a C program to find 1+1/2!+1/3!+1/4!+…..+1/n!

#include<stdio.h>

int main()
{
int n,i,j,fact=1;
float sum=0;
printf("\n Enter Value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=1;
for(j=i;j>0;j--)
{
fact=fact * j;
}
sum=sum+(1.0/fact);
}
printf("\n Sum of Series = %f",sum);
return 0;
}
PROGRAMME - 28

Write a program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4!……-


x^9/9!

#include<stdio.h>

int main()
{
int n,i,j,fact=1;
float sum=0;
printf("\n Enter Value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=1;
for(j=i;j>0;j--)
{
fact=fact * j;
}
sum=sum+(1.0/fact);
}
printf("\n Sum of Series = %f",sum);
return 0;
}
PROGRAMME - 29

Write a program to print following patterns :


i. * ii * iii *****
** * * ****
*** * * * ***
**** * * * * **
***** * * * * * *

#include <stdio.h>
int main(void)
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
printf(" *");
}
printf("\n");
}
return 0;
}
PROGRAMME -30

Write a C program to print following number patterns


i 1 ii 12345 iii 55555 iv 1
12 1234 4444 22
123 123 333 333
1234 12 22 4444
12345 1 1 55555

#include <stdio.h>

int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
printf("%d",j+1);
}
printf("\n");
}
return 0;
}
PROGRAMME - 31

Write a program to print following patterns:


I AAAAA ii ABCDE
BBBB ABCD
CCC ABC
DD AB
E A

#include <stdio.h>

int main(void)
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5-i;j++)
{
printf("%c",'A'+i);
}
printf("\n");
}
return 0;
}
PROGRAMME - 32

Write a C program to read and store the roll no and marks of 20 students using
array.

#include <stdio.h>

int main(void)
{
int rollno[20],marks[20],i;

for(i=0;i<20;i++)
{
printf("\n Enter Roll of Student [%d]",i+1);
scanf("%d",&rollno[i]);
printf("\n Enter Mark of Student [%d]",i+1);
scanf("%d",&marks[i]);
}

for(i=0;i<20;i++)
{
printf("\n Roll No : %d Marks : %d",rollno[i],marks[i]);
}

return 0;
}
PROGRAMME - 33

Write a program to find out which number is even or odd from list of 10 numbers
using array

#include <stdio.h>

int main(void)
{
int a[10],i;
for(i=0;i<9;i++)
{
printf("\n Enter Value in Array at Position [%d] :",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<9;i++)
{
if(a[i]%2==0)
{
printf("\n %d is an EVEN number.",a[i]);
}
else
{
printf("\n %d is an ODD number.",a[i]);
}
}
return 0;
}
PROGRAMME - 34

Write a program to find maximum element from 1-Dimensional array.

#include <stdio.h>
int main(void)
{
int a[50],i,n,max;
printf("\n Enter How many numbers you want to enter [Max 50] : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter Value in Array at Position [%d] :",i+1);
scanf("%d",&a[i]);
if(i==0)
{
max=a[i];
}
else
{
if(max<a[i])
{
max=a[i];
}
}
}
printf("\n Maximum Value in Array = %d",max);
return 0;
}
PROGRAMME - 35

Write a C program to calculate the average, geometric and harmonic mean of n


elements in an array.

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

int main()
{
float a[50],sum=0,sum1=0,sum2=1;
int i,n;
printf("\n How many numbers you want to enter :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter Value at Position [%d] : ",i+1);
scanf("%f",&a[i]);
sum=sum+a[i];
sum1=sum1+(1.0/a[i]);
sum2=sum2*a[i];
}
printf("\n Average = %f",sum/n);
printf("\n Geometric Mean = %f",pow(sum2,(1.0/n)));
printf("\n Harmonic Mean = %f",n*pow(sum1,-1));
return 0;
}
PROGRAMME - 36

Write a program to sort given array in ascending order (Use Insertion sort,
Bubble sort, Selection sort, Mergesort, Quicksort, Heapsort).

// Sorting of 1-D array using Selection sort

#include <stdio.h>
int main()
{
int a[10],i,j,n,min,temp;
printf("\n Enter How many numbers you want to enter: ");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("\n Enter Value at Position [%d] :",i+1);
scanf("%d",&a[i]);
}
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min = a[i];
for (j = i+1; j < n; j++)
{
if (a[j] < a[i])
{
min = j;
// Swap the found minimum element with the first element
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf(" %d ->",a[i]);
}
printf(" %d ->",a[i]);
return 0;
}
PROGRAMME - 37

Write a program to find a character from a given string.

#include <stdio.h>
int main()
{
char str[20],ch,flag=1;
int i=0;
printf("\n Enter String ");
gets(str);
printf("Enter Character to Search in String :");
scanf("%c",&ch);
printf("\n Character ");
for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
{
printf(" %d ",i+1);
flag=0;
}
}
if(flag==1)
{
printf("NOT FOUND");
}
return 0;
}
PROGRAMME - 38

Write a program to replace a character in given string.

#include<stdio.h>

int main()
{
char str[50],ch1,ch2;
int i;
printf("\n Enter String : ");
scanf("%[^\n]s",str);
fflush(stdin);

printf("\n Enter Character to Find : ");


scanf("%c",&ch1);
fflush(stdin);

printf("\n Enter Character to Replace : ");


scanf("%c",&ch2);

for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch1)
{
str[i]=ch2;
}
}
printf("\n Final String = %s",str);
return 0;
}
PROGRAMME 39

Write a program to delete a character in given string.


/* C Program to Remove All Occurrences of a Character in a String */

#include <stdio.h>
#include <string.h>

int main()
{
char str[100], ch;
int i, len, j;

printf("\n Please Enter any String : ");


gets(str);

printf("\n Please Enter the Character that you want to Remove : ");
scanf("%c", &ch);

len = strlen(str);

for(i = 0; i < len; i++)


{
if(str[i] == ch)
{
for(j = i; j < len; j++)
{
str[j] = str[j + 1];
}
len--;
i--;
}
}
printf("\n The Final String after Removing All Occurrences of '%c' = %s ", ch,
str);

return 0;
}
PROGRAMME - 40

Write a program to reverse a number.


#include <stdio.h>
int main()
{
int no,rev=0;
printf("\n Enter No to make it Reverse : ");
scanf("%d",&no);
while(no>0)
{
rev=(rev*10)+(no%10);
no=no/10;
}
printf("\n Reverse of entered no is : %d",rev);
return 0;
}
PROGRAMME - 41

Convert string into upper case


//41. Write a C program to convert string into upper case
#include<stdio.h>

int main()
{
char str[50];
int i;
printf("\n Enter String : ");
scanf("%[^\n]s",str);

for(i=0;str[i]!='\0';i++)
{
if(str[i]>='a' && str[i]<='z')
{
str[i]=str[i]-32;
}
}
printf("\n Upper Case String = %s",str);
return 0;
}
PROGRAMME - 42

Write a program that defines a function to add first n numbers.


#include <stdio.h>

int main()

int i, num, sum = 0;

printf("\nEnter an integer number : ");

scanf ("%d", &num);

for (i = 1; i <= num; i++)

sum = sum + i;

printf ("\nSum of first %d natural numbers = %d\n", num, sum);

}
PROGRAMME - 43
Write a function in the program to return 1 if number is prime otherwise return 0
/*
Write a function prime that returns 1
if it‘s argument is prime and return 0 otherwise.
*/

#include <stdio.h>

int prime(int );

int main()
{
int no;
printf("\n Enter any No : ");
scanf("%d",&no);
if(prime(no)==1)
{
printf(" %d is Prime",no);
}
else
{
printf(" %d is not Prime",no);
}
return 0;
}

int prime(int n)
{
int i=2;
while(i<n)
{
if(n%i==0)
{
break;
}
i++;
}

if(i==n)
{
return 1;
}
else
{
return 0;
}
}
PROGRAMME - 44

Write a function Exchange to interchange the values of two variables, say x and y.
illustrate the use of this function in a calling function.
/*
Write a function Exchange to interchange the values of two variables,
say x and y. illustrate the use of this function in a calling function.
*/

#include<stdio.h>
void swap(int *, int *);

int main()
{
int i=5,j=8;
printf("\n Values Before Exchange :");
printf("\n i = %d j = %d",i,j);
swap(&i,&j);
printf("\n Values After Exchange :");
printf("\n i = %d j = %d",i,j);
return 0;
}

void swap(int *a,int *b)


{
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
PROGRAMME - 45

Write a C program to use recursive calls to evaluate F(x) = x – x3 / 3! + x5 / 5 ! – x7


/ 7! + … xn/ n!.
/*
Write a C program to use recursive calls to evaluate
F(x) = x – x3 / 3! + x5 / 5 ! – x7 / 7! + … xn/ n!.
*/

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

float rec_call(int,int);
int fact(int);

int main()
{
int n,x;
float sum=0;
printf("\n Enter Value of X :");
scanf("%d",&x);
printf("\n Enter no of iteration n :");
scanf("%d",&n);
sum = rec_call(x,n);
printf("Sum = %f",sum);
return 0;
}

float rec_call(int x, int n)


{
static float sum;
if(n==1)
return sum+x;
if(n%2==0)
{
sum = sum - ((pow(x,(2*n)-1)*1.0) / fact((2*n)-1) ) ;
}
else
{
sum = sum + ((pow(x,(2*n)-1)*1.0) / fact((2*n)-1) ) ;
}
rec_call(x,--n);
}

int fact(int n)
{
if(n==1)
return 1;
return n * fact(n-1);
}
PROGRAMME - 46

Write a program to find factorial of a number using recursion.


#include <stdio.h>
int main()
{
int no,fact=1;
printf("\n Enter No to find its Factorial : ");
scanf("%d",&no);
while(no>1)
{
fact=fact*no;
no=no-1;
}
printf("\n Factorial of entered no is : %d",fact);
return 0;
}
PROGRAMME - 47

Write a C program using global variable, static variable.


/*
Write a C program using global variable, static variable.
*/

#include<stdio.h>

int fact();
int n;
int main()
{
printf("\n Enter Value of n :");
scanf("%d",&n);
printf("Factorial = %d",fact());
return 0;
}

int fact()
{
static int ans=1;
if(n==1)
{
return ans;
}
ans = n-- * fact();
}
PROGRAMME - 48

Write a function that will scan a character string passed as an argument and
convert all lowercase character into their uppercase equivalents
/*
Write a function that will scan a character string passed as an argument and convert all
lowercase character into their uppercase equivalents
*/

#include <stdio.h>

void UpperCase(char *);

int main(void)
{
char str[50];
printf("Enter String : ");
scanf("%s",str);
UpperCase(str);
printf("String in Upper Case : %s",str);
return 0;
}

void UpperCase(char *ch)


{
int i=0;
while(ch[i]!='\0')
{
if(ch[i]>='a' && ch[i]<='z')
{
ch[i]=ch[i]-32;
}
i++;
}
}
PROGRAMME - 49.

Write a C program to read structure elements from keyboard.

/*
Write a C program to read structure elements from keyboard.
*/

#include <stdio.h>

struct book
{
int id;
char name[20];
float price;
};

int main(void)
{
struct book b1;
printf("\n Enter Book Id : ");
scanf("%d",&b1.id);
fflush(stdin);
printf("\n Enter Book Name : ");
scanf("%[^\n]s",b1.name);
printf("\n Enter Book Price : ");
scanf("%f",&b1.price);

printf("\nBook Id = %d",b1.id);
printf("\nBook Name = %s",b1.name);
printf("\nBook Price = %.2f",b1.price);
return 0;
}
PROGRAMME - 50

Define a structure type struct personal that would contain person name, date of
joining and salary using this structure to read this information of 5 people and
print the same on screen.
/*
Define a structure type struct personal that would contain person name, date of joining
and salary using this structure to read this information of 5 people and print the same
on screen.
*/

#include <stdio.h>

struct person
{
char name[20];
char doj[10];
float salary;
}p[5];

int main(void)
{
int i=0;

for(i=0;i<5;i++)
{
printf("\n Enter Person Name : ");
scanf("%s",p[i].name);
printf("\n Enter Person Date of Joining (dd-mm-yyyy) : ");
scanf("%s",p[i].doj);
printf("\n Enter Person Salary : ");
scanf("%f",&p[i].salary);
}

for(i=0;i<5;i++)
{
printf("\n Person %d Detail",i+1);
printf("\n Name = %s",p[i].name);
printf("\n DOJ = %s",p[i].doj);
printf("\n Salary = %.2f",p[i].salary);
}
return 0;
}
PROGRAMME - 51.

Define structure data type called time_struct containing three member’s integer
hour, integer minute and integer second. Develop a program that would assign
values to the individual number and display the time in the following format: 16:
40:51

/*
Define structure data type called time_struct containing three member’s integer hour,
integer minute and integer second. Develop a program that would assign values to the
individual number and display the time in the following format: 16: 40:51
*/

#include <stdio.h>

struct time_struct
{
int hour;
int minute;
int second;
}t;

int main(void)
{
printf("\n Enter Hour : ");
scanf("%d",&t.hour);
printf("\n Enter Minute: ");
scanf("%d",&t.minute);
printf("\n Enter Second : ");
scanf("%d",&t.second);

printf("\n Time %d:%d:%d",t.hour%24,t.minute%60,t.second%60);

return 0;
}
PROGRAMME -52

Define a structure called cricket that will describe the following information:
Player name
Team name
Batting average
Using cricket, declare an array player with 50 elements and write a C program to
read the information about all the 50 players and print team wise list containing
names of players with their batting average.
/*
Define a structure called cricket that will describe the following information:
Player name
Team name
Batting average
Using cricket, declare an array player with 50 elements and write a C program to read
the information about all the 50 players and print team wise list containing names of
players with their batting average.
*/

#include <stdio.h>
#include <string.h>

struct cricket
{
char player_name[20];
char team_name[20];
float batting_avg;
}p[50],t;

int main(void)
{
int i=0,j=0,n=50;

for(i=0;i<n;i++)
{
printf("\n Enter Player Name : ");
scanf("%s",p[i].player_name);
printf("\n Enter Team Name : ");
scanf("%s",p[i].team_name);
printf("\n Enter Batting Average : ");
scanf("%f",&p[i].batting_avg);
}

//Sorting of Data based on Team


for(i=0;i<n-1;i++)
{
for(j=i;j<n;j++)
{
if(strcmp(p[i].team_name,p[j].team_name)>0)
{
t=p[i];
p[i]=p[j];
p[j]=t;
}
}
}

j=0;
for(i=0;i<n;i++)
{
if(strcmp(p[i].team_name,p[j].team_name)!=0 || i==0)
{
printf("\n Team Name: %s",p[i].team_name);
j=i;
}
printf("\n Player Name = %s",p[i].player_name);
printf("\n Batting Average = %f",p[i].batting_avg);
}
return 0;
}
PROGRAMME - 53

Design a structure student record to contain name, branch and total marks
obtained. Develop a program to read data for 10 students in a class and print
them.
/*
Design a structure student record to contain name, branch and total marks obtained.
Develop a program to read data for 10 students in a class and print them.
*/

#include <stdio.h>

struct student_record
{
char name[20];
char branch[20];
int total_marks;
}p[10];

int main(void)
{
int i=0,n=10;

for(i=0;i<n;i++)
{
printf("\n Enter Student Name : ");
scanf("%s",p[i].name);
printf("\n Enter Students Branch : ");
scanf("%s",p[i].branch);
printf("\n Enter Students Marks : ");
scanf("%d",&p[i].total_marks);
}

for(i=0;i<n;i++)
{
printf("\n Student %d Detail",i+1);
printf("\n Name = %s",p[i].name);
printf("\n Branch = %s",p[i].branch);
printf("\n Total marks = %d",p[i].total_marks);
}
return 0;
}
PROGRAMME - 54
Write a program to print address of variable using pointer.

/*
Write a C program to print address of variable using pointer.
*/

#include <stdio.h>

int main(void)
{
int i=15;
int *p;
p=&i;
printf("\n Address of Variable i = %u",p);
return 0;
}
PROGRAMME - 55
Write a C program to swap the two values using pointers.

#include <stdio.h>

void swapTwo(int *x, int *y)


{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

int main()
{
int num1, num2;

printf("Please Enter the First Value to Swap = ");


scanf("%d", &num1);

printf("Please Enter the Second Value to Swap = ");


scanf("%d", &num2);

printf("\nBefore Swapping: num1 = %d num2 = %d\n", num1, num2);

swapTwo(&num1, &num2);

printf("After Swapping : num1 = %d num2 = %d\n", num1, num2);

}
PROGRAMME - 55
Write a C program to swap the two values using pointers.

#include<stdio.h>
#include<conio.h>

void main()
{

//Declaration of variables and pointer variables


int x,y,*ptr_x,*ptr_y,temp;
clrscr();

//Insert value of x and y


printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);

//Printing value before swapping


printf("Before Swapping\nx = %d\ny = %d\n", x, y);

//Assigning address of variables to pointers


ptr_x = &x;
ptr_y = &y;

//Swapping pointer address with the help of temp variable


temp = *ptr_y;
*ptr_y = *ptr_x;
*ptr_x = temp;

//printing values of x and y


printf("After Swapping\nx = %d\ny = %d\n", x, y);
getch();
}
PROGRAMME - 56

Write a C program to print the address of character and the character of string
using pointer
/*C program to print a string using pointer.*/
#include <stdio.h>
int main()
{
char str[100];
char *ptr;

printf("Enter a string: ");


gets(str);

//assign address of str to ptr


ptr=str;

printf("Entered string is: ");


while(*ptr!='\0')
printf("%c",*ptr++);

return 0;
}
PROGRAMME- 57

Write a program to access elements using pointer.

#include<stdio.h>

int main()
{
int arr[5] = {100, 200, 300, 400, 500}, i;
int *ptr = arr;

//Address ptr+i
//Value stored at the address *(ptr+i)
for(i = 0; i < 5; i++)
printf("&arr[%d] = %p\t arr[%d] = %d\n",i,ptr+i,i,*(ptr+i));

return 0;
}
PROGRAMME - 58

Write a program for sorting using pointer.


#include <stdio.h>
#include <conio.h>
#include <alloc.h>
void main()
{

//Declaration of variables
int n,*p,i,j,temp;
clrscr();
printf("\nEnter n: ");
scanf("%d",&n);

//Memory allocation to pointer


p=(int *) malloc(n*2);
if(p==NULL)
{
printf("\nMemory allocation failed");
}

printf("\nEnter %d numbers",n);
for(i=0;i<n;i++)
{
scanf("%d",p+i);
}

//Accessing array for sorting


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(*(p+i)<*(p+j))
{
temp=*(p+i);
*(p+i)=*(p+j);
*(p+j)=temp;
}
}
}

//Printing sorted array


printf("\nTHE SORTED NUMBERS ARE:\n");
for(i=0;i<n;i++)
printf("%d ",*(p+i));
getch();
}
PROGRAMME - 59

Write a program to write a string in file

#include <stdio.h>

int main()
{
FILE *fp;
char filename[100];
char writestr[100];

// Read filename
printf("Enter a filename :");
gets(filename);

// Read string to write


printf("Enter the string to write :");
gets(writestr);

// Open file in write mode


fp = fopen(filename,"w+");

// If file opened successfully, then write the string to file


if ( fp )
{
fputs(writestr,fp);
}
else
{
printf("Failed to open the file\n");
}
//Close the file
fclose(fp);
return(0);
}
Programme - 60

A file named data contains series of integer numbers. Write a c program to read
all numbers from file and then write all odd numbers into file named “odd” and
write all even numbers into file named “even”. Display all the contents of these
file on screen

/*
60. A file named data contains a series of integer numbers. Write a c program to read all
numbers from file and then write all odd numbers into file named “odd” and write all
even numbers into file named “even”. Display all the contents of these file on screen
*/

#include<stdio.h>

int main()
{
FILE *f1,*f2,*f3;
int number,i, n=10;

printf("Contents of DATA file\n\n");

f1 = fopen("DATA","w");

for(i=0;i<n;i++)
{
scanf("%d",&number);
if(number==-1)
{
break;
}
putw(number,f1);
}
fclose(f1);

f1 = fopen("DATA","r");
f2 = fopen("ODD","w");
f3 = fopen("EVEN","w");

while((number = getw(f1)) != EOF)


{
if(number%2==0)
{
putw(number,f3);
}
else
{
putw(number,f2);
}
}

fclose(f1);
fclose(f2);
fclose(f3);

f2 = fopen("ODD","r");
f3 = fopen("EVEN","r");

printf("\n\n Contents of ODD file \n\n");

while((number = getw(f2)) != EOF)


{
printf("%d ",number);
}

printf("\n\nContents of EVEN file \n\n");

while((number = getw(f3)) != EOF)


{
printf("%d ",number);
}

fclose(f2);
fclose(f3);
return 0;
}

You might also like