0% found this document useful (0 votes)
248 views

C Programming

Write a program in C that reads a first name, last name, and year of birth and display the names and the year one after another sequentially.

Uploaded by

shiela
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)
248 views

C Programming

Write a program in C that reads a first name, last name, and year of birth and display the names and the year one after another sequentially.

Uploaded by

shiela
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/ 24

Write a program in C that reads a firstname, lastname and year of birth

and display the names and the year one after another sequentially.

#include <stdio.h>
int main()
{
char firstname[20], lastname[20];
int bir_year;
printf("Input your firstname: ");
scanf("%s", firstname);
printf("Input your lastname: ");
scanf("%s", lastname);
printf("Input your year of birth: ");
scanf("%d", &bir_year);
printf("%s %s %d\n", firstname, lastname, bir_year);
return 0;
}

Input your firstname: Tom

Input your lastname: Davis

Input your year of birth: 1982

Tom Davis 1982

Flowchart:

Write a C program to print your name, date of birth. and mobile number.
#include <stdio.h>
int main()
{
printf("Name : Alexandra Abramov\n");
printf("DOB : July 14, 1975\n");
printf("Mobile : 99-9999999999\n");
return(0);
}

Name : Alexandra Abramov

DOB : July 14, 1975

Mobile : 99-9999999999
Flowchart:

Printing Name Using C Language

#include<stdio.h> 
void main() 
{
     printf("Name : John");  
     getch();  //to add a Pause 
}

Write a Program to Display your Name in C.

#include<stdio.h> 
void main() 
{
  printf("My Name is John"); 
  getch(); 
}
C Program to input name and Display
#include<stdio.h> 
void main() 
{
  char name[20]; 
  printf("Enter Your Name : ");
  scanf("%s",&name); 
  printf("Your name is : %s", name); 
  getch();  
}

C program to print name and address


