0% found this document useful (0 votes)
3 views17 pages

C Java Lab Programs-7

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

C Java Lab Programs-7

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C and Core Java

Lab Programs
RGPV University | Lab Assignment

Index
[Link]. Program Title Language Sign
1 Find Cube of a Number C
2 Area of a Circle C
3 Factorial of a Number C
4 Average of N Numbers C
5 Leap Year Check C
6 Even and Odd Number C
7 Swapping of Two Numbers (Using Third Variable) C
8 Swapping Without Third Variable C
9 Prime Number Check C
10 Public Access Modifier Java
11 Private Access Modifier Java
12 Protected Access Modifier Java
13 Input Using Scanner Class Java
14 Local Variable in Java Java
15 Instance Variable in Java Java
16 Static Variable in Java Java

C & Core Java Lab Programs | Page


Part A — C Programs

Program 1: Find Cube of a Number


Aim:
Write a program in C to find the cube of a number.
Code:
#include <stdio.h>

int main() {
int n, cube;
printf("Enter a number: ");
scanf("%d", &n);
cube = n * n * n;
printf("Cube of %d = %d\n", n, cube);
return 0;
}

Output:
Enter a number: 4
Cube of 4 = 64

C & Core Java Lab Programs | Page


Program 2: Area of a Circle
Aim:
Write a program in C to calculate the area of a circle.
Code:
#include <stdio.h>
#define PI 3.14159

int main() {
float r, area;
printf("Enter radius: ");
scanf("%f", &r);
area = PI * r * r;
printf("Area of circle = %.2f\n", area);
return 0;
}

Output:
Enter radius: 5
Area of circle = 78.54

C & Core Java Lab Programs | Page


Program 3: Factorial of a Number
Aim:
Write a program in C to find the factorial of a number.
Code:
#include <stdio.h>

int main() {
int n, i;
long long fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fact *= i;
printf("Factorial of %d = %lld\n", n, fact);
return 0;
}

Output:
Enter a number: 6
Factorial of 6 = 720

C & Core Java Lab Programs | Page


Program 4: Average of N Numbers
Aim:
Write a program in C to calculate the average of N numbers.
Code:
#include <stdio.h>

int main() {
int n, i;
float sum = 0, avg, num;
printf("Enter how many numbers: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter number %d: ", i + 1);
scanf("%f", &num);
sum += num;
}
avg = sum / n;
printf("Average = %.2f\n", avg);
return 0;
}

Output:
Enter how many numbers: 3
Enter number 1: 10
Enter number 2: 20
Enter number 3: 30
Average = 20.00

C & Core Java Lab Programs | Page


Program 5: Leap Year Check
Aim:
Write a program in C to check whether a year is a leap year or not.
Code:
#include <stdio.h>

int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
printf("%d is a Leap Year.\n", year);
else
printf("%d is NOT a Leap Year.\n", year);
return 0;
}

Output:
Enter a year: 2024
2024 is a Leap Year.

C & Core Java Lab Programs | Page


Program 6: Even and Odd Number
Aim:
Write a program in C to check whether a number is even or odd.
Code:
#include <stdio.h>

int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 == 0)
printf("%d is Even.\n", n);
else
printf("%d is Odd.\n", n);
return 0;
}

Output:
Enter a number: 7
7 is Odd.

C & Core Java Lab Programs | Page


Program 7: Swapping of Two Numbers (Using Third Variable)
Aim:
Write a program in C to swap two numbers using a third variable.
Code:
#include <stdio.h>

int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After Swap: a = %d, b = %d\n", a, b);
return 0;
}

Output:
Enter two numbers: 10 20
After Swap: a = 20, b = 10

C & Core Java Lab Programs | Page


Program 8: Swapping Without Third Variable
Aim:
Write a program in C to swap two numbers without using a third variable.
Code:
#include <stdio.h>

int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("After Swap: a = %d, b = %d\n", a, b);
return 0;
}

Output:
Enter two numbers: 5 15
After Swap: a = 15, b = 5

C & Core Java Lab Programs | Page


Program 9: Prime Number Check
Aim:
Write a program in C to check whether a number is prime or not.
Code:
#include <stdio.h>

int main() {
int n, i, flag = 1;
printf("Enter a number: ");
scanf("%d", &n);
if (n <= 1) flag = 0;
for (i = 2; i * i <= n; i++) {
if (n % i == 0) { flag = 0; break; }
}
if (flag)
printf("%d is a Prime Number.\n", n);
else
printf("%d is NOT a Prime Number.\n", n);
return 0;
}

Output:
Enter a number: 13
13 is a Prime Number.

C & Core Java Lab Programs | Page


Part B — Core Java Programs

Program 10: Public Access Modifier


Aim:
Write a Java program to demonstrate the public access modifier.
Code:
public class PublicDemo {
public int num = 100;
public void display() {
[Link]("Public variable: " + num);
}
public static void main(String[] args) {
PublicDemo obj = new PublicDemo();
[Link]();
}
}

Output:
Public variable: 100

C & Core Java Lab Programs | Page


Program 11: Private Access Modifier
Aim:
Write a Java program to demonstrate the private access modifier.
Code:
public class PrivateDemo {
private int num = 200;
private void display() {
[Link]("Private variable: " + num);
}
public static void main(String[] args) {
PrivateDemo obj = new PrivateDemo();
[Link](); // accessible within same class
}
}

Output:
Private variable: 200

C & Core Java Lab Programs | Page


Program 12: Protected Access Modifier
Aim:
Write a Java program to demonstrate the protected access modifier.
Code:
class Parent {
protected int num = 300;
protected void display() {
[Link]("Protected variable: " + num);
}
}

public class ProtectedDemo extends Parent {


public static void main(String[] args) {
ProtectedDemo obj = new ProtectedDemo();
[Link]();
}
}

Output:
Protected variable: 300

C & Core Java Lab Programs | Page


Program 13: Input Using Scanner Class
Aim:
Write a Java program to take input from user using Scanner class.
Code:
import [Link];

public class ScannerDemo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Enter your age: ");
int age = [Link]();
[Link]("Name: " + name + ", Age: " + age);
[Link]();
}
}

Output:
Enter your name: Sai
Enter your age: 20
Name: Sai, Age: 20

C & Core Java Lab Programs | Page


Program 14: Local Variable in Java
Aim:
Write a Java program to demonstrate a local variable.
Code:
public class LocalVar {
public static void main(String[] args) {
int localNum = 50; // local variable
[Link]("Local Variable: " + localNum);
}
}

Output:
Local Variable: 50

C & Core Java Lab Programs | Page


Program 15: Instance Variable in Java
Aim:
Write a Java program to demonstrate an instance variable.
Code:
public class InstanceVar {
int instanceNum = 75; // instance variable

public static void main(String[] args) {


InstanceVar obj = new InstanceVar();
[Link]("Instance Variable: " + [Link]);
}
}

Output:
Instance Variable: 75

C & Core Java Lab Programs | Page


Program 16: Static Variable in Java
Aim:
Write a Java program to demonstrate a static variable.
Code:
public class StaticVar {
static int count = 0; // static variable

StaticVar() {
count++;
}

public static void main(String[] args) {


new StaticVar();
new StaticVar();
new StaticVar();
[Link]("Static Variable count = " + [Link]);
}
}

Output:
Static Variable count = 3

C & Core Java Lab Programs | Page

You might also like