0% found this document useful (0 votes)
9 views27 pages

Revised Final CSE Spring2022 Solution EzazC223009

Uploaded by

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

Revised Final CSE Spring2022 Solution EzazC223009

Uploaded by

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

Revised CSE Final Spring 22 Solution

Name:Ezaz Ahmed ID:C223009


GLv‡b wKQz †Kv‡Wi `yBUv K‡i mwjDkb Av‡Q GKUv Avgvi Avi weKí mwjDkb Avgvi †d«Û
FahimulC223110 Gi| †h hviUv fv‡jv K‡i eySev IBUvB Kiev|
1(a)
#include<stdio.h>
int main()
{
int i,j;
for(i=0;i<3;i++)
for(j=2;j>=0;j--)
printf("%d %d\n",i,j);
return 0;
}

1(b)
#include<stdio.h>
int main()
{
int a=8,n=30,sum=0;
int i;
for(i=a;i<=n;i=i+3)
{
if(i%5==0)
{
sum=0;
continue;
}
sum=sum+i;
printf("sum = %d\n",sum);
}
return 0;}
Again;
#include<stdio.h>
int main()
{
int a=8,n=30,sum=0;
int i;
for(i=a;i<=n;i=i+3)
{
if(i%5==0)
{
sum=0;
break;
}
sum=sum+i;
printf("sum = %d\n",sum);
}
return 0;
}
1(c):
Solved By EzazC223009-
#include<stdio.h>
int main()
{
int i, j, N, columns;
columns=1;
scanf("%d",&N);
for(i=1; i<N*2; i++)
{
for(j=1; j<=columns; j++)
{
printf("*");
}
if(i < N)
{
columns++;
}
else
{
columns--;
}
printf("\n");
}
return 0;
}
Alternative Solution of 1(c) by FahimulC223110:
#include<stdio.h>
int main()
{
int i, j, n;
scanf("%d",&n);//3
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
{
printf("*");
}
printf("\n");
}
for(i=n-1; i>=1; i--)
{
for(j=1; j<=i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
1(d)
Solved By EzazC223009-
#include<stdio.h>
int main()
{
int a,i,sum=0,count=0;
for(i=0;;i++)
{
scanf("%d",&a);
if(a%3==0 || a%5==0)
{
continue;
}
else if(a<0)
{
break;
}
if(a%3!=0 || a%5!=0)
{
sum+=a;
count++;
}
}
double avg;
avg=(double)sum/count;
printf("Sum: %d\n",sum);
printf("Average: %.2lf\n",avg);
return 0;
}
1(d) or;
Solved By EzazC223009-
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 0 || n == 1)
{
flag = 1;
}
for (i = 2; i <= n / 2; ++i)
{
if (n % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number", n);
else
printf("%d is not a prime number", n);
return 0;
}
2(b)i:

2(b)ii:
2(d)
Solved By EzazC223009-
#include<stdio.h>
int is_Multiple(int n1, int n2)
{
if(n2%n1==0)
{
return 1;
}
else
{
return 0;
}
}
void main()
{
int n1, n2;
printf("Input the first integer : ");
scanf("%d", &n1);
printf("Input the second integer: ");
scanf("%d", &n2);
if(is_Multiple(n1, n2)==1)
printf("%d is a multiple of %d.\n", n2, n1);
else
printf("%d is not a multiple of %d.\n", n2, n1);
return 0;
}
2(d) or:
Solved By EzazC223009-
#include <stdio.h>
void main()
{
int number,sum;
printf("Enter a number: ");
scanf("%d", &number);
sum=factors(number);
printf("Sum: %d\n",sum);
}
int factors(int number)
{
int i,sum=0;
for (int i = 1; i <= number; i++)
{
if (number % i == 0)
{
if(i!=1 && i!=number)
{
sum=sum+i;
}
}
}
return(sum);
}
এর র এ এ Return Statement এর র সলভ র ল Return
Statement র ল
Alternative Solution of 2(d)or By FahimulC223110:
#include<stdio.h>
int sum_factors(int n)
{
int sum;
for(int i=2; i<n; i++)
{
if(n%i==0)
sum+=i;
}
return sum;
}
int main()
{
int a,x;
printf("Enter a number:");
scanf("%d",&a);
x=sum_factors(a);
printf("%d",x);
}
3(a)
Solved By EzazC223009-
#include <stdio.h>
int main()
{
int i;
int arr[20]={2,2,3,0,0,9}; //My id=C223009
for (int i = 1; i < 7; i++)
{
arr[i] += arr[i - 1];
}
printf("Cumulative sum of the array : ");
for (i = 0; i < 6; i++)
{
printf("%d ", arr[i]);
}
return 0;
}

Go to the next page


3(b):
Solved By EzazC223009-
#include <stdio.h>
int main()
{
int arr[6][6],n,RowSum=0,ColSum=0;
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
scanf("%d",&arr[i][j]);
}
}
scanf("%d",&n);
for(int i=1;i<=5;i++){
RowSum += arr[n][i];
ColSum += arr[i][n];
}
printf("Row Sum = %d\nCol Sum = %d",RowSum,ColSum);
return 0;
}

Go to the next page


3(b) or:
Solved By EzazC223009-
#include <stdio.h>
int main()
{
int array[100][100];
int i, j, m, n, a = 0, sum = 0;
printf("Enter the order of the matix \n");
scanf("%d %d", &m, &n);
if (m == n )
{
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
sum = sum + array[i][i];
a = a + array[i][m - i - 1];
}
printf("\nThe sum of the main diagonal elements is = %d\n", sum);
printf("The sum of the secondary diagonal elements is = %d\n", a);
}
else
printf("The given order is not square matrix\n");
return 0;
}
Alternative Solution of 3(b)or By FahimulC223110:
#include <stdio.h>
#include<math.h>
int main()
{
int i, j, n, k, sum1 = 0,sum2=0;
scanf("%d", &n);
int a[n + 5][n + 5];
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (i == j)
{
sum1 += a[i][j];
}
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (i+j == n-1)
{
sum2 += a[i][j];
}
}
}
printf("sum of the elements of 1st diagonal = %d\n",sum1);
printf("sum of the elements of 2nd diagonal = %d\n",sum2);
return 0;
}
3(c):
3(d)
Solved By EzazC223009-
#include <stdio.h>
#include <string.h>
int Identical(char *str)
{
int n = strlen(str);
for (int i = 1; i < n; i++)
{
if (str[i] != str[0])
return 0;
}
return 1;
}
int main()
{
char arr[100];
scanf("%s", &arr);

if(Identical(arr))
printf("Identical.");
else
printf("Not Identical.");

return 0;
}
Alternative Solution of 3(d) By FahimulC223110:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int i,f = 1;
scanf("%s",str);
for(int i = 0; i<strlen(str); i++)
{
if(str[0] != str[i])
{
f = 0;
break;
}
}
if(f==0)
{
printf("Not Identical\n");
}
else
{
printf("Identical\n");
}
return 0;
}
3(d) or;
Solved By EzazC223009-
#include<stdio.h>
int main()
{
char s[100];
scanf("%s",&s);
if(s[2]=='T' && s[3]=='N')
{
printf("Age=%c%c,Taxpayer,Not a Landowner",s[0],s[1]);
}
else if(s[2]=='T' && s[3]=='L')
{
printf("Age=%c%c,Taxpayer,Landowner",s[0],s[1]);
}
else if(s[2]=='N' && s[3]=='N')
{
printf("Age=%c%c,Not a Taxpayer,Not a Landowner",s[0],s[1]);
}
else if(s[2]=='N' && s[3]=='L')
{
printf("Age=%c%c,Not a Taxpayer,Landowner",s[0],s[1]);
}
return 0;
}
4(b): The main difference between pass by value and pass by reference is that, in a
pass by value, the parameter value copies to another variable while, in a pass by
reference, the actual parameter passes to the function.
Example:
Pass by Value:
#include <stdio.h>
void swap (int a, int b)
{
int temp = a;
a = b;
b = temp;
}
int main ()
{
int a = 10;
int b = 20;
printf ("Before swap, a = %d, b = %d\n", a, b);
swap (a, b);
printf ("After swap, a = %d, b = %d\n", a, b);
return 0;
}

Pass By Reference:
#include <stdio.h>
int main ()
{
int a = 100;
int b = 200;
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );
swap(&a, &b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
return 0;
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
return;
}
4(d)
Solved By EzazC223009-
#include <stdio.h>
int main()
{
struct Player
{
int runs;
char name[24],country[16];
double average;
}
player[10];
int i;
for(i=0;i<10;i++)
{
printf("Enter Name: ");
scanf("%s",player[i].name);
printf("Enter Country: ");
scanf("%s",&player[i].country);
printf("Enter Runs: ");
scanf("%d",&player[i].runs);
printf("Enter Average: ");
scanf("%lf",&player[i].average);
}
for(i=0;i<10;i++)
{
printf("\nName: %s",player[i].name);
printf("\nCountry: %s",player[i].country);
printf("\nRuns: %d",player[i].runs);
printf("\nAverage: %.3lf",player[i].average);
}
int max;
max=0;
for(i=0;i<10;i++)
{
if(player[i].average>player[max].average)
{
max=i;
}
}
printf("\n\nThe player who has the highest average is: %s\n",player[max].name);
return 0;
}
৩ ১০

Go to the Next Page:


4(d) or:

#include <stdio.h>
int main()
{
int N,i;
scanf("%d",&N);
struct CT
{
int ct1_marks,ct2_marks;
char Id_no[100],name[100];
}ct[N];
for(i=0;i<N;i++)
{
scanf("%s %s %d %d",ct[i].Id_no,ct[i].name,&ct[i].ct1_marks,&ct[i].ct2_marks);
}
for(i=0;i<N;i++)
{
if(ct[i].ct1_marks>ct[i].ct2_marks)
printf("%d\n",ct[i].ct1_marks);
else
printf("%d\n",ct[i].ct2_marks);
}
return 0;
}
5(a):
feof() function is used to determine if the end of the file (stream) specified
has been reached or not. This function keeps on searching the end of the file
(EOF) in the file program.

5(b):
Solved By EzazC223009-
strrev() use সলভ ল ল ভ
#include<stdio.h>
int main()
{
FILE *fp;
FILE *rev;
char ch;
int i=0,pos;
fp=fopen("copy.txt","r");
rev=fopen("clone.txt","w");
if(fp==NULL)
{
printf("File does not exist..");
return 0;
}
fseek(fp,0,SEEK_END);
pos=ftell(fp);
while(i<pos)
{
i++;
fseek(fp,-i,SEEK_END);
ch=fgetc(fp);
fputc(ch,rev);
}
fclose(fp);
fclose(rev);
return 0;
}
After executing the code

Go to the Next Page:


Alternative Solution of 5(b) By FahimulC223110:
strrev() ও use
#include<stdio.h>
#include<string.h>
int main()
{
char str[100],clone[100];
FILE *fr;
fr = fopen("copy.txt", "r");
if(fr==NULL)
{
printf("File does not exist..");
return 0;
}
fgets(str,100,fr);
fclose(fr);
FILE *fw = fopen("clone.txt", "w");
strcpy(clone,strrev(str));
fprintf( fw,"%s", clone);
fclose(fw);
return 0;
}

After executing the code


5(b) or:
Solved By EzazC223009-
#include<stdio.h>
int main()
{
FILE *fp=fopen("test.txt","w");
int i,n,a[10];
printf("Enter 10 Numbers => ");
for(i=0; i<10; i++)
{
scanf("%d",&n);
fprintf(fp,"%d ",n);
}
fclose(fp);
printf("\nNumbers in reverse order => ");
FILE *fpr=fopen("test.txt","r");
for(i=0; i<10; i++)
{
fscanf(fpr,"%d ", &a[i]);
}
for(i=9;i>=0;i--)
{
printf("%d ",a[i]);
}
printf("\n");
fclose(fpr);
}
5(d) i:
#include <stdio.h>
#define MULTI(x,y) x*y
int main()
{
printf("%d ",MULTI(2+3,3+5));
return 0;
}

5(d) ii:
#include<stdio.h>
#define cube(x) x*x*x
int main()
{
int x=36/cube(6);
printf("%d",x);
return 0;
}

Thanks Everyone Assalamualikum

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy