Skip to content
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
15 changes: 8 additions & 7 deletions c/happydiwali.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include<stdio.h>
void main()
{
while(1)
{
printf("Happy diwali \n");
}
#include <stdio.h>
int main() {
// Infinite loop to print "Happy Diwali" repeatedly
while (1) {
printf("Happy Diwali\n");
}

return 0; // although unreachable, good practice
}
39 changes: 17 additions & 22 deletions c/isprime.c
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
#include <stdio.h>

int main() {
int n, i, flag = 0;

printf("Enter a positive integer: ");
scanf("%d", &n);

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 <= 1) {
flag = 1; // 0 and 1 are not prime
}

// 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;
for (i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
flag = 1; // divisible => not prime
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);
if (flag == 0)
printf("%d is a prime number.\n", n);
else
printf("%d is not a prime number.\n", n);

return 0;
return 0;
}
52 changes: 26 additions & 26 deletions c/twosum.c
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@


#include <stdio.h>

int chkPair(int A[], int size, int x)
{
for (int i = 0; i < (size - 1); i++) {
for (int j = (i + 1); j < size; j++) {
if (A[i] + A[j] == x) {
return 1;
}
}
}

return 0;
// Function to check if a pair exists with sum equal to x
int chkPair(int A[], int size, int x) {
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (A[i] + A[j] == x) {
return 1; // Pair found
}
}
}
return 0; // Pair not found
}

int main(void)
{
int A[] = { 0, -1, 2, -3, 1 };
int x = -2;
int size = sizeof(A) / sizeof(A[0]);
int main(void) {
int A[] = {0, -1, 2, -3, 1};
int x = -2;
int size = sizeof(A) / sizeof(A[0]);

if (chkPair(A, size, x)) {
printf("Yes\n");
}
else {
printf("No\n");
}
printf("Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", A[i]);
}
printf("\nTarget sum: %d\n", x);

return 0;
}
if (chkPair(A, size, x)) {
printf("Yes, a pair with the given sum exists.\n");
} else {
printf("No, no pair with the given sum exists.\n");
}

return 0;
}
32 changes: 23 additions & 9 deletions java/SumOfNumbers1.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
public class SumOfNumbers1
{
public static void main(String args[])
{
int n1 = 225, n2 = 115, sum;
sum = n1 + n2;
System.out.println("The sum of numbers is: "+sum);
}
}
import java.util.Scanner;

public class SumOfNumbers {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Input two numbers from user
System.out.print("Enter the first number: ");
int n1 = sc.nextInt();

System.out.print("Enter the second number: ");
int n2 = sc.nextInt();

// Calculate sum
int sum = n1 + n2;

// Display result
System.out.println("The sum of the numbers " + n1 + " and " + n2 + " is: " + sum);

sc.close();
}
}
2 changes: 2 additions & 0 deletions java/newsAPI/NewsApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ public class NewsApplication {

public static void main(String[] args) {
SpringApplication.run(NewsApplication.class, args);
System.out.println("NewsApplication is running..."); // optional startup message
}

// Bean for RestTemplate to make HTTP requests
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
Expand Down
78 changes: 47 additions & 31 deletions java/twosum.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
import java.io.*;
class GFG {

static boolean chkPair(int A[], int size, int x)
{
for (int i = 0; i < (size - 1); i++) {
for (int j = (i + 1); j < size; j++) {
if (A[i] + A[j] == x) {
return true;
}
}
}

return false;
}

public static void main(String[] args)
{

int A[] = { 0, -1, 2, -3, 1 };
int x = -2;
int size = A.length;

if (chkPair(A, size, x)) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
import java.util.Scanner;
import java.util.Arrays;

public class CheckPairSum {

// Function to check if a pair exists with sum equal to x
static boolean chkPair(int[] A, int size, int x) {
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (A[i] + A[j] == x) {
return true; // Pair found
}
}
}
return false; // Pair not found
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// User input for array size
System.out.print("Enter the number of elements: ");
int size = sc.nextInt();
int[] A = new int[size];

// Input array elements
System.out.println("Enter " + size + " elements:");
for (int i = 0; i < size; i++) {
A[i] = sc.nextInt();
}

// Input target sum
System.out.print("Enter target sum: ");
int x = sc.nextInt();

System.out.println("Array: " + Arrays.toString(A));
System.out.println("Target sum: " + x);

// Check if pair exists
if (chkPair(A, size, x)) {
System.out.println("Yes, a pair with the given sum exists.");
} else {
System.out.println("No, no pair with the given sum exists.");
}

sc.close();
}
}
85 changes: 45 additions & 40 deletions sparse_matrix.c
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
#include<stdio.h>
int main()
{
int m,n,c,d,a[10][10],count=0;
printf(" enter the number of rows and columns:\n");
scanf("%d%d",&m,&n);
printf("enter the element the matrix:\n");
for(c=0;c<m;c++)
{
for(d=0;d<n;d++)
{
scanf("%d",&a[c][d]);
}
}
printf("elements are:\n");
for(c=0;c<m;c++)
{
for(d=0;d<n;d++)
{
printf("%d\t",a[c][d]);
}
printf("\n");
}
for(c=0;c<m;c++)
{
for(d=0;d<n;d++)
{
if(a[c][d]==0)
count++;
}
}
if(count>((m*n)/2))
{
printf("sparse matrix");
}
else
{
printf("not a sparse matrix");
}
return 0;
#include <stdio.h>

int main() {
int rows, cols, i, j, count = 0;
int matrix[10][10];

// Input matrix dimensions
printf("Enter the number of rows and columns: ");
scanf("%d%d", &rows, &cols);

// Input matrix elements
printf("Enter the elements of the matrix:\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("Element at [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}

// Display the matrix
printf("\nThe matrix is:\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}

// Count zero elements
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
if(matrix[i][j] == 0) {
count++;
}
}
}

// Check if the matrix is sparse
if(count > (rows * cols) / 2) {
printf("\nThe matrix is a sparse matrix.\n");
} else {
printf("\nThe matrix is not a sparse matrix.\n");
}

return 0;
}