0% found this document useful (0 votes)
8 views6 pages

C Programming Language

The document contains multiple C programming tasks, including calculating the sum of even integers, computing factorials, handling arrays, and finding maximum values. It also covers input validation for non-negative integers and printing elements in increasing order. Additionally, it addresses operations on 2D arrays and string manipulation for full names.

Uploaded by

22142031
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)
8 views6 pages

C Programming Language

The document contains multiple C programming tasks, including calculating the sum of even integers, computing factorials, handling arrays, and finding maximum values. It also covers input validation for non-negative integers and printing elements in increasing order. Additionally, it addresses operations on 2D arrays and string manipulation for full names.

Uploaded by

22142031
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
You are on page 1/ 6

C programming language

Đề: Nhập số nguyên dương n. Tính tổng số nguyên dương chẵn đó:
#include <stdio.h>

int main() {
int n;
int sum=0;
// Nhập số nguyên dương n từ người dùng
printf("Nhập số nguyên dương n: ");
scanf("%d", &n);

// Kiểm tra nếu n không phải số nguyên dương thì yêu cầu nhập lại
while (n <= 0) {
printf("n phải là số nguyên dương. Nhập lại: ");
scanf("%d", &n);
}

// Tính tổng các số nguyên dương chẵn từ 0 đến n


while (n <= 0) ;
for(int i=2; i<=n; i+=2){

sum += i;
}

// In ra tổng các số nguyên dương từ 1 đến n


printf("Tổng các số nguyên dương chẵn từ 0 đến %d là: %d\n", n, sum);
return 0;
}
Đề: Write a program that reads a nonegative integer n, compute and print its
factorial.
// Online C compiler to run C program online
#include <stdio.h>

int main() {
int n;
int accumulation=1;
do
{
printf("Enter n: ");
scanf("%d", &n);}
while(n<0);
for(int i=1; i<=n; i++)
{
accumulation*= i;
}
printf("A factorial of n is %d!\n ", n);
printf("%d! = %d", n, accumulation);
return 0;
}
Đề: Write a C program to allow user enter a array of 10 integer numbers
then print all.
#include <stdio.h>

int main()
{
int a[10]; // 0 ->9
int i;
for(i=0; i<10; i++)
{
printf("Enter a[%d]= ", i);
scanf("%d", &a[i]);
}
printf("Output: \n");
for(i=0; i<10; i++)
{
printf("\t a[%d] = %d\n", i, a[i]);
}

return 0;
}
Đề: Write a C program to allow user enter a array of 10 integer numbers:
a/ print the maxixmum number.
b/ even max value
A/ #include <stdio.h>

int main()
{
int a[10]; // 0 ->9
int i;
for(i=0; i<10; i++)
{
printf("Enter a[%d]= ", i);
scanf("%d", &a[i]);
}
int max=a[0];
for(i=0; i<10; i++)
{
if (a[i] >max)
{
max=a[i];
}
printf("Max = %d\n", max);
}

return 0;
}
B/
#include <stdio.h>

void main() {
int array[10];
int i;
for (i=0; i<10; i++)
{
printf("Enter integer number %d: ",i+1);
scanf("%d", &array[i]);
}
int k=0;
for (i=0; i<10; i++)
{
if(array[i]%2==0)
{k++;}
}
if (k == 0)
{
printf("There is no even element");
}
else
{
int max;
for(i=0; i<10; i++)
{
if (array[i]%2 == 0)
{max = array[i];}
}
for (i=0; i<10; i++)
{
if((array[i] %2 == 0) && (array[i] > max))
{max = array[i];}
}
printf("Even max value: %d", max);
}

}
OR:
#include <stdio.h>

void main() {
int array[10];
int i;
for (i=0; i<10; i++)
{
printf("Enter integer number %d: ",i+1);
scanf("%d", &array[i]);
}
int k=0;
for (i=0; i<10; i++)
{
if(array[i]%2==0)
break;
}
if (i == 10)
{
printf("There is no even element");
}
else
{
int max = array[i];
for(i=i+1; i<10; i++)
{
if((array[i] %2 == 0) && (array[i] > max))
{max = array[i];}
}
printf("Even max value: %d", max);
}

}
Đề: Write a C program that calculate the average value of 20 integer
numbers input from user.
#include <stdio.h>

int main() {
int number;
int sum=0;
float average;
printf("Enter 20 numbers: \n");
for (int i=0; i <20; i++) // i==0
{
printf("number %d: ", i+1); // So 0: // i=1
scanf("%d", &number);
sum+= number;
}
average = (sum/20.0);
printf("the average value of 20 integer numbers: %f\n", average);
return 0;
}
Đề: Write a C program to allow user enter
(input) a array of 10 integer numbers
(negative value is not allowed):
a) print the maximum number
b) print the sum of all elements
c) print the sum of all even elements
#include<stdio.h>
void main()
{
int array[10];
int i;
for (i=0; i<10; i++)
{
do
{
printf("Enter integer number %d :", i+1);
scanf("%d", &array[i]);
}
while (array[i] <0);
}
int max= array[0];
for (i=0; i<10; i++)
{
if((array[i]) > max)
{max=array[i];}
}
printf("Max = %d", max);
int sum=0;
for(i=0;i<10;i++)
{sum+=array[i];}
printf("\n Sum of all elements:%d",sum);
int sumeven=0;
for(i=0;i<10;i++)
{if(array[i]%2==0)
{sumeven+=array[i];}
}
printf("\n Sum of all even elements: %d",sumeven);
}
Đề: Write a C program to allow user enter
(input) an array of 10 integer numbers. Print
all numbers in the form of increasing order
#include<stdio.h>
void main()
{
int array[10];
int i;
for (i=0; i<10; i++)
{
do
{
printf("Enter integer number %d :", i+1);
scanf("%d", &array[i]);
}
while (array[i] <0);
}
int j, temp;
for (i= 0; i<9; i++)
for (j= i+1; j<10; j++)
{
if ((array[i]) > (array[j]))
{temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
for (i=0; i<10; i++)
{printf("%d ", array[i]);}
}
Đề: Write a C program to read 2D array, the
array’s size is entered first (n row and m
column).
a) print the maximum element of array
b) print the maximum of elements on the
main diagonal
#include<stdio.h>
void main()
{
int a[5][5];
int i,j;
for (i=0; i<5; i++)
for (j=0; j<5; j++)
{printf("a[%d][%d]= ",i ,j);
scanf("%d",&a[i][j]);
}
int max= a[0][0];
for (i=0; i<5; i++)
for (j=0; j<5; j++)
{
if (max < a[i][j])
(max = a[i][j]);
}
printf("%d\n", max);
for (i=0; i<5; i++)
{
if (max < a[i][j])
max = a[i][j];}
printf("the maximum of the elenment of the main diagonal is
%d ", max);
}
Đề: Write a c program to read full name
a) print the given name
b) counting the number of words in the full
name

You might also like