Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

5. write a C program to display days of a week using switch case.

#include <stdio.h>
int main()
{
int week;
/* Input week number from user */
printf("Enter week number(1-7): ");
scanf("%d", &week);
switch(week)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid input! Please enter week number between 1-7.");
}
return 0;
}

Output:
Enter week number(1-7): 3
Wednesday

6.write a C program to print all upper case and lower case alphabets

#include <stdio.h>

int main()
{

Page 1 of 15
char i;

printf("Capital (upper) case characters:\n");


for(i='A'; i<='Z'; i++)
printf("%c ",i);

printf("\n\nLower case characters:\n");


for(i='a'; i<='z'; i++)
printf("%c ",i);

return 0;
}
Output:
Capital (upper) case characters:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Lower case characters:
abcdefghijklmnopqrstuvwxyz

9.write a C Program to Search for given Element in an array using LINEAR SEARCH
#include <stdio.h>
void main()
{
int a[10], i, item;
printf("\nEnter SEVEN elements of an array:\n");
for (i=0; i<=6; i++)
scanf("%d", &a[i]);
printf("\nEnter item to search: ");
scanf("%d", &item);
for (i=0; i<=9; i++)
if (item == a[i])
{
printf("\nItem found at location %d", i+1);
break;
}
if (i > 9)
printf("\nItem does not exist.");
getch();
}

Output:
Enter SEVEN elements of an array:
12 25 45 89 54 68 88
Enter item to search: 89
Item found at location 4

Page 2 of 15
10.write a C Program to find the Sum of Digits of a given Integer

#include <stdio.h>
void main()
{
long num, temp, digit, sum = 0;

printf("Enter the number \n");


scanf("%ld", &num);
temp = num;
while (num > 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
printf("Given number = %ld\n", temp);
printf("Sum of the digits %ld = %ld\n", temp, sum);
}

Output:
Enter the number
300
Given number = 300
Sum of the digits 300 = 3

12.write a c program to find the largest element in a given array of elements


#include <stdio.h>
int main()
{
int i, n;
int arr[100];

printf("Enter total number of elements(1 to 100): ");


scanf("%d", &n);
printf("\n");

// Stores number entered by the user


for(i = 0; i < n; ++i)
{
printf("Enter Number %d: ", i+1);
scanf("%d", &arr[i]);
}

// Loop to store largest number to arr[0]


for(i = 1; i < n; ++i)
{

Page 3 of 15
// Change < to > if you want to find the smallest element
if(arr[0] < arr[i])
arr[0] = arr[i];
}
printf("Largest element = %d", arr[0]);

return 0;
}
Output:
Enter total number of elements(1 to 100): 6
Enter Number 1: 20
Enter Number 2: 30
Enter Number 3: 15
Enter Number 4: 64
Enter Number 5: 15
Enter Number 6: 55
Largest element = 64

15.C program to find square of given number using user defined function

#include<stdio.h>
int square(int); // function prototype declaration.
void main()
{
int number, answer;

printf("Enter your number:");


scanf("%d", &number);

answer = square(number); //Call function.

printf("Square of %d is %d.", number, answer);


}

int square(int n)
{
//function logic is written here..
return(n*n); //This will return answer to main function.
}

Output:
Enter your number:5
Square of 5 is 25.

16. C program to check whether number is EVEN or ODD using switch.

#include <stdio.h>

Page 4 of 15
int main()
{
int number;

printf("Enter a positive integer number: ");


scanf("%d",&number);

switch(number%2) //this will return either 0 or 1


{
case 0:
printf("%d is an EVEN number.\n",number);
break;
case 1:
printf("%d is an ODD number.\n",number);
break;
}

return 0;
}
Output:
First run:
Enter a positive integer number: 10
10 is an EVEN number.

Second run:
Enter a positive integer number: 11
11 is an ODD number.

18.C Program to display Factorial of a given Number using While Loop

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,f;
f=i=1;
clrscr();
printf("Enter a Number to Find Factorial: ");
scanf("%d",&n);
while(i<=n)
{
f=f*i;
i++;
}
printf("The Factorial of %d is : %d",n,f);
getch();

Page 5 of 15
}

Output:
Enter a Number to Find Factorial: 5
The Factorial of 5 is : 120

