C Programs Document
C Programs Document
#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;
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;
Output:
Enter the number
300
Given number = 300
Sum of the digits 300 = 3
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;
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.
#include <stdio.h>
Page 4 of 15
int main()
{
int number;
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.
#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
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
#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);
strcat(a,b);
return 0;
Output;
Enter the first string
vidya
Enter the second string
Nagar
String obtained on concatenation is vidyanagar
int main()
{
int i, n;
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
int main()
{
int first, second, *p, *q, sum;
p = &first;
q = &second;
sum = *p + *q;
return 0;
}
Output:
Enter two integers to add
15
25
Sum of entered numbers = 40
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
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
#include <stdio.h>
int main()
{
int num;
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 ") ;
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
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
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: ");
return 0;
}
Output:
Enter elements: 10
20
30
40
50
You entered:
10
20
30
40
50
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
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