C Programming Exercises: Practical Work 1 (TP 1)

Telechargé par sawsan kasti
UNIVERSITE LIBANAISE
Institut Universitaire de Technologie
TP 1: info I
Exercise 1:
Complete the following program described in C language:
# include <stdio.h>
Main ()
{ // This program will display "Hello from C language" on screen//
printf("Hello World!");
}
Exercise 2 :
Write a program that displays "Hello World"
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Exercise 3 :
Write a C program that:
a. Displays the message "Hello from C language" on one line.
#include <stdio.h>
int main() {
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
b. Displays the message "Hello from C language" as each word on a line.
#include<stdio.h>
int main()
{
int i;
char s[100];
scanf("%s", s);
for(i=0; s[i]!='\0'; i++)
{
printf("%c", s[i]);
if(s[i]==' ')
{
printf("\n");
}
}
}
c. Displays the message "Hello from C language", where each word is separated from the other tab.
Exercise 4 :
What is the result of the following code:
#include <stdio.h>
void main()
{
printf (“ \n * * \n * * * * \n * * * * *”);
}
Exercise 5 :
Write a program that displays the letter B as follows :
* * * * *
* *
* *
* * * * *
* *
* *
* * * * *
/ C++ implementation to print the
// pattern of alphabets A to Z using *
#include <stdio.h>
// Below height and width variable can be used
// to create a user-defined sized alphabet's pattern
// Number of lines for the alphabet's pattern
int height = 5;
// Number of character width in each line
int width = (2 * height) - 1;
// Function to find the absolute value
// of a number D
int abs(int d)
{
return d < 0 ? -1 * d : d;
}
// Function to print the pattern of 'B'
void printB()
{
int i, j, half = (height / 2);
for (i = 0; i < height; i++) {
printf("*");
for (j = 0; j < width; j++) {
if ((i == 0 || i == height - 1 || i == half)
&& j < (width - 2))
printf("*");
else if (j == (width - 2)
&& !(i == 0 || i == height - 1
|| i == half))
printf("*");
else
printf(" ");
}
printf("\n");
}
}
Exercise 6 :
Write a program that initializes two integers, displays their sum.
#include <stdio.h>
int main()
{
int num1, num2, sum;
printf("Enter first number :");
scanf("%d", &num1);
printf("Enter second number :");
scanf("%d", &num2);
sum = num1 + num2;
printf("sum of two numbers is %d", sum);
return 0;
}
1 / 3 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans l'interface ou les textes ? Ou savez-vous comment améliorer l'interface utilisateur de StudyLib ? N'hésitez pas à envoyer vos suggestions. C'est très important pour nous!