20.write a to check whether a number is prime or not


#include <stdio.h>

main() {
int n, i, c = 0;
printf("Enter any number n:");
scanf("%d", &n);

/*logic*/
for (i = 1; i <= n; i++) {
if (n % i == 0) {
c++;
}
}

if (c == 2) {
printf("Given number is a Prime number");
}
else {
printf("Given number is not a Prime number");
}
return 0;
}
Output:
First time run:
Enter any number n:10
Given number is not a Prime number

second time run:


Enter any number n:5
Given number is a Prime number

22.Write a C program to Concatenate two Strings using string functions

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

int main()
{
char a[100], b[100];

Page 6 of 15
printf("Enter the first string\n");
gets(a);

printf("Enter the second string\n");


gets(b);

strcat(a,b);

printf("String obtained on concatenation is %s\n",a);

return 0;

Output;
Enter the first string
vidya
Enter the second string
Nagar
String obtained on concatenation is vidyanagar

25.write a C program C program to print all natural numbers from 1 to n


#include <stdio.h>

int main()
{
int i, n;

printf("Enter any number: ");


scanf("%d", &n);

printf("Natural numbers from 1 to %d : \n", n);

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


{
printf("%d\n", i);
}

return 0;
}

Output:
Enter any number: 10
Natural numbers from 1 to 10 :
1

Page 7 of 15
2
3
4
5
6
7
8
9
10

26.write a C program to add two numbers using pointers


#include <stdio.h>

int main()
{
int first, second, *p, *q, sum;

printf("Enter two integers to add\n");


scanf("%d%d", &first, &second);

p = &first;
q = &second;

sum = *p + *q;

printf("Sum of entered numbers = %d\n",sum);

return 0;
}

Output:
Enter two integers to add
15
25
Sum of entered numbers = 40

29. write a C program to count the number of vowels in a given string


#include <stdio.h>
#include <conio.h>
void main()
{
char a[100];
int len,i,vow=0;
clrscr();
printf("\nENTER A STRING: ");
gets(a);
len=strlen(a);

Page 8 of 15
for(i=0;i<len;i++)
{
if(a[i]=='a' || a[i]=='A' || a[i]=='e' || a[i]=='E' || a[i]=='i' ||
a[i]=='I' || a[i]=='o' || a[i]=='O' || a[i]=='u' || a[i]=='U')
vow=vow+1;
}
printf("\nTHERE ARE %d VOWELS IN THE STRING",vow);
getch();
}
Output:
ENTER A STRING: vasundara
THERE ARE 4 VOWELS IN THE STRING

30.write a c program to reverse a string using recursion


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

void reverse(char [], int, int);


int main()
{
char str1[20];
int size;

printf("Enter a string to reverse: ");


scanf("%s", str1);
size = strlen(str1);
reverse(str1, 0, size - 1);
printf("The string after reversing is: %s\n", str1);
return 0;
}

void reverse(char str1[], int index, int size)


{
char temp;
temp = str1[index];
str1[index] = str1[size - index];
str1[size - index] = temp;
if (index == size / 2)
{
return;
}
reverse(str1, index + 1, size);
}

Output:
Enter a string to reverse: language
The string after reversing is: egaugnal

Page 9 of 15
34.write a C program to display the length and reverse of a string using string functions
#include<stdio.h>
#include<conio.h>
void main()
{
char a[10];int length;
printf("Enter a string:");

gets(a);
length=strlen(a);
printf("\n length of string is %d",length);
printf("\n string after revesed is %s", strrev(a));
getch();
}

Output:
Ente a string: asdfg
length of string is 5
string after revesed is gfdsa

35.C program to check even or odd number using conditional operator

#include <stdio.h>

int main()
{
int num;

/* Input number from user */


printf("Enter any number to check even or odd: ");
scanf("%d", &num);

/* Print Even if (num%2==0) */


printf("The number is %s", (num%2==0 ? "EVEN" : "ODD"));

return 0;
}

Output:
First time run:
Enter any number to check even or odd: 55
The number is ODD
Second time run:
Enter any number to check even or odd: 50
The number is EVEN

Page 10 of 15
37.write a c program to accept and display the details of an employee using structures

#include<stdio.h>
//include<conio.h>
struct emp
{
int empno ;
char name[10] ;
int bpay, allow, ded, npay ;
}e;
void main()
{
//int i, n ;
printf("Enter employee details : \n ") ;

printf("\nEnter the employee number : ") ;


scanf("%d", &e.empno) ;
printf("\nEnter the name : ") ;
scanf("%s", e.name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;
scanf("%d %d %d", &e.bpay, &e.allow, &e.ded) ;
e.npay = e.bpay + e.allow - e.ded ;

printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;

printf("%d \t %s \t %d \t %d \t %d \t %d \n", e.empno,


e.name, e.bpay, e.allow, e.ded, e.npay) ;

getch() ;
}

Output:
Enter employee details :
Enter the employee number : 121
Enter the name : john
Enter the basic pay, allowances & deductions : 1000
1000
500
Emp. No. Name Bpay Allow Ded Npay
121 john 1000 1000 500 1500

40.write a c program to display length of a string using points


#include<stdio.h>
int main() {
char str[20], *pt;

Page 11 of 15
int i = 0;
printf(" Find or Calculate Length of String \n");
printf("Enter Any string [below 20 chars] : ");
gets(str);
pt = str;
while (*pt != '\0') {
i++;
pt++;
}
printf("Length of String : %d", i);

return 0;
}

Output:
Find Length of String
Enter Any string [below 20 chars] : asdfgh
Length of String : 6

42.write a C program to find the sum of the Series 12 +22 +33+.+N2


#include<stdio.h>
int main()
{
int i,N;
unsigned long sum;

/*read value of N*/


printf("Enter the value of N: ");
scanf("%d",&N);

/*set sum by 0*/


sum=0;

/*calculate sum of the series*/


for(i=1;i<=N;i++)
sum= sum+ (i*i);

/*print the sum*/

printf("Sum of the series is: %ld\n",sum);

return 0;
}

Output:
Enter the value of N: 5
Sum of the series is: 55

Page 12 of 15
43. write a C Program to display Elements of an given Array Using Pointer
#include <stdio.h>
int main()
{
int data[5], i;
printf("Enter elements: ");

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


scanf("%d", data + i);

printf("You entered: \n");


for(i = 0; i < 5; ++i)
printf("%d\n", *(data + i));

return 0;
}

Output:
Enter elements: 10
20
30
40
50
You entered:
10
20
30
40
50

47. write a C program to find whether a triangle is equilateral isosceles or scalene


#include "stdio.h"

void main()
{
int x,y,z;
printf("\nEnter the sides of a triangle");
scanf("%d %d %d",&x,&y,&z);
if((x==y) && (y==z))
{
printf("\nThe triangle is equilateral");
}
else if((x==z) || (y==z) || (x==y))
{
printf("\nThe triangle is isoseles");
}

Page 13 of 15
else
{
printf("\nThe triangle is scalene");
}
getch();
}

Output:
Enter the sides of a triangle10
12
10
The triangle is isoseles

48.Write a C program to calculate simple and compound interest.


#include<stdio.h>
#include<math.h>
int main()
{
float p,q,r,SI,CI;
int n;
printf("Enter the value of Principal p = ");
scanf("%f",&p);
printf("Enter the value of Rate r = ");
scanf("%f",&r);
printf("Enter the value of Period in year n = ");
scanf("%d",&n);
SI = ((p*r*n)/100);
printf("Simple Interest SI=%f \n",SI);
q = 1+(r/100);
CI=p*pow(q,n)-p;
printf("Compound Interest CI=%f \n",CI);
return 0;
}
Output:
Enter the value of Principal p = 10000
Enter the value of Rate r = 3
Enter the value of Period in year n = 2
Simple Interest SI=600.000000
Compound Interest CI=608.999390

50. write a C program to find largest of two numbers using user-defined functions
#include<stdio.h>
int largest(int x, int y);
void main()
{
int a, b, large;

Page 14 of 15
printf("\n Enter values for a and b:");
scanf("%d%d",&a,&b);
large = largest(a,b);
printf("\n Larger amongst %d and %d is %d\n",a, b, large);
getch();
}
int largest(int x, int y)
{
if(x>y)
return x;
else
return y;
}
Output:
Enter values for a and b:25
62
Larger amongst 25 and 62 is 62

Page 15 of 15

You might also like