1.
Write Program to Add two monetary amounts using structures
#include <stdio.h>
struct weight{
int grams;
int kg;
}w1, w2, hanum;
int main() {
scanf("%d", &w1.kg);
scanf("%d", &w1.gram);
scanf("%d", &w2.kg);
scanf("%d", &w2.gram);
if (wl-grams > w2.grams && w1.kg>w2.kg) {
hanum.grams = wl.grams - w2.grams;
hanum.kg = w1.kg - w2.kg;
else {
hanum.grams = w2.grams - w1.grams;
hanum.kg - w2.kg - w1.kg;
}
printf ("%d grams \n %d kg", hanum.grams, hanum.kg);
return 0;
};
-----------------------------------------------------------------------------------
-
2. Write program to Calculate Difference Between Two Weigth using Structures.
//struct weigth(
int gram;
int kilograms;
);//
#include <stdio.h>
struct weight{
int grams;
int kilograms;
}w1, w2, sum;
int (main){
printf ("For the 1st weight\n");
printf("Enter kilograms");
scanf("%d" , &w1.kilograms);
printf("Enter grams");
scanf ("%d" , &w1.grams);
printf("For the 2nd weight\n");
printf("Enter kilograms");
scanf("%d" , &w2.kilograms);
printf ("Enter grams");
scanf("%d" , &w2.grams);
sum.grams = w1.grams + w2.grams;
sum.kiligrams = w1.kilograms + w2.kilograms;
while(sum.grams>1000){
sum.kilograms++;
sum.grams -= 1000;
}
printf ("%d kg %d g", sum.kilograms, sum.grams);
return 0;
}
----------------------
tarberak 2
#include <stdio.h>
#include <stdlib.h>
struct weight{
int grams;
int kilograms;
}w1, w2, difference;
int main(){
printf("Enter the kilograms ");
scanf("%d"‚&w1.kilograms);
printf("Enter thr grams ");
scanf("%d",&wl.grams);
printf("Enter the kilograms");
scanf ("%d" ‚&w2.kilograms);
printf("Enter the grams");
scanf ("%d" ‚ &2.grams);
difference.grams=w1. grams-w2.grams;
difference.kilograms=w1.kilograms-w2.kilograms;
while(difference.grams<0){
difference.kilograms=difference.kilograms-1;
difference.grams=1000-(w2.grams-wl.grams);
}
printf("the answer is %d and %d" ,difference.kilograms, difference.grams);
return 0;
----------------------------------------------------------------------
3. The algorithm for calculating the function F(n).where n is a natural nuber is
F(n<=1)=1
F(n) = F(n-1)*(n+8) n>1
#include <stdio.h>
#include <stdlib.h>
int F(int n);
int main(){
int n;
printf("Enter n: ");
scanf("%d", &n);
printf("%d" , F(n) );
return 0;
}
int F(int n){
if (n<=1){
return 1;
}
else{
return F(n-1)*(n+8);
}
}
-----------------------------------------------------------------------------------
---
4. Write a program to write an ID array to a file.
#include <stdlib.h>
#include <stdio.h>
int main() {
int arr[8]= {1, 2, 3, 4, 5, 6, 7};
FILE*fptr;
fptr = fopen("array.txt", "w")
int i;
for(i=0; i<7; i++){
fprintf(fptr, "%d," ,arr[i]);
}
fclose(fptr);
return 0;
}
-----------------------------------------------------------------------------------
---
5.
#include <stdlib.h>
#include <stdio.h>
int main() {
int n;
printf("Enter n:");
scanf("%d", &n);
int i=2;
int f=1;
while(i<=n){
f *=(i+8);
i++;
}
printf("%d", f);
return 0;
}
-----------------------------------------------------------------------------------
-----
6. Create function using referaace and use it inside of main
void circle_length (int*a).
#include <stdlib.h>
#include <stdio.h>
void circle_length(int*a);
int main() {
int r = 3;
circle_length(&r);
return 0;
}
void circle_length(int* a){
float len = 2*3.14* *a;
printf("%f", len);
}
-----------------------------------------------------------------------------------
-
7.You have 2D array. Please write program for finding row's number with maximal
average of
elements.
#include <stdio.h>
#include <stdlib.h>
int main(){
int arr[10][10];
int r, c;
printf("Enter the number of rows");
scanf("%d", &r);
printf("Enter the number of columns");
scanf("%d", &c);
int i , j;
for(i = 0; i<r; i++){
for(j = 0; j<c; j++){
printf("enter a number: ");
scanf("%d", &arr[i][j]);
}
}
int sum = 0, row=0, maxRow;
int max = INT_MIN;
for(i = 0; i<r; i++){
for(j = 0; j<c; j++){
sum += arr[i][j];
}
row++;
if(max<(sum/c)){
max = sum/c;
maxRow = row;
}
}
printf("%d", maxRow);
return 0;
}
-----------------------------------------------------------------------------------
-
8. You have 2D array. Please write program for finding colums's number with maximal
average of
elements.
#include <stdio.h>
#include <stdlib.h>
int main(){
int arr[10][10];
int r, c;
printf("Enter the number of rows");
scanf("%d", &r);
printf("Enter the number of columns");
scanf("%d", &c);
int i , j;
for(i = 0; i<r; i++){
for(j = 0; j<c; j++){
printf("enter a number: ");
scanf("%d", &arr[i][j]);
}
}
int max = INT_MIN;
int m,n, col = 0, maxCol, sum = 0;
for(m = 0; m<r; m++){
for(n = 0; n<c; n++){
sum += arr[n][m];
}
col++;
if(max<sum/r){
max = sum/r;
maxRow = col;
}
}
printf("%d", maxCol);
return 0;
}
--------------------------------------------------------------------------------
9. User enters the x end n= 1,3 which is number of action using switchconsturction
calculate
#include <stdio.h>
int main(){
int x, n;
printf("Enter x);
scanf ("%d", &x);
printf("Enter n (1, 2, 3): ");
scanf("%d", &n);
switch (n){
case 1;
printf("%d", (-5*x*x-4));
break;
case 2:
printf("%d", (8*x+2));
break;
case 3:
printf("%d", (15-3*x));
break;
default:
printf("n is not correct);
}
return 0;
----------------------------------------------------------------------------------
10.Using dynamic memory allocation create arry of students structure. array size is
5.
#include <stdio.h>
#include <stdlib.h>
void swap(int* x, int* y){
int temp = *x;
*x = *у;
*y - temp;
}
struct Students{
char firstName[10];
char lastName [15];
int age;
int midtermExamGrade;
int finalexamGrade;
};
int main(){
struct Students *ptr;
ptr = (struct Students*) malloc(5 sizeof (struct Students));
if(ptr == NULL){
exit(1);
}
int i, j;
for(i = 0; i<5; i++){
printf("Enter first name and last name, age, midterm exam grade and final exam
grade;");
scanf("%s %s %d %d %d", (ptr +i)->firstName, (ptr+i)->lastName, &(ptr+i)-
>age,&(ptr+i)->midtermExamGrade, &(ptr+i)->finalExamGrade);
}
for(i = 0; i<5; i++){
for(j=0; j<5; j++){
if((ptr+j)->finalExamGrade>&(ptr+j+i)->finalExamGrade){
swap(&(ptr+j)->finalExamGrade, &(ptr+j+i)->finalExamGrade);
}
}
}
for(i = 0; i<5; i++;){
printf("%s %s %d %d %d\n", (ptr +i)->firstName, (ptr+i)->lastName, &(ptr+i)-
>age,&(ptr+i)->midtermExamGrade, &(ptr+i)->finalExamGrade);
}
return 0;
};
-----------------------------------------------------------------------------------
---