0% found this document useful (0 votes)
8 views

C and Java Programming Concepts

Lectures

Uploaded by

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

C and Java Programming Concepts

Lectures

Uploaded by

bruce chileshe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

C Programming: Introduction

• C is a structured, procedural language.


• Widely used for system software, embedded
systems, and application development.
#include <stdio.h>
• A C program consists
int main() { of functions, libraries,
and syntax rules.printf("Hello,
return 0;
World!\n");

}
C: Variables, Data Types, and
Constants
• Variables store information like numbers or
text.
• Constants are fixed values (e.g., Pi = 3.14).
#define PI 3.14159
• Data types include int, float, char, etc.
#include <stdio.h>
int main() {
float radius, area;
printf("Enter radius: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("Area = %.2f\n", area);
return 0;
}
C: Control Structures
• Control structures allow decision-making and
repetition.
• Examples include if-else, switch, for loops,
while loops, etc.
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18)
printf("You are eligible to vote.\n");
else
printf("You are not eligible to vote.\n");
return 0;
}
C: Functions
• Functions break the program into smaller,
reusable pieces.
• Example: A function to convert Celsius to
Fahrenheit.
#include <stdio.h>
float convert(float c) {
return (c * 9/5) + 32;
}
int main() {
float celsius;
printf("Enter Celsius: ");
scanf("%f", &celsius);
printf("Fahrenheit: %.2f\n", convert(celsius));
return 0;
}
Java Programming: Introduction
• Java is a high-level, object-oriented language
designed for portability and performance.
• Popular for web applications, mobile apps,
and enterprise systems.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Java: Variables, Data Types, and
Constants
• Java supports primitive data types (int, float,
char, etc.) and reference types (objects).
• Constants can be defined using the 'final'
keyword. public class CircleArea {
public static final double PI = 3.14159;
public static void main(String[] args) {
double radius = 5.0;
double area = PI * radius * radius;
System.out.println("Area = " + area);
}
}
Java: Control Structures
• Control structures allow conditional execution
and loops.
• Examples: if-else, switch, for loop, while loop.
public class VotingEligibility {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("Eligible to vote.");
} else {
System.out.println("Not eligible to vote.");
}
}
}
Java: Methods and Classes
• Methods are reusable blocks of code defined
in classes.
• Example: A Celsius to Fahrenheit converter
using apublic
method.
class TemperatureConverter {
public static double convert(double celsius) {
return (celsius * 9/5) + 32;
}
public static void main(String[] args) {
double celsius = 25.0;
System.out.println("Fahrenheit: " + convert(celsius));
}
}

You might also like