C Prog
C Prog
//remainder on division by 7
dayOfWeek = dayOfWeek % 7;
switch (dayOfWeek) {
case 0:
printf("weekday = Saturday");
break;
case 1:
printf("weekday = Sunday");
break;
case 2:
printf("weekday = Monday");
break;
case 3:
printf("weekday = Tuesday");
break;
case 4:
printf("weekday = Wednesday");
break;
case 5:
printf("weekday = Thursday");
break;
case 6:
printf("weekday = Friday");
break;
Area of scalen triangle : Area of right angle triangle :
#include<stdio.h> #include<stdio.h>
#include<math.h>
int main() {
int main() { int base, height;
int s1, s2, angle; float area;
float area;
printf("\nEnter the base of Right Angle Triangle : ");
printf("\nEnter Side1 : "); scanf("%d", &base);
scanf("%d", &s1);
printf("\nEnter the height of Right Angle Triangle : ");
printf("\nEnter Side2 : "); scanf("%d", &height);
scanf("%d", &s2);
area = 0.5 * base * height;
printf("\nEnter included angle : "); printf("\nArea of Right Angle Triangle : %f", area);
scanf("%d", &angle);
return (0);
area = (s1 * s2 * sin((M_PI / 180) * angle)) / 2; }
Enter Side1 : 3
Enter Side2 : 4
reads the customer number and power consumed
Enter included angle : 30
Area of Scalene Triangle : 3.000
and prints the amount to be paid by the customer.
1
Area of equi triangle :
2 Consumption Rate of Units Charge
#include<stdio.h>
#include<math.h>
3 0-200 Rs.0.50 per unit
int main() {
int side;
float area, r_4; 4 201-400 Rs.100 plus Rs.0.65 per unit excess 200
r_4 = sqrt(3) / 4; 5 401-600 Rs.230 plus Rs.0.80 per unit excess of 400.
void main() {
Output :
int cust_no, powerUsage;
Enter the Length of Side : 5
Area of Equilateral Triangle : 10 float amount;
clrscr();
scanf("%d", &powerUsage);
else if (powerUsage > 400 && powerUsage <= 600) printf("\nEnter temp in Celsius : ");
scanf("%f", &celsius);
amount = 230 + ((powerUsage - 400) * 0.80);
fahrenheit = (1.8 * celsius) + 32;
printf("Amount to be paid by customer no. %d is printf("\nTemperature in Fahrenheit : %f ", fahrenheit);
Rs.:%5.2f.", cust_no, amount);
return (0);
}
getch();
} Output :
#include<stdio.h>
GREATEST OF 3 NO #include<math.h>
if ((c > a) && (c > b)) root1 = (-b + desc) / (2.0 * a);
printf("\nc is greatest"); root2 = (-b - desc) / (2.0 * a);
return (0); }
Output :
} rev = rev * 10 + rem;
num = num / 10;
}
Output :
printf("\nReversed Number : %d", rev);
Enter the Values of a : 1 return (0);
Enter the Values of a : -5 }
#include<stdio.h>
#include<conio.h>
Output :
Reverse of given no :
Enter the term : 5
#include<stdio.h>
Enter First Number : 1
int main() {
int num, rem, rev = 0; Enter Second Number : 30
printf("\nEnter any no to be reversed : ");
Fibonacci Series : 1 3 4 7 11 18 29
scanf("%d", &num);
ARMSTRONG NO : Palindrome :
#include<stdio.h> #include<stdio.h>
#include<string.h>
int main() {
int num, temp, sum = 0, rem; int main() {
int num, i, count = 0;
printf("\nEnter number for checking Armstrong : "); char str1[10], str2[10];
scanf("%d", &num);
printf("nEnter a number:");
temp = num; scanf("%d", &num);
PRIME NO : OUTPUT :
bin_dec(num);
Decimal to hexadecimal :
getch();
} #include<stdio.h>
#include<conio.h>
#include<math.h>
Output :
void dec_hex(long int num) // Function Definition
{
Enter the Binary number : 111 long int rem[50],i=0,length=0;
Decimal number : 7 while(num>0)
{
rem[i]=num%16;
num=num/16;
i++;
length++;
Decimal to binary : }
fseek(fp, 0, SEEK_END);
Program to copy the contents of one file into another length = ftell(fp);
using fgetc and fputc function fseek(fp, (length - num), SEEK_SET);
do {
1 #include<stdio.h> ch = fgetc(fp);
2 #include<process.h> putchar(ch);
3 } while (ch != EOF);
4 void main() {
5 FILE *fp1, *fp2; fclose(fp);
6 char a; return(0);
7 clrscr(); }
8
9 fp1 = fopen("test.txt", "r");
Output : gets(fname2);
if (fp1 == NULL) {
exit(1);
Convert the file contents in Upper-case & Write
Contents in a Output file } else if (fp2 == NULL) {
fcloseall(); fclose(fp2);
getch();
} }
return (0);
Compare two files :
}
#include<stdio.h>
Copy Text From One File to Other File
int main() {
struct Student {
SUM OF NO FROM 1 TO N
int roll;
#include<stdio.h>
char name[12];
int calculateSum(int);
int percent;
int main() {
} s1 = { 10, "SMJC", 80 }; int i, num;
int result;
int main() {
printf("Input a number : ");
scanf("%d", &num);
FILE *fp;
result = calculateSum(num);
struct Student s2; printf("\nSum of Number From 1 to %d : %d", num, result);
fclose(fp);
Output :
printf("\nRoll : %d", s2.roll);
Input a number : 5
printf("\nName : %s", s2.name);
Sum of Number From 1 to 5 :15
printf("\nPercent : %d", s2.percent); SUM OF DIGITS USING RECURSION
int size;
struct stud s;
TOWER OF HANOI
void TOH(int num, char x, char y, char z); printf("nSize of Structure : %d", size);
} xyz;
int main() {
return (0);
#include<stdio.h>
int main() { To display the highest literate rate and the highest
income of a state using array of structures
float a = 3.14, *fptr;
#include<stdio.h>
fptr = &a;
#define M 50
printf("Size of Float Pointer : %d", sizeof(fptr));
struct state {
return (0);
char name[50];
}
long int population;
Output:
float literacyRate;
Size of Float Pointer : 2
float income;
integer pointer
} st[M]; /* array of structure */
#include<stdio.h>
int main() {
int main() {
int a = 10, *ptr;
int i, n, ml, mi, maximumLiteracyRate, maximumIncome;
ptr = &a;
float rate;
printf("Size of int pointer : %d", sizeof(ptr));
ml = mi = -1;
return (0);
maximumLiteracyRate = maximumIncome = 0;
}
printf("Enter how many states:");
Output :
scanf("%d", &n);
Size of int pointer : 2
for (i = 0; i < n; i++) {
character pointer
printf("\nEnter state %d details :", i);
#include<stdio.h>
printf("\nEnter state name : ");
int main() {
scanf("%s", &st[i].name);
char a = 'a', *cptr;
printf("\nEnter total population : ");
cptr = &a;
scanf("%ld", &st[i].population);
printf("\nSize of Char Pointer : %d", sizeof(cptr));
printf("\nEnter total literary rate : ");
return (0);
scanf("%f", &rate);
}
st[i].literacyRate = rate;
Output:
maximumLiteracyRate = st[i].literacyRate;
printf("\nState with highest literary rate :%s", Write a program to use structure within union , display
st[ml].name); the contents of structure elements
#include<conio.h>
} struct student {
int rollno;
} gets(str);
Enter details:
if (*str == ' ') {
Enter name : Pritesh
// counting number of blank spaces.
Enter rollno: 10
pos = high;
Enter sex: M
++nob;
Enter percentage: 89
} else if (pos == high) {
++now;
Rollno : 10
Sex : M }
#include<stdlib.h>
case 'o':
int main() {
case 'u':
char *str[5], *temp;
case 'A':
int i, j, n;
case 'E':
printf("\nHow many names do you want to have?");
case 'I':
scanf("%d", &n);
case 'O':
for (i = 0; i < n; i++) {
case 'U':
printf("\nEnter the name %d: ", i);
++nov;
flushall();
break; gets(str[i]);
} }
strcpy(temp, str[j]);
str++;
} p++;
}
Output :
return count;
1
How many names do you want to have? 4
2 Output :
Enter the name 0: pri
3 1 Enter the String : pritesh
Enter the name 1: prt
4 2 Length of the given string pritesh is : 7
Enter the name 2: prq
5
Enter the name 3: pra
6
Sorted List : pra pri prq prt
7
#include<stdio.h>
#include<conio.h>
int string_ln(char*);
void main() {
char str[20];
int length;
clrscr();
gets(str);
length = string_ln(str);
getch();
} to Concat Two Strings without Using Library Function
Output : #include<stdio.h>
#include<string.h>
1 emocleW ot gnimmargorP dlroW
void concat(char[], char[]);
Program : Delete all occurrences of Character from the
String int main() {
printf("\nEnter character which you want to delete : "); printf("nConcated string is :%s", s1);
scanf("%ch", &ch);
return (0);
del(str, ch);
getch(); }
}
void concat(char s1[], char s2[]) { i++;
int i, j; j--;
i = strlen(s1); }
}
Output :
s1[i] = '\0';
Output of Program :
j = strlen(str) - 1; #include<stdio.h>
#include<stdio.h>
Output :
int FindLength(char str[]);
1 Enter The String : Pritesh A Taral int main() {
2 Uppercase Letters : 3 char str[100];
3 Lowercase Letters : 10 int length;
printf("\nEnter the String : ");
gets(str);
length = FindLength(str);
swap(&num1, &num2);
1 Enter 10 elements : 11 12 13 14 15 16 17 18 19 20
printf("\nFirst number : %d", num1);
2 The sum of array elements is 155
printf("\nSecond number : %d", num2);
Output : #include<stdio.h>
#include<conio.h>
1 Enter the first number : 12 #define MAX_ROWS 3
#define MAX_COLS 4
2 Enter the Second number : 22
void print_square(int[]);
3 First number : 22
void main(void) {
4 Second number : 12 int row;
int num[MAX_ROWS][MAX_COLS] = { { 0, 1, 2, 3 },
{ 4, 5, 6, 7 }, printf("\nTranspose matrix is :");
{ 8, 9, 10, 11 } }; for (i = 0; i < size; i++) {
printf("\n");
for (row = 0; row < MAX_ROWS; row++) for (j = 0; j < size; j++) {
print_square(num[row]); printf("%d\t", arr[i][j]);
} }
}
void print_square(int x[]) {
int col; getch();
for (col = 0; col < MAX_COLS; col++) }
printf("%d\t", x[col] * x[col]);
printf("\n");
}
Output :
0 1 4 9
16 25 36 49
64 81 100 121
Output : Output
1 Enter the Element a[0][0] : 1
1 Enter the number of Rows : 3
2 Enter the Element a[0][1] : 2 2 Enter the number of Columns : 3
3
3 Enter the Element a[0][2] : 3 4 Enter the Element a[0][0] : 1
5 Enter the Element a[0][1] : 2
4 Enter the Element a[1][0] : 2 6 Enter the Element a[0][2] : 3
7 Enter the Element a[1][0] : 2
8 Enter the Element a[1][1] : 1
5 Enter the Element a[1][1] : 1
9 Enter the Element a[1][2] : 1
10 Enter the Element a[2][0] : 1
6 Enter the Element a[1][2] : 1 11 Enter the Element a[2][1] : 2
12 Enter the Element a[2][2] : 1
7 Enter the Element a[2][0] : 1 13
14 Sum of Upper Triangle Elements : 6
8 Enter the Element a[2][1] : 2
#include<stdio.h>
to calculate sum of Upper Triangular Elements in C
int main() {
#include<stdio.h> int i, j, mat1[10][10], mat2[10][10], mat3[10][10];
#include<conio.h> int row1, col1, row2, col2;
//Print out the Resultant Matrix printf("\nEnter the number of Rows of Mat2 : ");
printf("\nThe Subtraction of two Matrices is : \n"); scanf("%d", &row2);
for (i = 0; i < row1; i++) { printf("\nEnter the number of Columns of Mat2 : ");
for (j = 0; j < col1; j++) { scanf("%d", &col2);
printf("%d\t", mat3[i][j]);
} /* Before accepting the Elements Check if no of
printf("\n"); rows and columns of both matrices is equal */
} if (row1 != row2 || col1 != col2) {
printf("\nOrder of two matrices is not same ");
return (0); exit(0);
} }
return (0);
}
Output : //Multiplication Logic
for (i = 0; i <= 2; i++) {
for (j = 0; j <= 2; j++) {
1 Enter the number of Rows : 2 sum = 0;
for (k = 0; k <= 2; k++) {
2 Enter the number of Columns : 2 sum = sum + a[i][k] * b[k][j];
}
3 c[i][j] = sum;
}
4 Enter the Element mat[0][0] : 1 }
#include<stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10], i, j, k;
int sum = 0;
#include<stdio.h>
Find Smallest Element in Array in C Programming
int main() {
smallest = a[i]; }
return (0); }
Output : }
2 11 55 33 77 22
Output :
3 Largest Element : 77
1 Enter no of elements : 5
C Program to Reversing an Array Elements in C
Programming 2 11 22 33 44 55
int main() {
} scanf("%d", &n1);
i++; // increment i i = 0;
j--; // decrement j j = 0;
} k = 0;
k++; Output :
j++;
1 Enter no of elements in 1st array : 4
}
2 11 22 33 44
} 3
7 Merged array is : 10 11 22 33 40 44 80
i++; #include<stdio.h>
res[k] = arr2[j]; }
} scanf("%d", &ele);
if (i < num) { }
printf("Number found at the location = %d", i + 1); //Printing of all elements of array
}
Output :
#include<stdio.h>
return (0);
1 Enter no of elements : 5
2 12345
5 162345
#include<stdio.h>
int main() {
int arr[30], num, i, loc;