#include<stdio.h> 
void main()
{
  printf("My Name is John and My Address is : 21 Jump Street
NewYork");   
  getch();  //add a Pause 
}

Write a program to print your name at the center of the screen


#include<stdio.h> 
void main()
{
  gotoxy(40,13); 
  printf("Rosie"); 
  getch();
}

& name in c
#include <stdio.h>
 
int main()
{
    char name[20];
    printf("Enter your Name\n");
    scanf("%s",&name);
    printf("\nYour name is = %s",name);
    getch();
}
c program to print name using array
#include<stdio.h> 
void main() 
{
     char name[] = {'j','o','h','n'}; 
     int i=0;
     while(i<sizeof(name)){
         printf("%c",name[i]);
         i++;
     }
     getch(); 
}

Variables and Types


#include <stdio.h>

int main() {
int a = 3;
float b = 4.5;
double c = 5.25;
float sum;

/* Your code goes here */

printf("The sum of a, b, and c is %f.", sum);


return 0;
}

Arrays
#include <stdio.h>

int main() {
/* TODO: define the grades variable here */
int average;

grades[0] = 80;
/* TODO: define the missing grade
so that the average will sum to 85. */
grades[2] = 90;

average = (grades[0] + grades[1] + grades[2]) / 3;


printf("The average of the 3 grades is: %d", average);

return 0;
}

Conditions
#include <stdio.h>

void guessNumber(int guess) {


// TODO: write your code here
}

int main() {
guessNumber(500);
guessNumber(600);
guessNumber(555);
}

Strings
#include <stdio.h>
#include <string.h>
int main() {
/* define first_name */
/* define last_name */
char name[100];

last_name[0] = 'B';
sprintf(name, "%s %s", first_name, last_name);
if (strncmp(name, "John Boe", 100) == 0) {
printf("Done!\n");
}
name[0]='\0';
strncat(name,first_name,4);
strncat(name,last_name,20);
printf("%s\n",name);
return 0;
}

For loops
#include <stdio.h>

int main() {
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int factorial = 1;
int i;

/* calculate the factorial using a for loop here*/

printf("10! is %d.\n", factorial);


}

While loops
#include <stdio.h>

int main() {
int array[] = {1, 7, 4, 5, 9, 3, 5, 11, 6, 3, 4};
int i = 0;

while (i < 10) {


/* your code goes here */

printf("%d\n", array[i]);
i++;
}

return 0;
}

If…else

// Program to display a number if it is negative

#include <stdio.h>int main() {

int number;
printf("Enter an integer: ");

scanf("%d", &number);

// true if number is less than 0

if (number < 0) {

printf("You entered %d.\n", number);

printf("The if statement is easy.");

return 0;

C User-defined functions
Here is an example to add two integers. To perform this task, we
have created an user-defined  addNumbers() .

#include <stdio.h>int addNumbers(int a, int b); // function


prototype

int main(){

int n1,n2,sum;

printf("Enters two numbers: ");

scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call

printf("sum = %d",sum);

return 0;

int addNumbers(int a, int b) // function definition {

int result;

result = a+b;

return result; // return statement

Function types

Example 1: No arguments passed and no return


value

#include <stdio.h>

void checkPrimeNumber();

int main(){

checkPrimeNumber(); // argument is not passed

return 0;

// return type is void meaning doesn't return any valuevoid


checkPrimeNumber(){

int n, i, flag = 0;

printf("Enter a positive integer: ");


scanf("%d",&n);

for(i=2; i <= n/2; ++i)

if(n%i == 0)

flag = 1;

if (flag == 1)

printf("%d is not a prime number.", n);

else

printf("%d is a prime number.", n);

C Recursion
void recurse()

... .. ...

recurse();

... .. ...

int main()
{

... .. ...

recurse();

... .. ...

Example: Sum of Natural Numbers Using Recursion

#include <stdio.h>int sum(int n);

int main() {

int number, result;

printf("Enter a positive integer: ");

scanf("%d", &number);

result = sum(number);

printf("sum = %d", result);

return 0;

int sum(int n) {

if (n != 0)

// sum() function calls itself

return n + sum(n-1);

else

return n;
}

Output

Enter a positive integer:3

sum = 6

C Arrays
Example 1: Array Input/Output

// Program to take 5 values from the user and store them in an array//
Print the elements stored in the array#include <stdio.h>

int main() {

int values[5];

printf("Enter 5 integers: ");

// taking input and storing it in an array

for(int i = 0; i < 5; ++i) {

scanf("%d", &values[i]);

printf("Displaying integers: ");


// printing elements of an array

for(int i = 0; i < 5; ++i) {

printf("%d\n", values[i]);

return 0;

Output

Enter 5 integers: 1

-3

34

Displaying integers: 1

-3

34

C Multidimensional Arrays
Example 1: Two-dimensional array to store and print
values

// C program to store temperature of two cities of a week and display


it.#include <stdio.h>const int CITY = 2;const int WEEK = 7;int main(){
int temperature[CITY][WEEK];

// Using nested loop to store values in a 2d array

for (int i = 0; i < CITY; ++i)

for (int j = 0; j < WEEK; ++j)

printf("City %d, Day %d: ", i + 1, j + 1);

scanf("%d", &temperature[i][j]);

printf("\nDisplaying values: \n\n");

// Using nested loop to display vlues of a 2d array

for (int i = 0; i < CITY; ++i)

for (int j = 0; j < WEEK; ++j)

printf("City %d, Day %d = %d\n", i + 1, j + 1, temperature[i]


[j]);

return 0;

Output
City 1, Day 1: 33

City 1, Day 2: 34

City 1, Day 3: 35

City 1, Day 4: 33

City 1, Day 5: 32

City 1, Day 6: 31

City 1, Day 7: 30

City 2, Day 1: 23

City 2, Day 2: 22

City 2, Day 3: 21

City 2, Day 4: 24

City 2, Day 5: 22

City 2, Day 6: 25

City 2, Day 7: 26

Displaying values:

City 1, Day 1 = 33

City 1, Day 2 = 34

City 1, Day 3 = 35

City 1, Day 4 = 33

City 1, Day 5 = 32

City 1, Day 6 = 31

City 1, Day 7 = 30

City 2, Day 1 = 23
City 2, Day 2 = 22

City 2, Day 3 = 21

City 2, Day 4 = 24

City 2, Day 5 = 22

City 2, Day 6 = 25

City 2, Day 7 = 26

Example 2: Sum of two matrices

// C program to find the sum of two matrices of order 2*2

#include <stdio.h>int main(){

float a[2][2], b[2][2], result[2][2];

// Taking input using nested for loop

printf("Enter elements of 1st matrix\n");

for (int i = 0; i < 2; ++i)

for (int j = 0; j < 2; ++j)

printf("Enter a%d%d: ", i + 1, j + 1);

scanf("%f", &a[i][j]);

// Taking input using nested for loop

printf("Enter elements of 2nd matrix\n");

for (int i = 0; i < 2; ++i)

for (int j = 0; j < 2; ++j)


{

printf("Enter b%d%d: ", i + 1, j + 1);

scanf("%f", &b[i][j]);

// adding corresponding elements of two arrays

for (int i = 0; i < 2; ++i)

for (int j = 0; j < 2; ++j)

result[i][j] = a[i][j] + b[i][j];

// Displaying the sum

printf("\nSum Of Matrix:");

for (int i = 0; i < 2; ++i)

for (int j = 0; j < 2; ++j)

printf("%.1f\t", result[i][j]);

if (j == 1)

printf("\n");

return 0;

}
Output

Enter elements of 1st matrix

Enter a11: 2;

Enter a12: 0.5;

Enter a21: -1.1;

Enter a22: 2;

Enter elements of 2nd matrix

Enter b11: 0.2;

Enter b12: 0;

Enter b21: 0.23;

Enter b22: 23;

Sum Of Matrix:

2.2 0.5

-0.9 25.0

Example 3: Three-dimensional array

// C Program to store and print 12 values entered by the user

#include <stdio.h>int main(){

int test[2][3][2];

printf("Enter 12 values: \n");

for (int i = 0; i < 2; ++i)


{

for (int j = 0; j < 3; ++j)

for (int k = 0; k < 2; ++k)

scanf("%d", &test[i][j][k]);

// Printing values with proper index.

printf("\nDisplaying values:\n");

for (int i = 0; i < 2; ++i)

for (int j = 0; j < 3; ++j)

for (int k = 0; k < 2; ++k)

printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);

return 0;
}

Output

Enter 12 values:

10

11

12

Displaying Values:

test[0][0][0] = 1

test[0][0][1] = 2

test[0][1][0] = 3

test[0][1][1] = 4

test[0][2][0] = 5

test[0][2][1] = 6

test[1][0][0] = 7
test[1][0][1] = 8

test[1][1][0] = 9

test[1][1][1] = 10

test[1][2][0] = 11

test[1][2][1] = 12

Multidimensional Arrays
#include <stdio.h>

int main() {
/* TODO: declare the 2D array grades here */
float average;
int i;
int j;

grades[0][0] = 80;
grades[0][1] = 70;
grades[0][2] = 65;
grades[0][3] = 89;
grades[0][4] = 90;

grades[1][0] = 85;
grades[1][1] = 80;
grades[1][2] = 80;
grades[1][3] = 82;
grades[1][4] = 87;

/* TODO: complete the for loop with appropriate terminating


conditions */
for (i = 0; i < ; i++) {
average = 0;
for (j = 0; j < ; j++) {
average += grades[i][j];
}

/* TODO: compute the average marks for subject i */


printf("The average marks obtained in subject %d is:
%.2f\n", i, average);
}
return 0;
}
C Arrays & Function

Example 1: Pass Individual Array Elements

#include <stdio.h>void display(int age1, int age2) {

printf("%d\n", age1);

printf("%d\n", age2);

int main() {

int ageArray[] = {2, 8, 4, 12};

// pass second and third elements to display()

display(ageArray[1], ageArray[2]);

return 0;

Output

Example 2: Pass Arrays to Functions

// Program to calculate the sum of array elements by passing to a


function

#include <stdio.h>float calculateSum(float num[]);

int main() {
float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};

// num array is passed to calculateSum()

result = calculateSum(num);

printf("Result = %.2f", result);

return 0;

float calculateSum(float num[]) {

float sum = 0.0;

for (int i = 0; i < 6; ++i) {

sum += num[i];

return sum;

Output

Result = 162.50

Example 3: Pass two-dimensional arrays

#include <stdio.h>void displayNumbers(int num[2][2]);

int main() {

int num[2][2];

printf("Enter 4 numbers:\n");
for (int i = 0; i < 2; ++i) {

for (int j = 0; j < 2; ++j) {

scanf("%d", &num[i][j]);

// pass multi-dimensional array to a function

displayNumbers(num);

return 0;

void displayNumbers(int num[2][2]) {

printf("Displaying:\n");

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

for (int j = 0; j < 2; ++j) {

printf("%d\n", num[i][j]);

Output

Enter 4 numbers:

4
5

Displaying:

C Exercises: Convert a given integer to


years, months and days
#include <stdio.h>

int main() {
int ndays, y, m, d;

printf("Input no. of days: ");


scanf("%d", &ndays);
y = (int) ndays/365;
ndays = ndays-(365*y);
m = (int)ndays/30;
d = (int)ndays-(m*30);

printf(" %d Year(s) \n %d Month(s) \n %d Day(s)", y, m, d);


return 0; }

You might also like