1)
2)#include <stdio.h>
#include <math.h>
void main()
{
int a,b,c,d;
float x1,x2;
printf("Input the value of a,b & c : ");
scanf("%d%d%d",&a,&b,&c);
d=b*b-4*a*c;
if(d==0)
{
printf("Both roots are equal.\n");
x1=-b/(2.0*a);
x2=x1;
printf("First Root Root1= %f\n",x1);
printf("Second Root Root2= %f\n",x2);
}
else if(d>0)
{
printf("Both roots are real and diff-2\n");
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("First Root Root1= %f\n",x1);
printf("Second Root root2= %f\n",x2);
}
else
printf("Root are imeainary;\nNo Solution. \n");
}
Sample Output:
Input the value of a,b & c : 1 5 7
Root are imaginary;
No Solution.
#include <stdio.h>
#include <string.h>
void extractDigits(unsigned int num)
{
int i,len;
//char pointer declaration
char temp[5];
//assig number as string in char pointer
sprintf((char*)temp,"%u",num);
//extract and print each digits
len= strlen(temp);
for(i=0; i<len; i++)
printf("%c",temp[i]);
printf("\n");
}
//main program
int main()
{
unsigned int number=12345;
//function calling
extractDigits(number);
return 0;
}
Output
12345
#include <stdio.h>
int main() {
int n, reverse = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", reverse);
return 0;
}
Output
Enter an integer: 2345
Reversed number = 5432
#include<stdio.h>
int main()
{
int n,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
return 0;
}
Output:
Enter a number:654
Sum is=15
Enter a number:123
Sum is=6
#include <stdio.h>
int main() {
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i) {
printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}
Output
Enter an integer: 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
Here, the use
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}
Output:
enter the number=153
armstrong number
enter the number=5
not armstrong number
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// 0 and 1 are not prime numbers
// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i) {
// if n is divisible by i, then n is not prime
// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}
// flag is 0 for prime numbers
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
Output
Enter a positive integer: 29
29 is a prime number.
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer variables
int n, temp, rev = 0, digit, sum_of_digits = 0;
printf (" Enter a Number: \n");
scanf (" %d", &n); // get the number
temp = n; // assign the number to temp variable
// use while loop to calculate the sum of digits
while ( temp > 0)
{
// extract digit one by one and store into the sum_of_digits
sum_of_digits = sum_of_digits + temp % 10; /* use modulus symbol to get the remainder of each iteration b
y temp % 10 */
temp = temp / 10;
}
temp = sum_of_digits; // assign the sum_of_digits to temp variable
printf (" \n The sum of the digits = %d", temp);
// get the reverse sum of given digits
while ( temp > 0)
{
rev = rev * 10 + temp % 10;
temp = temp / 10;
}
printf (" \n The reverse of the digits = %d", rev);
printf (" \n The product of %d * %d = %d", sum_of_digits, rev, rev * sum_of_digits);
// use if else statement to check the magic number
if ( rev * sum_of_digits == n)
{
printf (" \n %d is a Magic Number. ", n);
}
else
{
printf (" \n %d is not a Magic Number. ", n);
}
return 0;
3)
// CPP code for implementing sin function
#include <iostream>
#include <math.h>
using namespace std;
// Function for calculating sin value
void cal_sin(float n)
{
float accuracy = 0.0001, denominator, sinx, sinval;
// Converting degrees to radian
n = n * (3.142 / 180.0);
float x1 = n;
// maps the sum along the series
sinx = n;
// holds the actual value of sin(n)
sinval = sin(n);
int i = 1;
do
{
denominator = 2 * i * (2 * i + 1);
x1 = -x1 * n * n / denominator;
sinx = sinx + x1;
i = i + 1;
} while (accuracy <= fabs(sinval - sinx));
cout << sinx;
}
// Main function
int main()
{
float n = 90;
cal_sin(n);
return 0;
}
Output:
4) #include<stdio.h> // include stdio.h library
void convert_to_x_base(int, int);
int main(void)
{
int num, choice, base;
while(1)
{
printf("Select conversion: \n\n");
printf("1. Decimal to binary. \n");
printf("2. Decimal to octal. \n");
printf("3. Decimal to hexadecimal. \n");
printf("4. Exit. \n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
base = 2;
break;
case 2:
base = 8;
break;
case 3:
base = 16;
break;
case 4:
printf("Exiting ...");
exit(1);
default:
printf("Invalid choice.\n\n");
continue;
}
printf("Enter a number: ");
scanf("%d", &num);
printf("Result = ");
convert_to_x_base(num, base);
printf("\n\n");
}
return 0; // return 0 to operating system
}
void convert_to_x_base(int num, int base)
{
int rem;
// base condition
if (num == 0)
{
return;
}
else
{
rem = num % base; // get the rightmost digit
convert_to_x_base(num/base, base); // recursive call
if(base == 16 && rem >= 10)
{
printf("%c", rem+55);
}
else
{
printf("%d", rem);
}
}
}
Select conversion:
1. Decimal to binary.
2. Decimal to octal.
3. Decimal to hexadecimal.
4. Exit.
Enter your choice: 1
Enter a number: 7
Result = 111
Select conversion:
1. Decimal to binary.
2. Decimal to octal.
3. Decimal to hexadecimal.
4. Exit.
Enter your choice: 2
Enter a number: 25
Result = 31
Select conversion:
1. Decimal to binary.
2. Decimal to octal.
3. Decimal to hexadecimal.
4. Exit.
Enter your choice: 4
Exiting ...
5)
#include < stdio.h >
long factorial(int);
int main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d", & n);
for (i = 0; i < n; i++) {
for (c = 0; c <= (n - i - 2); c++) printf(" ");
for (c = 0; c <= i; c++) printf("%ld ", factorial(i) / (factorial(c) * factorial(i - c)));
printf("\n");
}
return 0;
}
long factorial(int n) {
int c;
long result = 1;
for (c = 1; c <= n; c++) result = result * c;
return result;
}
6)GCD
#include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}
int hcf(int n1, int n2) {
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}
FIBINOCI SERIES
#include<stdio.h>
int main()
{
int first=0, second=1, i, n, sum=0;
printf("Enter the number of terms: ");
scanf("%d",&n);
//accepting the terms
printf("Fibonacci Series:");
for(i=0 ; i<n ; i++)
{
if(i <= 1)
{
sum=i;
}
//to print 0 and 1
else
{
sum=first + second;
first=second;
second=sum;
//to calculate the remaining terms.
//value of first and second changes as new term is printed.
}
printf(" %d",sum)
}
return 0;
}
7)
So I'm coding in C, and I need to come up with code that will take n numbers from the user, and find
their minimum, maximum, average, and sum of squares for for their values. So far I have the average and
sum of squares portion, but the minimum and maximum is biting me.
Keep in mind I'm at a very rudimentary level, and I have not reached arrays yet. All I know are logical
operators, functions, loops, and the use of the stdlib.h, math.h, and stdio.h libraries.
This is what I have so far. The average function gave me a lot of problems when I tried to put float and
double during compiling, so multiply it by a 1.0 fixed that. I have everything, just the minimum and
maximum. I keep getting the last entry as my maximum, and a 0 for my minimum.
#include<stdio.h>
int main()
{
float average;
int i, n, count=0, sum=0, squaresum=0, num, min, max;
printf("Please enter the number of numbers you wish to evaluate\n");
scanf_s("%d",&n);
printf("Please enter %d numbers\n",n);
while(count<n)
{
min=0;
max=0;
if(num>max)
max=num;
if(num<min)
min=num;
scanf_s("%d",&num);
sum = sum+num;
squaresum = squaresum + (num*num);
count++;
}
average = 1.0*sum/n;
printf("Your average is %.2f\n",average);
printf("The sum of your squares is %d\n",squaresum);
printf("Your maximum number is %d\n",max);
printf("Your minimum number is %d\n",min);
return(0);
}
8)
// Iterative C program to reverse an array
#include<stdio.h>
/* Function to reverse arr[] from start to end*/
void rvereseArray(int arr[], int start, int end)
{
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
} /* Utility that prints out an array on a line */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
/* Driver function to test above functions */
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
printArray(arr, n);
rvereseArray(arr, 0, n-1);
printf("Reversed array is \n");
printArray(arr, n);
return 0;
}
Output :
1 2 3 4 5 6
Reversed array is
6 5 4 3 2 1
REMOVE DUPLICATE
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare local variables
int arr[20], i, j, k, size;
printf (" Define the number of elements in an array: ");
scanf (" %d", &size);
printf (" \n Enter %d elements of an array: \n ", size);
// use for loop to enter the elements one by one in an array
for ( i = 0; i < size; i++)
{
scanf (" %d", &arr[i]);
}
// use nested for loop to find the duplicate elements in array
for ( i = 0; i < size; i ++)
{
for ( j = i + 1; j < size; j++)
{
// use if statement to check duplicate element
if ( arr[i] == arr[j])
{
// delete the current position of the duplicate element
for ( k = j; k < size - 1; k++)
{
arr[k] = arr [k + 1];
}
// decrease the size of array after removing duplicate element
size--;
// if the position of the elements is changes, don't increase the index j
j--;
}
}
}
/* display an array after deletion or removing of the duplicate elements */
printf (" \n Array elements after deletion of the duplicate elements: ");
// for loop to print the array
for ( i = 0; i < size; i++)
{
printf (" %d \t", arr[i]);
}
return 0;
}
OUT PUT
Define the number of elements in an array: 10
Enter 10 elements of an array:
57
12
89
32
62
12
89
35
67
75
Array elements after deletion of the duplicate elements: 57 12 89 32
62 35 67 75
9)
#include<stdio.h>
#include<stdlib.h>
// function to add two 3x3 matrix
void add(int m[3][3], int n[3][3], int sum[3][3])
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
sum[i][j] = m[i][j] + n[i][j];
}
// function to subtract two 3x3 matrix
void subtract(int m[3][3], int n[3][3], int result[3][3])
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
result[i][j] = m[i][j] - n[i][j];
}
// function to multiply two 3x3 matrix
void multiply(int m[3][3], int n[3][3], int result[3][3])
{
for(int i=0; i < 3; i++)
{
for(int j=0; j < 3; j++)
{
result[i][j] = 0; // assign 0
// find product
for (int k = 0; k < 3; k++)
result[i][j] += m[i][k] * n[k][j];
}
}
}
// function to find transpose of a 3x3 matrix
void transpose(int matrix[3][3], int trans[3][3])
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
trans[i][j] = matrix[j][i];
}
// function to display 3x3 matrix
void display(int matrix[3][3])
{
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
printf("%d\t",matrix[i][j]);
printf("\n"); // new line
}
}
// main function
int main()
{
// matrix
int a[][3] = { {5,6,7}, {8,9,10}, {3,1,2} };
int b[][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
int c[3][3];
// print both matrix
printf("First Matrix:\n");
display(a);
printf("Second Matrix:\n");
display(b);
// variable to take choice
int choice;
// menu-driven
do
{
// menu to choose the operation
printf("\nChoose the matrix operation,\n");
printf("----------------------------\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Transpose\n");
printf("5. Exit\n");
printf("----------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add(a, b, c);
printf("Sum of matrix: \n");
display(c);
break;
case 2:
subtract(a, b, c);
printf("Subtraction of matrix: \n");
display(c);
break;
case 3:
multiply(a, b, c);
printf("Multiplication of matrix: \n");
display(c);
break;
case 4:
printf("Transpose of the first matrix: \n");
transpose(a, c);
display(c);
printf("Transpose of the second matrix: \n");
transpose(b, c);
display(c);
break;
case 5:
printf("Thank You.\n");
exit(0);
default:
printf("Invalid input.\n");
printf("Please enter the correct input.\n");
}
}while(1);
return 0;
}
Output:-
First Matrix:
567
8 9 10
312
Second Matrix:
123
456
789
Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 1
Sum of matrix:
6 8 10
12 14 16
10 9 11
Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 2
Subtraction of matrix:
444
444
-4 -7 -7
Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 3
Multiplication of matrix:
78 96 114
114 141 168
21 27 33
Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 4
Transpose of the first matrix:
583
691
7 10 2
Transpose of the second matrix:
147
258
369
Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 6
Invalid input.
Please enter the correct input.
Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 5
Thank You.
10)
STRING HANDLING FUNCTIONS
WRITE FROM NOTES
11) STRING WITH OUT USING STRING FUNCTION
#include<stdio.h>
#include<string.h>
void concat(char[], char[]);
int main() {
char s1[50], s2[30];
printf("\nEnter String 1 :");
gets(s1);
printf("\nEnter String 2 :");
gets(s2);
concat(s1, s2);
printf("\nConcated string is :%s", s1);
return (0);
}
void concat(char s1[], char s2[]) {
int i, j;
i = strlen(s1);
for (j = 0; s2[j] != '\0'; i++, j++) {
s1[i] = s2[j];
}
s1[i] = '\0';
}
Output
The above code sample will produce the following result.
1. Enter String 1 : Ankit
2. Enter String 2 : Singh
3. Concated string is : AnkitSingh
12)#include <stdio.h>
int main()
{
char str[100];//input string with size 100
int words=0,newline=0,characters=0; // counter variables
scanf("%[^~]",&str);//scanf formatting
for(int i=0;str[i]!='\0';i++)
{
if(str[i] == ' ')
{
words++;
}
else if(str[i] == '\n')
{
newline++;
words++;//since with every next line new words start. corner case 1
}
else if(str[i] != ' ' && str[i] != '\n'){
characters++;
}
}
if(characters > 0)//Corner case 2,3.
{
words++;
newline++;
}
printf("Total number of words : %d\n",words);
printf("Total number of lines : %d\n",newline);
printf("Total number of characters : %d\n",characters);
return 0;
}
Input : HELLO STUDENTS
HOW R U
Output : Total number of words : 5
Total number of lines : 2
Total number of characters : 18
13)
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr;
char filename[100], c;
printf("Enter the filename to open \n");
scanf("%s", filename);
// Open file
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
// Read contents from file
c = fgetc(fptr);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fptr);
}
fclose(fptr);
return 0;
}
OUT PUT
Enter the filename to open
a.txt
/*Contents of a.txt*/