Practical Record Book - Cprog
Practical Record Book - Cprog
BY
KATABALWA JOHN VIANNEY
012220073
BSc.AIT
This is to certify that I have completed the C Programming Practical Record Book for
4. Write a c program which prompts a user for First name, Last name, Age,
Salary (in $) and output 5 records on the console.
//C Program to check whether a number is prime or not//
# include <stdio.h>
int main () {
int num, i, flag = 0;
printf ("Enter any number num:");
scanf ("%d", &num);
if (flag == 2) {
printf ("num is a Prime number");
}
else {
printf ("num is not a Prime number");
}
return 0;
}
//C program for a 2D array of size 3x4 of salaries and print the matrix and sum//
//Assuming the salaries are fixed point integers//
# include <stdio.h>
int main () {
int matrix[3][4] = {{20,30,40,10},{10,20,30,40},{30,10,20,50}};
int i, j, sum= 0;
printf ("\n Elements in the Matrix are: \n");
for (i = 0 ; i < 3 ; i++) {
for (j = 0; j < 4; j++) {
printf ("\t %d ", matrix[i][j]);
}
printf (" \n ") ;
}
for (i = 0 ; i < 3 ; i++) {
for (j = 0; j < 4; j++) {
sum = sum + matrix[i][j];
}
}
printf ("\t Sum of matrix elements is: %d", sum) ;
return 0 ;
}
//c program to execute the flow chart for the maximum of three variables//
# include <stdio.h>
int main() {
int a, b, c;
printf ("\n Enter the First value:");
scanf ("%d", & a);
printf ("\n Enter the Second value:");
scanf("%d", & b);
printf("\n Enter the Third value:");
scanf("%d", & c);
if (a > b) {
if (a > c) {
printf ("\n First value %d is maximum", a);
}
else {
printf ("\n Third value %d is maximum", c);
}
}
else {
if (b > c) {
printf ("\n Second value %d is maximum", b);
}
else {
printf ("\n Third value %d is maximum", c);
}
}
return 0;
}
//c program which prompts a user for First name, Last name, Age, Salary (in $)
and output 5 records on the console//
#include <stdio.h>
struct payroll {
char name1[20];
char name2[20];
int Age;
float Salary;
};
int main () {
struct payroll record[5];
printf ("\nEnter the payroll data");
for (int i = 0; i<5; i++) {
printf ("\n Enter the First name:");
scanf ("%s",&record[i].name1);
printf ("\n Enter the Last name:");
scanf ("%s",&record[i].name2);
printf("\n Enter Age");
scanf("%d",&record[i].Age);
printf("\n Enter Salary");
scanf("%f",&record[i].Salary);
}
printf ("\nDisplay for the payroll data");
for (int i = 0; i<5; i++) {
printf ("\n %s \t %s\t%d\t%f", record[i].name1, record[i].name2, record[i].Age,
record[i].Salary);
}
return 0;
}