1. Write a program that performs as a calculator ( addition, multiplication, division, subtraction).
/*
1. Write a program that performs as a calculator ( addition, multiplication, division, subtraction).
*/
#include<stdio.h>
int main()
{
int i,j;
printf("\n Enter 1st Integer Value :");
scanf("%d",&i);
printf("\n Enter 2nd Integer Value :");
scanf("%d",&j);
printf("\n addition : %d",i+j);
printf("\n multiplication : %d",i*j);
printf("\n division : %d",i/j);
printf("\n subtraction : %d",i-j);
return 0;
}
2. Write a program to find area of triangle(a=h*b*.5) a = area h = height b = base
/*
2. Write a program to find area of triangle.
(a=h*b*.5)
a = area h = height b = base
*/
#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 Area of triangle = %f",a);
return 0;
}
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
/*
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
*/
#include <stdio.h>
int main()
{
int n;
float p, r, I;
printf("\n Enter Amount p :");
scanf("%f",&p);
printf("\n Enter Rate r :");
scanf("%f",&r);
printf("\n Enter No of Years n :");
scanf("%d",&n);
I = (p*r*n)/100;
printf("\n Interest = %.2f",I);
return 0;
}
4. Write a C program to interchange two numbers.
/*
4. Write a C program to interchange two numbers.
*/
#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 Swapping Values a = %d b = %d",a,b);
return 0;
}
5. Write a C program to enter a distance to kilometre and convert it into meter, feet, inches and centimetres.
/*
5. Write a C program to enter a distance in to kilometer and convert it in to meter, feet, inches and centimeter
*/
#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;
}
6. Write a program to compute Fahrenheit from centigrade (f=1.8*c +32)
/*
6. Write a C program to compute Fahrenheit from centigrade (f=1.8*c +32)
*/
#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;
}
7. Write a C program to find out the distance travelled by the equation d = ut + at^2
/*
7. Write a C program to find out distance traveled by the equation d = ut + at^2
*/
#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 : %f",d);
return 0;
}
8. Write a C program to find that the accepted number is Negative, or Positive or Zero.
/*
8. Write a C program to find that the accepted number is Negative or Positive or 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;
}
9. Write a program to read marks of a student from the keyboard whether the student is pass or fails ( using if-else)
/*
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;
}
10. Write a program to read three numbers from the keyboard and find out the maximum out of these three. (nested if-else)
/*
10. Write a C program to read three numbers from keyboard and find out maximum out of these three. (nested if else)
*/
#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;
}
11. Write a C program to check whether the entered character is a capital, small letter, digit or any special character.
/*
11. Write a C program to check whether the entered character is capital, small letter, digit or any 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;
}
12. Write a program to read marks from the keyboard and your program should display equivalent grade according to the following table(if else ladder)
Marks Grade
100 - 80 Distinction
79 - 60 First Class
59 - 40 Second Class
< 40 Fail
/*
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;
}
13. Write a c program to prepare a pay slip using the 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.
/*
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;
}
14. Write a C program to read no 1 to 7 and print relatively day Sunday to Saturday.
/*
14. Write a C program to read no 1 to 7 and print relatively day Sunday to Saturday.
*/
#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;
}
15. Write a C program to find out the Maximum and Minimum numbers from the given 10 numbers
/*
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;
}
16. Write a C program to input an integer number and check the last digit of the number is even or odd.
/*
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);
// Condition can be write as
// if(i%2==0)
// ultimately Even number has last digit even and same for odd
if((i%10)%2==0)
{
printf("\n Last Digit of Number is Even");
}
else
{
printf("\n Last Digit of Number is Odd");
}
return 0;
}
17. Write a C program to find the factorial of a given number.
/*
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;
}
18. Write a program to reverse a number.
/*
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;
}
19. Write a program to generate the first n number of the Fibonacci series
/*
19. Write a C program to generate first n number of Fibonacci series
*/
#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;
}
20. Write a program to find out the sum of the first and last digits of a given number.
/*
20. Write a C 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;
}
21. Write a C program to find the sum and average of different numbers which are accepted by using as many as the user wants
/*
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 nos 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;
}
22. Write a program to calculate the average and total of 5 students for 3 subjects (use nested for loops)
/*
22. Write a C 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;
}
23. Read five persons’ height and weight and count the number of people having a height greater than 170 and weight less than 50,
/*
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;
}
24. Write a program to check whether the given number is prime or not.
/*
24. Write a C 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 whether 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;
}
25. Write a program to evaluate the series 1^2+2^2+3^2+……+n^2
/*
25. Write a C 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;
}
26. Write a C program to find 1+1/2+1/3+1/4+....+1/n.
/*
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;
}
27. Write a C program to find 1+1/2!+1/3!+1/4!+.....+1/n!.
/*
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;
}
28. Write a program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4!......-x^9/9!
28. Write a C program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4!......-x^9/9!
*/
#include<stdio.h>
#include<math.h>
int main()
{
int n,i,j,x,fact=1;
float sum=0;
printf("\\n Enter Highest Power Value (Max 9):");
scanf("%d",&n);
printf("\\n Enter the Value of X :");
scanf("%d",&x);
for(i=0;i<=n;i++)
{
fact=1;
for(j=i;j>0;j--)
{
fact=fact*j;
}
if(i%2==0)
{
sum=sum+(pow(x,i)/fact);
}
else
{
sum=sum-(pow(x,i)/fact);
}
}
printf("\\n Sum of Series = %f",sum);
return 0;
}
/*
OUTPUT:
Enter Highest Power Value (Max 9):3
Enter the Value of X :2
Sum of Series = -0.333333
*/
29. Write a program to print the following patterns :
//(i)
#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;
}
//(ii)
#include <stdio.h>
int main(void)
{
int i,j;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
printf(" *");
}
printf("\\n");
}
return 0;
}
//(iii)
#include <stdio.h>
int main(void)
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<i;j++)
{
printf(" ");
}
for(j=i;j<5;j++)
{
printf("*");
}
printf("\\n");
}
return 0;
}
30. Write a program to print the following patterns :
//(i)
#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;
}
//(ii)
#include <stdio.h>
int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5-i;j++)
{
printf("%d",j+1);
}
printf("\n");
}
return 0;
}
//(iii)
#include <stdio.h>
int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5-i;j++)
{
printf("%d",5-i);
}
printf("\n");
}
return 0;
}
//(iv)
#include <stdio.h>
int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
printf("%d",i+1);
}
printf("\n");
}
return 0;
}
31. Write a program to print the following patterns:
//(i)
#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;
}
//(ii)
#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'+j);
}
printf("\n");
}
return 0;
}
32. Write a C program to read and store the roll no and marks of 20 students using an array.
/*
32. Write a C program to read and store the roll no and marks of 20 students using an array.
*/
#include <stdio.h>
int main(void)
{
int rollno[20],marks[20],i;
for(i=0;i<20;i++)
{
printf("Enter Roll of Student [%d] : ",i+1);
scanf("%d",&rollno[i]);
printf("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;
}
33. Write a program to find out which number is even or odd from a list of 10 numbers using an array
/*
33. Write a C 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;
}
34. Write a program to find the maximum element from the 1-Dimensional array.
/*
34. Write a C program to find a 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;
}
35. Write a C program to calculate the average, geometric and harmonic mean of n elements in an array.
/*
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;
}
/*
OUTPUT :
How many numbers you want to enter :5
Enter Value at Position [1] : 12
Enter Value at Position [2] : 23
Enter Value at Position [3] : 14
Enter Value at Position [4] : 11
Enter Value at Position [5] : 17
Average = 15.400000
Geometric Mean = 14.851686
Harmonic Mean = 14.368940
*/
36. Write a program to sort a given array in ascending order (Use Insertion sort, Bubble sort, Selection sort, Merge sort, Quicksort, Heapsort).
/*
36. Write a C 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;
}
37. Write a program to find a character from a given string.
//37. Write a C program to find a character from 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;
}
38. Write a program to replace a character in the given string.
//38. Write a C 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;
}
39. Write a program to delete a character in the given string.
//39. Write a C program to delete a character in given string.
#include<stdio.h>
#include<stdlib.h>
int main()
{
char str[50],ch;
int i,j;
printf("\n Enter String : ");
scanf("%[^\n]s",str);
fflush(stdin);
printf("\n Enter Character to Delete : ");
scanf("%c",&ch);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
{
for(j=i;j<str[j]!='\0';j++)
{
str[j]=str[j+1];
}
i--;
}
}
printf("\n Final String = %s",str);
return 0;
}
40. Write a program to reverse the string.
//40. Write a C program to reverse string.
#include<stdio.h>
int main()
{
char str[50],rev[50];
int i,j;
printf("\n Enter String to Reverse : ");
scanf("%[^\n]s",str);
for(i=0;str[i]!='\0';i++)
{
}
i--;
for(j=0;i>=0;j++,i--)
{
rev[j]=str[i];
}
rev[j]='\0';
printf("\n Reverse String = %s",rev);
return 0;
}
41. Write a program to convert the 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;
}
42. Write a program that defines a function to add first n numbers.
/*
42. Write a C program that defines a function to add first n numbers.
*/
#include <stdio.h>
int getsum(int );
int main(void)
{
int n;
printf("Enter Any number n = ");
scanf("%d",&n);
printf("\n SUM = %d",getsum(n));
return 0;
}
int getsum(int n)
{
return ((n*(n+1))/2);
}
43. Write a function in the program to return 1 if the 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;
}
}
44. Write a functioning Exchange to interchange the values of two variables, say x and y. illustrate the use of this function in a calling function.
/*
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.
*/
#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;
}
45. Write a C program to use recursive calls to evaluate F(x) = x – x^3 / 3! + x^5 / 5 ! – x^7 / 7! + … x^n/ n!.
/*
45. 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);
}
46. Write a program to find the factorial of a number using recursion.
/*
46. Write a C program to find factorial of a number using recursion.
*/
#include<stdio.h>
int fact(int);
int main()
{
int n;
printf("\n Enter Value of n :");
scanf("%d",&n);
printf("Factorial = %d",fact(n));
return 0;
}
int fact(int n)
{
if(n==1)
{
return 1;
}
return n * fact(n-1);
}
47. Write a C program using a global variable and static variable.
/*
47. 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();
}
48. Write a function that will scan a character string passed as an argument and convert all lowercase characters into their uppercase equivalents
/*
48. 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++;
}
}
49. Write a program to read structure elements from the keyboard.
/*
49. 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;
}
50. Define a structure type struct personal that would contain person's name, date of joining and salary using this structure to read this information of 5 people and print the same on screen.
/*
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.
*/
#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;
}
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
/*
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;
}
52. Define a structure called cricket that will describe the following information:
Player name
Team name
Batting average
Using cricket, declare an array of players with 50 elements and write a C program to read the information about all the 50 players and print a team-wise list containing the names of players with their batting average.
/*
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.
*/
#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;
}
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.
/*
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.
*/
#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;
}
54. Write a program to print the address of a variable using a pointer.
/*
54. 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;
}
55. Write a C program to swap the two values using pointers.
/*
55. Write a C program to swap the two values using pointers.
*/
#include <stdio.h>
void swap(int *,int *);
int main(void)
{
int i=15,j=20;
printf("\n Before Swapping i = %d j = %d",i,j);
swap(&i,&j);
printf("\n After Swapping i = %d j = %d",i,j);
return 0;
}
void swap(int *a,int *b)
{
*a=*a + *b;
*b=*a - *b;
*a=*a - *b;
}
56. Write a C program to print the address of the character and the character of the string using a pointer.
/*
56. Write a C program to print the address of character and the character of string using the pointer.
*/
#include <stdio.h>
int main(void)
{
char str[50];
char *ch;
printf("\n Enter String : ");
scanf("%s",str);
ch=&str[0];
while(*ch!='\0')
{
printf("\n Position : %u Character : %c",ch,*ch);
ch++;
}
return 0;
}
57. Write a program to access elements using a pointer.
/*
57. Write C program to access elements using pointer.
*/
#include <stdio.h>
int main(void)
{
int a[10]={2,4,6,7,8,9,1,2,3,4};
int *p,i=0;
p=&a[0];
while(i<10)
{
printf("\n Position : %d Value : %d",i+1,*(p+i));
i++;
}
return 0;
}
58. Write a program for sorting using a pointer.
/*
58. Write C program for sorting using a pointer.
*/
#include <stdio.h>
int main(void)
{
int a[10]={2,10,6,7,8,9,5,3,4,1};
int *p,i=0,j=0;
p=&a[0];
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(*(p+i) > *(p+j))
{
*(p+i) = *(p+i) + *(p+j);
*(p+j) = *(p+i) - *(p+j);
*(p+i) = *(p+i) - *(p+j);
}
}
}
printf("\n Sorted Values : ");
for(i=0;i<10;i++)
{
printf("%d ",*(p+i));
}
return 0;
}
59. Write a program to write a string in the file
/*
59. Write a C program to write a string in file
*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
char str[50],ch[50];
FILE *fp;
printf("Enter String : ");
scanf("%[^\n]s",str);
fp = fopen("Test.txt","w");
fputs(str,fp);
fclose(fp);
fp = fopen("Test.txt","r");
fgets(ch,50,fp);
fclose(fp);
printf("\n String from File : %s",ch);
return 0;
}
60. A file named data contains a series of integer numbers. Write a c program to read all numbers from the file and then write all odd numbers into a file named “odd” and write all even numbers into a file named “even”. Display all the contents of these files 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;
}
Top comments (1)
In 21 (i), (ii) and (iii)
There are two slashes for new line,
printf ("\n");
I think this is an error.