Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Assignment1.c #26

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions Assignment1.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
//Write a C program that allows the user to enter 'N' numbers and find the total positive numbers and total negative numbers using a for loop.

int main()

#include <stdio.h>

int countPositiveNumbers(int* arr, int n)
{
//add your code here
return 0;
int pos_count = 0;
int i;
for (i = 0; i < n; i++) {
if (arr[i] > 0)
pos_count++;
}
return pos_count;
}
int countNegativeNumbers(int* arr, int n)
{
int neg_count = 0;
int i;
for (i = 0; i < n; i++) {
if (arr[i] < 0)
neg_count++;
}
return neg_count;
}
void printArray(int* arr, int n)
{
int i;

printf("Array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[] = { 2, -1, 5, 6, 0, -3 };
int n;
n = sizeof(arr) / sizeof(arr[0]);

printArray(arr, n);

printf("Count of Positive elements = %d\n",
countPositiveNumbers(arr, n));
printf("Count of Negative elements = %d\n",
countNegativeNumbers(arr, n));

return 0;
}
34 changes: 33 additions & 1 deletion Assignment10.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,44 @@
//if all elements in the array are even it is known as "EVEN ARRAY"
//if both even and odd available it is known as MIXED array


#include<stdio.h>
int findType(int n, int *) //complete this function
{

}
int main()
{
int findType(int *arr, int n) {
int oddCount = 0;
int evenCount = 0;

for(int i = 0; i < n; i++) {
if(arr[i] % 2 == 0) {
evenCount++;
}
else {
oddCount++;
}
}

if(evenCount == n) {
printf("EVEN ARRAY\n");
}
else if(oddCount == n) {
printf("ODD ARRAY\n");
}
else {
printf("MIXED ARRAY\n");
}
}

int main() {
int arr1[] = {1,3,5,11,9};
int arr2[] = {2,4,6,8,10};
int arr3[] = {1,2,3,4,5,6};

findType(arr1, 5);
findType(arr2, 5);
findType(arr3, 6);
return 0;
}
11 changes: 11 additions & 0 deletions Assignment11.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
int main()
{
int N = 7;
binary = 0, place = 1, remainder;

while (N> 0) {
remainder = N % 2;
binary += remainder * place;
place *= 10;
N /= 2;
}

printf("Binary equivalent: %d", binary);


return 0;
}
23 changes: 21 additions & 2 deletions Assignment12.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
//Input marks of 10 students in an array and then find the student with maximum marks

int main()
{
#include<stdio.h>
#define MAX_STUDENTS10

int main() {

int marks[MAX_STUDENTS];
int max_marks = 0;
int max_index = 0;

for (int i = 0; i < MAX_STUDENTS; i++) {
printf("Enter marks of student %d: ", i+1);
scanf("%d", &marks[i]);

if (marks[i] > max_marks) {
max_marks = marks[i];
max_index = i;
}
}

printf("The student with maximum marks is student %d with %d marks.\n",
max_index+1, max_marks);
return 0;
}
36 changes: 35 additions & 1 deletion Assignment2.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
//A number is said to be valid if it is divisible by 8. Write a program that allows the user to keep entering numbers as long as the input is valid and also displays a count of the valid numbers. Use a while loop to complete the task

int main()
{
{int n = str.Length;

// Empty string
if (n == 0)
return false;

// If there is single digit
if (n == 1)
return ((str[0] - '0') %8 == 0);

// If there is double digit
if (n == 2)
return (((str[n - 2] - '0') * 10 +
(str[n - 1] - '0')) % 8 == 0);

// If number formed by last three
// digits is divisible by 8
int last = str[n - 1] - '0';
int second_last = str[n - 2] - '0';
int third_last = str[n - 3] - '0';

return ((third_last * 100 + second_last
* 10 + last) % 8 == 0);
}

// Driver Code
public static void Main ()
{
String str = "76952";
if(check(str))
Console.Write("Yes");
else
Console.Write("No");
}
}

return 0;
}
27 changes: 25 additions & 2 deletions Assignment3.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
//Can you help your digital design faculty in correcting the assignment on number conversions by writing a program to convert a decimal number to its binary equivalent using a while loop.
//Example, lets say input is 7, output should be 111

int main()
{


int convert(long long);

int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}

// function definition
int convert(long long n) {
int dec = 0, i = 0, rem;

while (n!=0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}

return dec;
}
return 0;
}
21 changes: 20 additions & 1 deletion Assignment4.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

int main()
{
int mat[3][3], i, j, max;
printf("Enter any 3*3 matrix: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat[i][j]);
}
max = mat[0][0];
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
if(max<mat[i][j])
max = mat[i][j];
}
}
printf("\nLargest Element = %d", max);
getch();
return 0;


return 0;
}
33 changes: 33 additions & 0 deletions Assignment5.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,38 @@

int main()
{

static string studentRecord(String[, ] S, int N)
{

// code here
int maxi = Int32.MinValue;
String result = "";
for (int i = 0; i < N; i++) {
int avg = (Int32.Parse(S[i, 1])
+ Int32.Parse(S[i, 2])
+ Int32.Parse(S[i, 3]))
/ 3;
if (avg > maxi) {
maxi = avg;
result = S[i, 0];
}
else if (avg == maxi) {
result = result + " " + S[i, 0];
}
}
return result + " " + maxi;
}
public static void Main(string[] args)
{
int N = 2;
String[, ] file
= {"paras", "20", "30", "10" },
{ "Ram", "100", "50", "10" } };

Console.WriteLine(studentRecord(file, N));
}
}

return 0;
}
33 changes: 33 additions & 0 deletions Assignment6.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,38 @@

int main()
{
int countDistinct(int a[], int n)
{
int i, j, count = 1;

for (i = 1; i < n; i++)
{
for (j = 0; j < i; j++)
{
if (a[i] == a[j])
{
break;
}
}
if (i == j)
{
count++;
}
}
return count;
}
int main()
{
int n;
printf("Enter the number of elements \n");
scanf("%d",&n);

int a[n];
printf("Enter the array elements : ");
for (int i=0;i<n;i++)
scanf("%d",&a[i]);

int c= countDistinct(a,n);
printf("The number of distinct elements are %d",c);
return 0;
}
24 changes: 23 additions & 1 deletion Assignment7.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
//Write a code find the length of a string and its reverse equivalent without using readymade c functions

#include<stdio.h>
int main()
{
char str[100];
int len = 0;

printf("Enter a string: ");
scanf("%s", str);

// Find length of string
while (str[len] != '\0') {
len++;
}

printf("Length of string: %d\n", len);

// Reverse string
char rev_str[100];
int i, j;
for (i = len - 1, j = 0; i >= 0; i--, j++) {
rev_str[j] = str[i];
}
rev_str[j] = '\0';

printf("Reverse equivalent of string: %s\n", rev_str);
return 0;
}
25 changes: 24 additions & 1 deletion Assignment8.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
//Write a C code to sort a string in alphabetical order
//input: Anitha
//output: Aahint

#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("Enter a string: ");
scanf("%s", str);
selectionSort(str, strlen(str));
printf("Sorted string: %s", str);
return 0;
}

void selectionSort(char str[], int n) {
int i, j, min_idx;
char temp;
for (i = 0; i < n-1; i++) {
min_idx = i;
for (j = i+1; j < n; j++) {
if (str[j] < str[min_idx]) {
min_idx = j;
}
}
temp = str[i];
str[i] = str[min_idx];
str[min_idx] = temp;
}
return 0;
}
Loading